Question about singly linked lists

  • Thread starter Thread starter AcecA
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on the manipulation of singly linked lists using pointer arithmetic, particularly in the context of C-like pseudocode. It clarifies that directly accessing the previous element in a singly linked list is not feasible without maintaining a separate pointer to the previous node. The conversation emphasizes that using expressions like "*(p-1).next = p+1" can lead to undefined behavior if not handled correctly, especially when the pointers do not correspond to valid list elements. The importance of checking pointer validity before dereferencing is also highlighted to avoid runtime errors.

PREREQUISITES
  • Understanding of singly linked lists and their structure
  • Familiarity with pointer arithmetic in C or similar languages
  • Knowledge of memory management and pointer validity
  • Basic programming concepts related to data structures
NEXT STEPS
  • Study the implementation of singly linked lists in C
  • Learn about pointer arithmetic and its implications in data structures
  • Explore error handling techniques for pointer dereferencing
  • Investigate alternative data structures that allow bidirectional traversal, such as doubly linked lists
USEFUL FOR

Software developers, computer science students, and anyone interested in understanding data structures and memory management in programming.

AcecA
Messages
12
Reaction score
0
If you have a pointer that's pointing to one element in a list, can you do the following

(*biggestPtr-1).next := biggestPtr+1
?

This is pseudocode with := meaning the assignment operator.
 
Technology news on Phys.org
It looks like you are using something similar to C pointer notation and I guess you would like to remove an element pointed to by p from the list by doing "*(p-1).next = p+1". If so, note that pointer arithmetic is similar to using array index, that is, p-1 points to the element in memory prior to p and this is in general not the same as the element prior to p in the linked list.

In a single linked list where each element chain to the next, it is by definition not possible to find the element prior to an element directly from the element itself. You must maintain (or obtain by iteration from the head of the list) a pointer to the previous element separately and use that to unchain the element in question (with special care if the element to remove is the first element in the list).
 
i assume bigestPtr is the size of your list.
In this case the last Element of your list *(biggestPtr-1) points to an element that does not (yet) exist.
This is no problem if you always check .next >= biggestPtr.
And indeed the last element of the list can't have a valid next-pointer.
But a reference to *(biggestPtr.next) might give a runtime error now, or produce random results.
If biggestPtr is not the size of the list, but points to any other element. you have set (*biggestPtr-1).next to the next element in memory, which is not necessarily the next logical element of the list.
 
Last edited:

Similar threads

  • · Replies 40 ·
2
Replies
40
Views
3K
  • · Replies 16 ·
Replies
16
Views
4K
Replies
86
Views
4K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 20 ·
Replies
20
Views
6K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K