链表实现增删改查

链表实现增删改查
输入一串数字直到-1结束,然后在之前的数据上进行增删改查。
#include
#include
struct num
{
int data;
struct num *next;
};
//增
void add(int n1,struct num *p1,struct num *q1,struct num *head1)
{
struct num *t,*s;
int k,i,a;
t=head1;
while(t!=NULL)
{
printf("%5d",t->data);
t=t->next;
}
printf(" -1\n");
printf(“please input a number for k\n”);
scanf("%d",&k);
s=head1;
for(i=1; i<=n1; i++)
{
if(i==k)
{
p1=(struct num *)malloc(sizeof(struct num));
printf(“please input a number\n”);
scanf("%d",&a);
p1->data=https://www.it610.com/article/a;
p1->next=s->next;
s->next=p1;
break;
}
s=s->next;
}
t=head1;
while(t!=NULL)
{
printf("%5d",t->data);
t=t->next;
}
printf(" -1\n");
}
//删
void delet(int n1,struct num *p1,struct num *q1,struct num *head1)
{
int k,a,i;
struct num *s,*m,*t;
t=head1;
printf(“修改之前为:\n”);
while(t!=NULL)
{
printf("%5d",t->data);
t=t->next;
}
printf(" -1\n");
printf(“please input a number for k\n”);
scanf("%d",&k);
s=head1;
for(i=1; i<=n1; i++)
{
if(i==k-1)
{
m=s->next;
s->next=m->next;

break; } s=s->next; } t=head1; while(t!=NULL) { printf("%5d",t->data); t=t->next; } printf("-1\n");

}
//改
void modification(int n1,struct num *p1,struct num *q1,struct num *head1)
{
int k,a,i;
struct num *t,*s;
t=head1; printf("修改之前为:\n"); while(t!=NULL) { printf("%5d",t->data); t=t->next; } printf("-1\n"); printf("please input a number for k\n"); scanf("%d",&k); s=head1; for(i=1; i<=n1; i++) { if(i==k) { printf("please input a data for struct num k\n"); scanf("%d",&a); s->data=https://www.it610.com/article/a; break; } s=s->next; } t=head1; while(t!=NULL) { printf("%5d",t->data); t=t->next; } printf("-1\n");

}
//查
void search(int n1,struct num *p1,struct num *q1,struct num *head1)
{
int k,a,i;
struct num *t,*s;
t=head1; printf("修改之前为:\n"); while(t!=NULL) { printf("%5d",t->data); t=t->next; } printf("-1\n"); printf("please input a number for k\n"); scanf("%d",&k); s=head1; for(i=1; i<=n1; i++) { if(i==k) { printf("%5d\n",s->data); break; } s=s->next; }

}
int main()
{
int b,flag=0;
int a,n;
struct num *p,*q,*head;
//菜单(使得界面更为清晰明了)
printf("1 is add\n"); printf("2 is delet\n"); printf("3 is modification\n"); printf("4 is search\n"); printf("0 is over\n"); //从菜单中的选项选择,对应相应的功能 printf("please choose a number\n"); scanf("%d",&b); //初始化 head=NULL; n=0; //输入一串数字直到-1结束 printf("please input a number\n"); fflush(stdin); scanf("%d",&a); while(a!=-1) { p=(struct num *)malloc(sizeof(struct num)); p->data=https://www.it610.com/article/a; p->next=NULL; if(head==NULL) { head=p; } else { q->next=p; } q=p; printf("please input a number\n"); scanf("%d",&a); n++; } while(flag==0) { switch(b) {case 1: { add(n,p,q,head); printf("please choose a number\n"); scanf("%d",&b); break; } case 2: { delet(n,p,q,head); printf("please choose a number\n"); scanf("%d",&b); break; } case 3: { modification(n,p,q,head); printf("please choose a number\n"); scanf("%d",&b); break; } case 4: { search(n,p,q,head); printf("please choose a number\n"); scanf("%d",&b); break; } case 0: { printf("game over\n"); flag=1; break; } } }system("pause"); return 0;

}
【链表实现增删改查】链表实现增删改查
文章图片

    推荐阅读