Question

I know that to traverse I can make a temp linked list and go:

while (temp->next!=NULL){
    ...}

However, what if I want to change one position of my actual lists while traversing it? The only way I can think of is to traverse the actual linked list from the head, but wouldn't it destroy my linked list after it reaches the end?

Was it helpful?

Solution

assume you have a list myList, and myList.head points to the first element:

temp = myList.head;
while (temp->next!=NULL){
  // do stuff with this element
  ...
  temp = temp->next; // get the next element
  }

Now you can go right back and do it again - myList is still the same, and

temp = myList.head;

gets you right back to the beginning again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top