2011-01-20 45 views
1

我对下面的一段代码有几个问题。请多多包涵。代码可能很容易理解,但我仍处于学习过程中,所以对我来说这仍然是抽象的。将线性链表转换为循环链表

struct listNode { 
int data; 
struct listNode *next }; 

//Is J a pointer, pointing to the head of the linked list? 
struct listNode * convert (struct listNode * J) { 

if (J == NULL) 
    return NULL; 

//Is this creating a new temporary pointer that will traverse the linked list? 
//Is it being set to J so that it can start at the first node and go to the last? 
struct listNode * temp = J; 

while (temp -> next != NULL) 
    temp = temp->next; //Is this where the temp pointer actually goes through the list? 

//Temp->next will eventually become the last node of the list and that will be set to J 
//which is the head pointer? 
    temp->next = J; 

return temp; 
} 
+0

和你的问题是什么? – sdadffdfd 2011-01-20 03:54:30

+0

我的问题在 – kachilous 2011-01-20 03:57:21

回答

3

一切你写的评论是真实的,并在最后你可以考虑任何点作为head因为它是一个圆形的列表现在

线性链表和一个圆形的之间的唯一区别最后一个节点在第一种情况下指向NULL,或者它指向第一个节点,第二个节点指向第一个节点。

算法:

1)你把temp指针找到的最后一个节点(与J初始化它的头,并解析列表,直到你打NULL)

2)你指向temp,这是现在的最后一个节点,到第一个节点,即J