我的循环链表有问题。我相信问题出在我的显示功能上。请让我知道发生了什么问题。我的问题是,第n-1个元素都显示出来,然后我得到一个分段错误(最后一个元素没有得到显示,我得到一个分段错误)。谢谢:-)在C中的循环链表中显示函数
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* link;
};
struct Node* last = NULL;
void Insert_begin(int a)
{
struct Node* temp;
temp = malloc(sizeof(struct Node));
temp->data = a;
if (last == NULL)
last = temp;
else
{
temp->link = last->link;
last->link = temp;
}
}
void Display()
{
struct Node* temp;
if (last == NULL)
{
printf("list is empty");
}
temp = last->link;
while(temp!=last)
{
printf("%d\n",temp->data);
temp = temp->link;
}
printf("%d\n",temp->data);
}
int main()
{
Insert_begin(0);
Insert_begin(1);
Insert_begin(2);
Insert_begin(3);
Insert_begin(4);
Display();
return 0;
}
'请让我知道了什么错误':你是不是做任何调试。 –
是啊!我需要学习调试。开始做吧。谢谢 –
如果你感兴趣的话,还有一个可能证明有用的[**一个奇异链接的循环链接列表**]的完整例子(http://pastebin.com/BirWtkvb)。 –