当我在链表中插入任何值之前选择删除选项时,出现分段错误
when i am selecting delete option before inserting any value in linked list i am getting segmentation fault error
#include<stdio.h>
#include<malloc.h>
typedef struct nnode
{
int value;
struct nnode *next;
} node;
void insert(node **ptr, int val) //for insertion
{
node *temp, *temp2;
temp = *ptr;
if (*ptr == NULL)//if list is empty
{
temp = (node *) malloc(sizeof (node));
temp->value = val;
temp->next = NULL;
*ptr = temp;
}
else
{
while (temp->next != NULL)
{
temp = temp->next;
}
temp2 = (node *) malloc(sizeof (node));
temp2->value = val;
temp2->next = NULL;
temp->next = temp2;
}
}
void display_node(node **ptr)
{
node *temp;
temp = *ptr;
while (temp != NULL)
{
printf("%d--->", temp->value);
temp = temp->next;
}
printf("null");
}
void de_node(node **ptr)
{
int val;
node *temp, *temp2;
temp = *ptr;
temp2 = temp->next;
printf("\nenter the value to be deleted\n");
scanf("%d", &val);
if ((*ptr) == NULL)
{
printf("list is empty .....");
return;
}
else if ((*ptr)->value == val)
{
*ptr = (*ptr)->next;
free(temp);
}
else
{
while ((temp->next->value != val)&&(temp2->next != NULL))
{
temp = temp->next;
temp2 = temp->next;
}
if (temp2->next == NULL)
{
printf("\nvalue not found");
}
if (temp->next->value == val)
{
temp->next = temp2->next;
free(temp2);
}
}
}
void main()
{
node *head = NULL;
int ch;
int n;
while (1)
{
printf("\nenter your choice\n");
printf("1.ADD ELEMENT\n");
printf("2.DELETE ELEMENT\n");
printf("3.DISPLAY LIST\n");
printf("4.EXIT\n");
scanf("%d", &ch);
switch (ch)
{
case 1:printf("\n enter data \n");
scanf("%d", &n);
insert(&head, n);
display_node(&head);
break;
case 2:de_node(&head);
display_node(&head);
break;
case 3:display_node(&head);
break;
case 4:exit(0);
}
}
}
我的问题是在删除元素时在列表中插入任何内容之前
即当我试图在列表为空时删除一个元素
然后根据我的代码它应该打印 "list is empty....."
但它给出了分段错误。
问题在这里:
temp = *ptr;
temp2 = temp->next;
您已将 temp
设置为 *ptr
,但您尚未检查 *ptr
是否为 NULL。因此,当您尝试使用 temp->next
取消引用它时,您会遇到段错误。
由于您稍后才使用 temp2
,因此请将此行移至 while
循环之前:
temp2 = temp->next;
#include<stdio.h>
#include<malloc.h>
typedef struct nnode
{
int value;
struct nnode *next;
} node;
void insert(node **ptr, int val) //for insertion
{
node *temp, *temp2;
temp = *ptr;
if (*ptr == NULL)//if list is empty
{
temp = (node *) malloc(sizeof (node));
temp->value = val;
temp->next = NULL;
*ptr = temp;
}
else
{
while (temp->next != NULL)
{
temp = temp->next;
}
temp2 = (node *) malloc(sizeof (node));
temp2->value = val;
temp2->next = NULL;
temp->next = temp2;
}
}
void display_node(node **ptr)
{
node *temp;
temp = *ptr;
while (temp != NULL)
{
printf("%d--->", temp->value);
temp = temp->next;
}
printf("null");
}
void de_node(node **ptr)
{
int val;
node *temp, *temp2;
temp = *ptr;
temp2 = temp->next;
printf("\nenter the value to be deleted\n");
scanf("%d", &val);
if ((*ptr) == NULL)
{
printf("list is empty .....");
return;
}
else if ((*ptr)->value == val)
{
*ptr = (*ptr)->next;
free(temp);
}
else
{
while ((temp->next->value != val)&&(temp2->next != NULL))
{
temp = temp->next;
temp2 = temp->next;
}
if (temp2->next == NULL)
{
printf("\nvalue not found");
}
if (temp->next->value == val)
{
temp->next = temp2->next;
free(temp2);
}
}
}
void main()
{
node *head = NULL;
int ch;
int n;
while (1)
{
printf("\nenter your choice\n");
printf("1.ADD ELEMENT\n");
printf("2.DELETE ELEMENT\n");
printf("3.DISPLAY LIST\n");
printf("4.EXIT\n");
scanf("%d", &ch);
switch (ch)
{
case 1:printf("\n enter data \n");
scanf("%d", &n);
insert(&head, n);
display_node(&head);
break;
case 2:de_node(&head);
display_node(&head);
break;
case 3:display_node(&head);
break;
case 4:exit(0);
}
}
}
我的问题是在删除元素时在列表中插入任何内容之前
即当我试图在列表为空时删除一个元素
然后根据我的代码它应该打印 "list is empty....."
但它给出了分段错误。
问题在这里:
temp = *ptr;
temp2 = temp->next;
您已将 temp
设置为 *ptr
,但您尚未检查 *ptr
是否为 NULL。因此,当您尝试使用 temp->next
取消引用它时,您会遇到段错误。
由于您稍后才使用 temp2
,因此请将此行移至 while
循环之前:
temp2 = temp->next;