简体   繁体   中英

Splitting a circular linked list

So I got a question in data structures assignment earlier this week. The question was to split a circularly linked list into two halves, with elements at the even positions in one list and the one at odd positions in another.This is the logic I tried out.

temp= p1 = front;
p2= front->next;
do
 {p1->next = temp->next->next;
  p1= temp->next->next;
  p2->next = temp->next->next->next;
  p2= temp->next->next->next;
  temp=temp->next;
 } while (temp!= front);
p1->next=p1;
p2->next=p2;

My logic was to create two pointers , one pointing to the first element and one to the next. Then I traverse the lists from one end to the other. While I do so, I set the next pointer of each to point to elements at the alternate positions. Note that p1, p2, front, temp and next are point of struct Node which is defined as this

  typedef struct 
  {
   type element;
   Node* next;
  }Node;

However my professor marked my answer as incorrect without any explanation. Can someone tell me why this was wrong?

假设第一个temp指向一个奇数位置,因此p1指向奇数, p2指向偶数元素,但是在第一个temp=temp->next temp之后指向一个偶数元素,并且将p1分配为p1-temp->next->next是偶数元素,现在p2将指向一个奇数位置元素。在每次交替迭代中, p1将指向偶数位置和其他偶数位置, p2也是如此。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM