双链表程序
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *rnext;
struct node*lnext;
}*first=NULL,*last=NULL;
void display()
{
struct node*temp;
if(first==NULL)
{
printf("list is empty\n");
return;
}
temp=first;
while(temp!=NULL)
{
printf("%d \n",temp->data);
temp=temp->rnext;
}
}
void insertion()
{
struct node *temp;
struct node *nn= (struct node*) malloc(sizeof(struct node));
printf("enter data to be inserted\n");
nn->rnext=NULL;
last->rnext=nn;
nn->lnext=last;
last=nn;
}
void deletion()
{
struct node *temp;
if(first==NULL||last==NULL)
{
printf("list is empty\n");
return;
}
temp=first;
first=first->rnext;
first->lnext=NULL;
free(temp);
}
/* main loop */
int main()
{
int option;
do
{
printf("enter option 1.insertion\n2.display\n3.deletion\n4.exit\n");
scanf("%d",&option);
switch(option)
{
case 1:
insertion();
break;
case 2:
display();
break;
case 3:
deletion();
break;
}
} while(option!=4);
}
我写了一个双链表,在 linux 中使用 C 语言在末尾插入一个节点并在开头删除一个节点。但是在执行程序时出现错误分段错误。我也在发布输出。
./out
enter option 1.insertion
2.display
3.deletion
4.exit
1
enter data to be inserted
12
Segmentation fault
please help me with the solution for segmentation fault
这是我的代码,请帮我运行它并调试它。我在最后插入节点并在最后删除节点