⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 实验三 单链表的操作建立 显示 查找 插入和删除.cpp

📁 数据结构代码(严为民)
💻 CPP
字号:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct student  
{char name[15];
struct student * next;
};
/*The program can repeat to circulate much */


void print(struct student *head,int n)
/*print all elements of list*/
{struct student *P;
int m;
printf("Print list elements:");
P=head;
if(head!=NULL)
for(m=1;m<=n;m++)
{printf("%s ",P->name);
P=P->next;
}
}

void find(char findname[],struct student *head,int n)
/*find location element of list*/
{struct student *P=head;int m;
for(m=1;m<=n;m++)
{if(strcmp(P->name,findname)==0)break;P=P->next;}

if(m<=n)
{printf("to find location of equal list element:");
printf("%d",m);}
else printf("not to find equal list element");
}

struct student * dele(struct student *head,int dl,int n)
/*delete element at some location of list*/
{struct student * P,* Q;
int m;
P=head;
if(dl==1)       
{head=P->next;
P->next=NULL;
}
else{for(m=1;m<=n;m++)
{if(m==dl)break;      
Q=P;P=P->next;
}					
Q->next=P->next;
P->next=NULL;}
return(head);
}

struct student *add(struct student *head,struct student *ae,int al,int n)
/*add element at some location of list*/
{int m;struct student *P,*Q;
P=head;
if(al==1)            
{ae->next=head;
head=ae;} 
else{
for(m=1;m<=n;m++)
{if(m==al)break;
Q=P;P=P->next;
}
ae->next=Q->next;
Q->next=ae;}
return(head);
}


void main ()



{struct student *head;
struct student *P,*Q,*ae; 
int n,m=1,l=0,dl,al; 
/*dl:that is a location to delete an element */
/*al:that is a location to add an element */
/*ae:the address of building a new element*/
/*flag:when flag is equal one,program can repeat*/
char findname[15], loopname[10],flag;

printf("enter list element number:");
scanf("%d",&n);
printf("input all elements:");
P=(struct student *)malloc(sizeof(struct student));
scanf("%s",P->name);
head=NULL;
for(;m<=n;m++)
{l++;
if(l==1)head=P;
else Q->next=P;
Q=P;
if(m<n)
{P=(struct student *)malloc(sizeof(struct student));
scanf("%s",P->name);}
}
P->next=NULL;

print(head,n);


loopfind:
printf("\nenter name to find location of equal list element:");
scanf("%s",findname);getchar();/*absorb char 'enter'.The mean below "getchar();" is the same*/
find(findname,head,n);
printf("\nenter value of flag(when flag is '1',program find can repeat.or quit find):");
scanf("%c",&flag);
while(flag=='1')
{printf("enter name to find equal list element:");
scanf("%s",findname);getchar();
find(findname,head,n);
printf("\nenter value of flag(when flag is '1',program find can repeat.or quit find):");
scanf("%c",&flag);}
printf("input 'loopfind'、'loopdele' or 'loopadd' switch to  opposite program:");
scanf("%s",loopname);
if(strcmp(loopname,"loopfind")==0)goto loopfind;
if(strcmp(loopname,"loopdele")==0)goto loopdele;
if(strcmp(loopname,"loopadd")==0)goto loopadd;
else printf("switch to below dele program\n");



loopdele:
printf("\ndelete a list element in what location of the list:");
scanf("%d",&dl);getchar();
if(dl<=n)
{head=dele(head,dl,n);
n--;
print(head,n);}
else printf("not exist %dth element",dl);
printf("\nenter value of flag(when flag is '1',program dele can repeat.or quit dele):");
scanf("%c",&flag);
while(flag=='1')
{printf("\ndelete a list element in what location of the list:");
scanf("%d",&dl);getchar();
if(dl<=n)
{head=dele(head,dl,n);
n--;
print(head,n);}
else printf("not exist %dth element",dl);
printf("\nenter value of flag(when flag is '1',program dele can repeat.or quit dele):");
scanf("%c",&flag);}	
printf("input'loopfind'、'loopdele' or 'loopadd' switch to  opposite program:");
scanf("%s",loopname);
if(strcmp(loopname,"loopfind")==0)goto loopfind;
if(strcmp(loopname,"loopdele")==0)goto loopdele;
if(strcmp(loopname,"loopadd")==0)goto loopadd;
else printf("switch to below add program\n");



loopadd:
printf("\nadd a list element in  what location of the list:");
scanf("%d",&al);if(al>n+1)getchar();
if(al<=n+1)
{ae=(struct student*)malloc(sizeof(struct student));
printf("input adding element:");
scanf("%s",ae->name);getchar();
head=add(head,ae,al,n);n++;
print(head,n);}
else printf("not exist %dth element",al);
printf("\nenter value of flag(when flag is '1',program add can repeat.or quit add):");
scanf("%c",&flag);
while(flag=='1')
{printf("\nadd a list element in  what location of the list:");
scanf("%d",&al);if(al>n+1)getchar();
if(al<=n+1)
{ae=(struct student*)malloc(sizeof(struct student));
printf("input adding element:");
scanf("%s",ae->name);getchar();
head=add(head,ae,al,n);n++;
print(head,n);}
else printf("not exist %dth element",al);
printf("\nenter value of flag(when flag is '1',program add can repeat.or quit add):");
scanf("%c",&flag);}
printf("input 'loopfind'、'loopdele'or 'loopadd' switch to  opposite program:");
scanf("%s",loopname);
if(strcmp(loopname,"loopfind")==0)goto loopfind;
if(strcmp(loopname,"loopdele")==0)goto loopdele;
if(strcmp(loopname,"loopadd")==0)goto loopadd;
else printf("over the program");

printf("\n");
}


/*first,absorb char*/
/*second,goto can achieve switch in diffrent modules */
/*third,flag can achieve circulation some function */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -