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

📄 实验三 单链表的操作建立 显示 查找 插入和删除.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 */


struct student *create(int n)
{struct student *P,*Q,*head;int l=0,m=1;
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;
return(head);
}


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,*ae;
  int n,dl,al,flag; 
/*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];

printf("enter list element number:");
scanf("%d",&n);
head=create(n);
print(head,n);


printf("\nenter name to find location of equal list element:");
scanf("%s",findname);
find(findname,head,n);
printf("\nenter value of flag(when flag is equal one,program find can repeat):");
scanf("%d",&flag);
while(flag==1)
{printf("enter name to find equal list element:");
scanf("%s",findname);
find(findname,head,n);
printf("\nenter value of flag(when flag is equal one,program find can repeat):");
scanf("%d",&flag);}

printf("\ndelete a list element in what location of the list:");
scanf("%d",&dl);
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 equal one,program dele can repeat):");
scanf("%d",&flag);
while(flag==1)
{printf("\ndelete a list element in what location of the list:");
scanf("%d",&dl);
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 equal one,program dele can repeat):");
scanf("%d",&flag);}	

printf("\nadd a list element in  what location of the list:");
scanf("%d",&al);if(al<=n+1)
{ae=(struct student*)malloc(sizeof(struct student));
printf("input adding element:");
scanf("%s",ae->name);
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 equal one,program add can repeat):");
scanf("%d",&flag);
while(flag==1)
{printf("\nadd a list element in  what location of the list:");
scanf("%d",&al);if(al<=n+1)
{ae=(struct student*)malloc(sizeof(struct student));
printf("input adding element:");
scanf("%s",ae->name);
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 equal one,program add can repeat):");
scanf("%d",&flag);}
printf("\n");
}

⌨️ 快捷键说明

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