문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top