#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*first=NULL;
void insert()
{
struct node *temp;
struct node *nn=(struct node*)malloc(sizeof(struct node));
printf("enter the data\n");
scanf("%d",&nn->data);
temp=first;
while(temp->next!=first)
temp=temp->next;
temp->next=nn;
nn->next=NULL;
}
void display()
{
struct node *temp;
temp=first;
if(temp==NULL)
{
printf("no elements\n");
return;
}
printf("elements in linked list are\n");
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}
void deletion()
{
struct node *temp;
temp=first;
first=first->next;
temp->next=NULL;
free(temp);
}
int main()
{
int op;
do
{
printf("1.insertion\n2.deletion\n3.display\n4.exi\n");
printf("enter option\n");
scanf("%d",&op);
switch(op)
{
case 1:insert();
break;
case 2:deletion();
break;
case 3:display();
break;
}
}while(op!=6);
}
这是一个为单链表编写的程序。并执行获取错误分段错误。请建议解决这个问题。 我得到下面的输出 ./out链接列表程序插入和删除节点
1.insertion
2.deletion
3.display
4.exit
输入选项
1
输入数据
23
Segmentation fault
参考http://tinyit.cc/letslearncs/插入链接列表/在链接列表中插入节点,以及http://tinyit.cc/letslearncs/deletion-in-linked-list在链接中删除名单。 – 2014-06-27 18:16:08