C 删除单数链表中的元素
在这段代码中,我正在删除链表中的元素 11->12->13->14->15->12->16 如果我想删除12,它只删除第一次出现的元素,即o/p wll be 11->13->14->15->12->16 但是我想删除所有出现的12个,怎么做呢 有人能给我一些建议吗C 删除单数链表中的元素,c,linked-list,C,Linked List,在这段代码中,我正在删除链表中的元素 11->12->13->14->15->12->16 如果我想删除12,它只删除第一次出现的元素,即o/p wll be 11->13->14->15->12->16 但是我想删除所有出现的12个,怎么做呢 有人能给我一些建议吗 #include<stdio.h> #include<stdlib.h> void insertbeg(); void delpos(); void display()
#include<stdio.h>
#include<stdlib.h>
void insertbeg();
void delpos();
void display();
struct node
{
int info;
struct node *link;
}*first=NULL;
struct node *create();
int item,key;
main()
{
int choice;
while(1)
{
printf("\nchoices are:\n");
printf("\n1.Insertbeg\n 2.delpos\n 3.display\n 4.exit\n");
printf("Enter U'r choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: insertbeg(); break;
case 2: delpos(); break;
case 3: display(); break;
case 4: exit(1);
default: printf("INVALID CHOICE TRY AGAIN\n");
}
}
}
struct node *create()
{
struct node *new;
new=(struct node*)malloc(sizeof(struct node));
return(new);
}
void insertbeg()
{
struct node *new;
new=create();
printf("Enter element to be inserted: ");
scanf("%d",&item);
if(first==NULL)
{
new->info=item;
new->link=NULL;
first=new;
}
else
{
new->info=item;
new->link=first;
first=new;
}
}
void delpos()
{
int key;
struct node *temp,*prev;
if(first==NULL)
{
printf("LIST IS EMPTY\n");
return;
}
else
{
temp=first;
printf("Enter the KEY element which is to be deleted: ");
scanf("%d",&key);
/* while(temp->info!=key&&temp->link!=NULL)
{
prev=temp;
temp=temp->link;
}
if(temp->info==key)
{
prev->link=temp->link;
free(temp);
}
else
printf("key element not found in the list\n");
*/
while(temp->link != NULL)
{
if(temp->info == key)
{
prev->link = temp->link;
free(temp);
temp = prev->link;
temp = temp->link;
}
else
temp = temp->link;
}
}
}
void display()
{
struct node *temp;
temp=first;
if(temp==NULL)
{
printf("LIST IS EMPTY\n");
return;
}
else
{
printf("Elements in Linked Lists: ");
while(temp!=NULL)
{
printf("%d->",temp->info);
temp=temp->link;
}
}
}
#包括
#包括
void insertbeg();
void delpos();
void display();
结构节点
{
国际信息;
结构节点*链接;
}*第一个=空;
结构节点*create();
int项,键;
main()
{
智力选择;
而(1)
{
printf(“\n选项为:\n”);
printf(“\n1.Insertbeg\n 2.delpos\n 3.display\n 4.exit\n”);
printf(“输入您的选择:”);
scanf(“%d”,选择(&C);
开关(选择)
{
案例1:insertbeg();中断;
案例2:delpos();中断;
案例3:显示();中断;
案例4:出口(1);
默认值:printf(“无效选择重试\n”);
}
}
}
结构节点*create()
{
结构节点*新建;
new=(结构节点*)malloc(sizeof(结构节点));
返回(新);
}
void insertbeg()
{
结构节点*新建;
新建=创建();
printf(“输入要插入的元素:”);
scanf(“%d”项和项目);
if(first==NULL)
{
新建->信息=项目;
新建->链接=NULL;
第一个=新的;
}
其他的
{
新建->信息=项目;
新建->链接=第一个;
第一个=新的;
}
}
void delpos()
{
int键;
结构节点*temp,*prev;
if(first==NULL)
{
printf(“列表为空\n”);
返回;
}
其他的
{
温度=第一;
printf(“输入要删除的关键元素:”);
scanf(“%d”和键)(&key);
/*而(临时->信息!=键和临时->链接!=空)
{
prev=温度;
温度=温度->链接;
}
如果(临时->信息==键)
{
上一个->链接=临时->链接;
免费(临时);
}
其他的
printf(“在列表中找不到关键元素\n”);
*/
while(临时->链接!=NULL)
{
如果(临时->信息==键)
{
上一个->链接=临时->链接;
免费(临时);
温度=上一个->链接;
温度=温度->链接;
}
其他的
温度=温度->链接;
}
}
}
无效显示()
{
结构节点*temp;
温度=第一;
if(temp==NULL)
{
printf(“列表为空\n”);
返回;
}
其他的
{
printf(“链表中的元素:”);
while(temp!=NULL)
{
printf(“%d->”,temp->info);
温度=温度->链接;
}
}
}
删除复选框temp->info=从文件中键入
,然后在while主体内使用if来执行该操作,while主体将成为启动器。删除复选框temp->info=输入文件中的
,然后在while主体中使用if进行操作。我可以发现您的代码存在两个问题,但没有一个问题与您的示例输入有关
1-
应该是
while(temp!=NULL)
2-temp=temp->链接代码>在中是多余的
if(temp->info == key)
{
prev->link = temp->link;
free(temp);
temp = prev->link;
temp = temp->link;
}
并跳过一个元素 我可以发现您的代码有两个问题,但没有一个会显示您的示例输入有问题
1-
应该是
while(temp!=NULL)
2-temp=temp->链接代码>在中是多余的
if(temp->info == key)
{
prev->link = temp->link;
free(temp);
temp = prev->link;
temp = temp->link;
}
并跳过一个元素 最后
prev不需要赋值吗
我可以在注释掉的部分看到它的分配,但在其他地方看不到。显然,如果第一个元素与键匹配,就需要对其进行空检查。最后
prev不需要赋值吗
我可以在注释掉的部分看到它的分配,但在其他地方看不到。显然,如果第一个元素与键匹配,则需要对其进行空检查。删除代码的主要问题是,需要将第一个元素视为特殊情况,因为第一个元素可能是要删除的节点,因此可能需要使用新节点更新第一个元素。这可以通过使用双指针来解决,比如struct node**temp=&first代码>。然而,我试图接近你原来的帖子
// special condition if first should be removed.
temp = first;
while ( temp != NULL && temp->info == key ){
first = temp->link;
free(temp);
temp = first;
}
// Here temp->info should not be removed(or it is NULL)
// lets look at temp->link->info and remove temp->link
while (temp!=NULL && temp->link != NULL) {
if (temp->link->info == key) {
struct node *to_free = temp->link;
// temp checks the next node.
temp->link = temp->link->link;
free(to_free);
} else {
// next link
temp = temp->link;
}
}
请注意,prev是不必要的 删除代码的主要问题是需要将第一个元素视为特殊情况,因为第一个元素可能是要删除的节点,因此可能需要使用新节点更新第一个元素。这可以通过使用o来解决
while(temp->info!=key&&temp->link!=NULL)
...
temp=first;
prev=NULL; /* Added */
printf("Enter the KEY element which is to be deleted: ");
scanf("%d",&key);
while(temp->info!=key&&temp->link!=NULL)
{
prev=temp;
temp=temp->link;
}
if(temp->info==key)
{
if (prev==NULL) /* Added */
first=temp->link; /* Added */
else /* Added */
prev->link=temp->link;
free(temp);
}
...