這是我爲循環鏈接列表編寫的代碼的link。代碼也粘貼在下面。從單個鏈接到循環鏈接列表的轉換
typedef struct node
{
int value;
struct node *next;
}mynode;
mynode *head, *tail, *temp,*sp,*fp;
void add(int value);
void iterative_reverse();
void print_list();
void findcycle();
int main()
{
head=(mynode *)0;
add(1);
add(2);
add(3);
//print_list();
findcycle();
return(0);
}
void add(int value)
{
temp = (mynode *) malloc(sizeof(struct node));
temp->value=value;
temp->next=(mynode *)0;
if(head==(mynode *)0)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
tail->next=head;
temp->next=head;
}
}
void findcycle()
{
if (head == NULL || head->next == NULL)
printf("null");
sp=head;
fp=head->next;
while (fp != NULL && fp->next != NULL)
{
if ((fp == sp) || (fp->next == sp))
printf("Cycle");
sp = sp->next;
fp = fp->next->next;
}
printf("Not a Cycle");
}
void print_list()
{
for(temp=head; temp!=tail; temp=temp->next)
printf("[%d]->",(temp->value));
}
我最初寫它的單,然後改變一些指點,使其循環。我在做一些錯誤,我無法跟蹤,因此得到一個超時。請建議。
非常感謝。
我的不好!得到了錯誤..感謝很多。 – Ava 2011-03-09 19:20:01
@vartika:那麼你應該upvote和/或接受最好的答案。 – Massimo 2011-03-09 19:44:07
這兩個答案都是一樣的,但是這是更詳細的解釋。我如何upvote? – Ava 2011-03-09 19:54:09