C “简化我的链接列表”;搜索并删除“;功能
函数要求用户输入要搜索的值,然后删除列表中所有匹配的值 该程序运行完美,但我无法找到一种简单/优雅的方法来区分“第一个节点”、“最后一个节点”和“中间节点”的情况 请帮我把它弄得更优雅易读 我将发布相关的代码块,然后是整个程序 在相关函数中,以下代码块是我使用的开关盒构造的一部分(如果用户输入“v”,则表示搜索“值”并删除,而不是“删除第一个节点”或“删除最后一个节点”) 完整程序:C “简化我的链接列表”;搜索并删除“;功能,c,linked-list,C,Linked List,函数要求用户输入要搜索的值,然后删除列表中所有匹配的值 该程序运行完美,但我无法找到一种简单/优雅的方法来区分“第一个节点”、“最后一个节点”和“中间节点”的情况 请帮我把它弄得更优雅易读 我将发布相关的代码块,然后是整个程序 在相关函数中,以下代码块是我使用的开关盒构造的一部分(如果用户输入“v”,则表示搜索“值”并删除,而不是“删除第一个节点”或“删除最后一个节点”) 完整程序: #include <stdio.h> #include <stdlib.h> stru
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
int count(struct node *);
void traverse(struct node *);
void insert(struct node **);
void delete(struct node **);
void deletelist(struct node **);
int main()
{ char choice;
struct node *head, **tohead;
head=(struct node *)malloc (sizeof(struct node));
head=NULL;
tohead=&head;
do
{
printf("\nChoose operation to perform:\n");
printf("\tCount number of nodes(c):\n");
printf("\tInsert a node(i)\n");
printf("\tDelete a node(d)\n");
printf("\tDelete the list(D)\n");
printf("\tShow currenct linked list(s)\n");
printf("\tQuit without saving changes(q):\t");
scanf(" %c",&choice);
switch(choice)
{
case 'i':
insert(tohead);
break;
case 'c':
printf("\nList count is %d\n",count(head));
break;
case 's':
traverse(head);
break;
case 'q':
printf("QUITTING\n");
break;
case 'd':
delete(tohead);
break;
case 'D':
deletelist(tohead);
break;
default:
printf("\n\tINVALID CHOICE\n");
}
}while(choice!='q');
}
int count(struct node *p)
{
int i;
for(i=0;p;i++)
p=p->next;
return i;
}
void traverse(struct node *p)
{
printf("\nLinked list looks like: ");
if(!p)
{
printf("nothing. It's EMPTY");
}
else
{ while(p)
{
printf("%d\t",p->data);
p=p->next;
}
}
printf("\n");
}
void insert(struct node **pp)
{
int value,position;
struct node *q;
q=*pp;
struct node *newnode=(struct node *)malloc(sizeof(struct node));
printf("Insert what number?:\t");
scanf("%d",&value);
printf("In what position? Push '0' for last,'1' for first");
printf("\n\t\tOR\nenter position no.:\t");
scanf("%d",&position);
newnode->data=value;
if(position==1)
{ newnode->next=q;
*pp=newnode;
}
else if(position==0)
{
if(!q)
{
newnode->next=q;
*pp=newnode;
}
else
{
while(q->next)
q=q->next;
q->next=newnode;
newnode->next=NULL;
}
}
else if((position>1)&&(position<=count(q)+1))
{
int i;
for(i=1;i<position-1;i++)
q=q->next;
newnode->next=q->next;
q->next=newnode;
}
else
printf("\n\t\tINVALID POSITION\n");
}
void delete(struct node **pp)
{
struct node *q,*r,*temp;
q=*pp;
int i,count=0,value,position;
char choice;
printf("\n\tPush 'v' to delete a specific value");
printf("\n\t\t\tOR");
printf("\n\t'1' to delete the first value");
printf("\n\t'0' to delete the last value:\t");
scanf(" %c",&choice);
if(q==NULL)
{ printf("List is EMPTY\n");
return;
}
switch(choice)
{
case '1':
*pp=q->next;
free(q);
break;
case '0':
while(q->next!=NULL)
{
r=q;
q=q->next;
}
r->next=NULL;
free(q);
break;
case 'v':
printf("\n\tEnter value to delete:\t");
scanf("%d",&value);
r=q;
if(!q)//If linked list is empty
{
printf("\n\t\tLinked list is EMPTY\n");
return;
}
else if(!(q->next))//If linked list consists of only one node
{
if((q->data)==value)
{
free(q);
printf("\nValue found and deleted in position 1\n");
count=1;
}
}
else//If linked list consists of more than one node
{
for(i=1;q;i++)
{
if(q->data==value)
{
if(i==1)//for first node
{
q=q->next;
free(r);
*pp=q;
printf("\nValue found and deleted in position 1\n");
count=1;
}
else//for the rest of the nodes
{
printf("\nValue found and deleted in position %d\n",i);
r->next=q->next;
temp=q;
q=q->next;
free(temp);
count++;
}
}
else
{
r=q;
q=q->next;
}
}
}
if(count==0)//If no matches are found
printf("Value not found");
break;
default:
printf("\nBad choice");
}return;
}
void deletelist(struct node **pp)
{
struct node *p,*temp;
p=*pp;
while(p)
{
temp=p;
p=p->next;
free(temp);
}
*pp=NULL;
printf("\nLIST DELETED\n");
}
#包括
#包括
结构节点
{
int数据;
结构节点*下一步;
};
整数计数(结构节点*);
无效遍历(结构节点*);
无效插入(结构节点**);
作废删除(结构节点**);
作废删除列表(结构节点**);
int main()
{字符选择;
结构节点*head,**tohead;
head=(结构节点*)malloc(sizeof(结构节点));
head=NULL;
tohead=&head;
做
{
printf(“\n选择要执行的操作:\n”);
printf(“\t节点数(c):\n”);
printf(“\t插入节点(i)\n”);
printf(“\t删除节点(d)\n”);
printf(“\t删除列表(D)\n”);
printf(“\t显示当前链接列表)\n”);
printf(“\t未保存更改的线路(q):\t”);
scanf(“%c”,选择(&c));
开关(选择)
{
案例“i”:
插入(tohead);
打破
案例“c”:
printf(“\n列表计数为%d\n”,计数(头));
打破
案例s:
导线(头部);
打破
案例‘q’:
printf(“退出”);
打破
案例“d”:
删除(tohead);
打破
案例“D”:
删除列表(tohead);
打破
违约:
printf(“\n\t有效选项\n”);
}
}while(choice!='q');
}
整数计数(结构节点*p)
{
int i;
对于(i=0;p;i++)
p=p->next;
返回i;
}
无效遍历(结构节点*p)
{
printf(“\n链接列表看起来像:”);
如果(!p)
{
printf(“没什么,它是空的”);
}
其他的
{while(p)
{
printf(“%d\t”,p->data);
p=p->next;
}
}
printf(“\n”);
}
无效插入(结构节点**pp)
{
int值、位置;
结构节点*q;
q=*pp;
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
printf(“插入什么号码?:\t”);
scanf(“%d”,和值);
printf(“在什么位置?最后按“0”,第一按“1”);
printf(“\n\t\tOR\n输入者位置号:\t”);
扫描频率(“%d”,位置(&P);
新建节点->数据=值;
如果(位置==1)
{newnode->next=q;
*pp=新节点;
}
否则如果(位置==0)
{
if(!q)
{
newnode->next=q;
*pp=新节点;
}
其他的
{
while(q->next)
q=q->next;
q->next=newnode;
newnode->next=NULL;
}
}
如果((位置>1)和&(位置下一步=q->下一步);
q->next=newnode;
}
其他的
printf(“\n\t\t有效位置\n”);
}
作废删除(结构节点**pp)
{
结构节点*q,*r,*temp;
q=*pp;
int i,计数=0,值,位置;
字符选择;
printf(“\n\t按“v”删除特定值”);
printf(“\n\t\t\tOR”);
printf(“\n\t'1'删除第一个值”);
printf(“\n\t'0”删除最后一个值:\t”);
scanf(“%c”,选择(&c));
if(q==NULL)
{printf(“列表为空\n”);
返回;
}
开关(选择)
{
案例“1”:
*pp=q->next;
免费(q);
打破
案例“0”:
while(q->next!=NULL)
{
r=q;
q=q->next;
}
r->next=NULL;
免费(q);
打破
案例“v”:
printf(“\n\t要删除的输入值:\t”);
scanf(“%d”,和值);
r=q;
if(!q)//如果链表为空
{
printf(“\n\t\t链接列表为空\n”);
返回;
}
else if(!(q->next))//如果链表只包含一个节点
{
如果((q->数据)=值)
{
免费(q);
printf(“\n在位置1中找到并删除了值”);
计数=1;
}
}
else//如果链表由多个节点组成
{
对于(i=1;q;i++)
{
如果(q->data==值)
{
如果(i==1)//对于第一个节点
{
q=q->next;
自由(r);
*pp=q;
printf(“\n在位置1中找到并删除了值”);
计数=1;
}
else//用于其余节点
{
printf(“\n在位置%d\n”i中找到并删除的值);
r->next=q->next;
温度=q;
q=q->next;
免费(临时);
计数++;
}
}
其他的
{
r=q;
q=q->next;
}
}
}
if(count==0)//如果找不到匹配项
printf(“未找到值”);
打破
违约:
printf(“\n广告选择”);
}返回;
}
无效删除列表(结构节点**pp)
{
结构节点*p,*temp;
p=*pp;
while(p)
{
温度=p;
p=p->n
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
int count(struct node *);
void traverse(struct node *);
void insert(struct node **);
void delete(struct node **);
void deletelist(struct node **);
int main()
{ char choice;
struct node *head, **tohead;
head=(struct node *)malloc (sizeof(struct node));
head=NULL;
tohead=&head;
do
{
printf("\nChoose operation to perform:\n");
printf("\tCount number of nodes(c):\n");
printf("\tInsert a node(i)\n");
printf("\tDelete a node(d)\n");
printf("\tDelete the list(D)\n");
printf("\tShow currenct linked list(s)\n");
printf("\tQuit without saving changes(q):\t");
scanf(" %c",&choice);
switch(choice)
{
case 'i':
insert(tohead);
break;
case 'c':
printf("\nList count is %d\n",count(head));
break;
case 's':
traverse(head);
break;
case 'q':
printf("QUITTING\n");
break;
case 'd':
delete(tohead);
break;
case 'D':
deletelist(tohead);
break;
default:
printf("\n\tINVALID CHOICE\n");
}
}while(choice!='q');
}
int count(struct node *p)
{
int i;
for(i=0;p;i++)
p=p->next;
return i;
}
void traverse(struct node *p)
{
printf("\nLinked list looks like: ");
if(!p)
{
printf("nothing. It's EMPTY");
}
else
{ while(p)
{
printf("%d\t",p->data);
p=p->next;
}
}
printf("\n");
}
void insert(struct node **pp)
{
int value,position;
struct node *q;
q=*pp;
struct node *newnode=(struct node *)malloc(sizeof(struct node));
printf("Insert what number?:\t");
scanf("%d",&value);
printf("In what position? Push '0' for last,'1' for first");
printf("\n\t\tOR\nenter position no.:\t");
scanf("%d",&position);
newnode->data=value;
if(position==1)
{ newnode->next=q;
*pp=newnode;
}
else if(position==0)
{
if(!q)
{
newnode->next=q;
*pp=newnode;
}
else
{
while(q->next)
q=q->next;
q->next=newnode;
newnode->next=NULL;
}
}
else if((position>1)&&(position<=count(q)+1))
{
int i;
for(i=1;i<position-1;i++)
q=q->next;
newnode->next=q->next;
q->next=newnode;
}
else
printf("\n\t\tINVALID POSITION\n");
}
void delete(struct node **pp)
{
struct node *q,*r,*temp;
q=*pp;
int i,count=0,value,position;
char choice;
printf("\n\tPush 'v' to delete a specific value");
printf("\n\t\t\tOR");
printf("\n\t'1' to delete the first value");
printf("\n\t'0' to delete the last value:\t");
scanf(" %c",&choice);
if(q==NULL)
{ printf("List is EMPTY\n");
return;
}
switch(choice)
{
case '1':
*pp=q->next;
free(q);
break;
case '0':
while(q->next!=NULL)
{
r=q;
q=q->next;
}
r->next=NULL;
free(q);
break;
case 'v':
printf("\n\tEnter value to delete:\t");
scanf("%d",&value);
r=q;
if(!q)//If linked list is empty
{
printf("\n\t\tLinked list is EMPTY\n");
return;
}
else if(!(q->next))//If linked list consists of only one node
{
if((q->data)==value)
{
free(q);
printf("\nValue found and deleted in position 1\n");
count=1;
}
}
else//If linked list consists of more than one node
{
for(i=1;q;i++)
{
if(q->data==value)
{
if(i==1)//for first node
{
q=q->next;
free(r);
*pp=q;
printf("\nValue found and deleted in position 1\n");
count=1;
}
else//for the rest of the nodes
{
printf("\nValue found and deleted in position %d\n",i);
r->next=q->next;
temp=q;
q=q->next;
free(temp);
count++;
}
}
else
{
r=q;
q=q->next;
}
}
}
if(count==0)//If no matches are found
printf("Value not found");
break;
default:
printf("\nBad choice");
}return;
}
void deletelist(struct node **pp)
{
struct node *p,*temp;
p=*pp;
while(p)
{
temp=p;
p=p->next;
free(temp);
}
*pp=NULL;
printf("\nLIST DELETED\n");
}
void filter(struct node **pp, int value){
struct node *curr, *prev=NULL, *tmp;
curr = *pp;
if(!curr)
return ;
tmp = curr;
while(tmp){
if(tmp->data != value){
curr->data = tmp->data;
prev = curr;
curr = curr->next;
}
tmp = tmp->next;
}
deletelist(&curr);
if(prev){
prev->next = NULL;
} else {
*pp = NULL;
}
}