【发布时间】:2021-02-25 04:10:37
【问题描述】:
目的是制作一个程序,删除链表中最后一次出现的元素 例如:-如果链表是 23->45->23->24->23 然后我寻找输出 23->45->23->24 但是我在调试时遇到了几个错误,主要是在 temp->next!=NULL 和 t1->next=t->next 部分,请指出阻碍程序执行的错误?
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void insertLL(struct node *head,int data)
{
struct node *temp;
struct node *new=(struct node *)malloc(sizeof(struct node));
new->data=data;
if(head==NULL)
{
head=new;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new;
}
}
void delete(struct node *head,int data)
{
struct node *temp=head;
struct node *t;
struct node *t1;
while(temp->next!=NULL)
{
if(temp->next->data==data)
{
t1=temp;
}
if(temp->data==data)
{
t=temp;
}
temp=temp->next;
}
t1->next=t->next;
free(t);
}
void display(struct node *head)
{
struct node *temp1=head;
if (head==NULL)
{
printf("List is empty");
}
else
{
while(temp1->next!=NULL)
{
printf("%d",temp1->data);
printf("->");
temp1=temp1->next;
}
}
}
int main()
{
int u,k,l,n;
struct node *start;
while(1)
{
printf("Press 1 to insert element into the Linked List, press 2 to delete the last occurence of an element from a linked list, press 3 to display the linked list, press 0 to exit the program\n");
scanf("%d",&u);
switch(u)
{
case 1:
{
printf("Enter the data you want to insert\n");
scanf("%d",&l);
insertLL(start,l);
break;
}
case 2:
{
printf("Enter the element that you want to be deleted\n");
scanf("%d",&k);
delete(start,k);
break;
}
case 3:
{
display(start);
break;
}
default:
{
printf("Invalid input\n");
}
}
printf("Enter 1 to continue or 0 to break\n");
scanf("%d",&n);
if(n==1)
{
continue;
}
if(n==0)
{
break;
}
}
}
【问题讨论】:
-
请告诉我们您遇到了什么错误或问题。即给出准确的输入、预期结果和实际结果。
-
是的,我通过调试器运行了代码,如果链表是 23->45->23->24->23 那么我寻找输出 23->45->23 ->24 但我在 temp->next!=NULL 和 t1->next=t->next 行的删除函数中遇到分段错误
-
请edit提供该信息的问题。
-
一个主要问题:
head=new;那行不通。head是一个局部变量,因为函数参数是在 C 中传递的值。更改该值不会更改调用者的变量。 -
那么我应该全局初始化struct node *start,然后直接初始化新的节点start吗?
标签: c struct linked-list singly-linked-list function-definition