单链表多成员的插入、删除、查找、打印

来源:互联网 发布:java 服务器监控 编辑:程序博客网 时间:2024/05/17 16:14
#include<stdio.h>
#include<windows.h>//exit,system等的头文件
#include<string.h>//char的头文件
void menu();
//功能定义一个结构体并把结构体重命名为stud
typedef struct student
{
int num;
char name[10];
struct student *next;
}stud;
//功能创建一个链表,并赋值!
stud *creat()
{
int n;//表示链表长度(节点数)
int i=0;
stud *h,*p,*s;//结构体类型的指针变量
puts("pleasa input Chain length of the table:");
scanf("%d",&n);
if((h=(stud *)malloc(sizeof(stud)))==NULL)//初始化并判空
{
printf("no space!");
exit (0);
}
h->next=NULL;//头指针指向空
p=h;//游动的指针p指向h!
for(i=0;i<n;i++)//循环向链表输入数据
{//开辟空间用结构体指针s来接收数据
if((s=(stud *)malloc(sizeof(stud)))==NULL)
{
puts("no space!");
}
printf("\nplease input %d student's information:",i+1);
scanf("%d%s",&(s->num),s->name);
p->next=s;
s->next=NULL;//输入数据后指向下一个空节点
p=s;//将p指向s,相当于p每次向后移动了一个节点
}
return h;//返回头节点
}
//函数功能:查找指针内存在的数据,用num查询
void search(stud *h,int number)
{
stud *p;//定于结构体类型的指针变量
p=h->next;//初始化p指向头节点;
puts("\nprintf search information of the search");

while(p!=NULL)//当p不等于空的时候执行
{

if(p->num==number)//判断链表中是否存你所查询的学号
{
printf("%d\t%s",p->num,p->name);//输出你所查询学生的所用信息
}
p=p->next;//如果不等,则p指向下一节点,知道找到为止
}

}
//函数功能:求链表长度
int Length(stud *h)//求链表长度
{
stud *p;
int count=0;
p=h->next;
while(p!=NULL)
{
count++;
}
//printf("%d",count);
return count;
}
//函数功能:插入数据
stud *Insert(stud *h)
{
char InsertName[10];//表示插入的名字
int n,j=0,InsertNum;//n表示要插入的位置InsertNum表示要插入的学号
stud *p,*q;
p=h;//初始化p
puts("please input position of the insert、student's num and name\n");
scanf("%d%d%s",&n,&InsertNum,InsertName);
while(p && j<n-1)//寻找要插入信息的位置
{
p=p->next;
j++;
}
q=(stud *)malloc(sizeof(stud));//为q开辟空间
q->num=InsertNum;//读入数据
strcpy(q->name,InsertName);
q->next=p->next;
p->next=q;
q=q->next;
return h;
}
//函数功能:删除链表中的某个学生的信息
void Del(stud *h)
{
stud *p,*q;
int n,len,j=0;
puts("please students's num of your delete:");
scanf("%d",&n);
p=q=h;
while(p && j<n-1)//寻找要删除信息的位置
{
p=p->next;
++j;
}
q=p->next;//q为要插入数据的指针,将p指向要插入的地址q
//插入数据后p,p->next指向q的下一个地址,将原数据连接上
p->next=q->next;
}
//函数功能:打印出链表中所有数据
void display(stud *h)
{
stud *p;
p=h->next;
puts("\nprint Chain length of the table!");
while(p!=NULL)
{

printf("%d\t%s",p->num,p->name);
p=p->next; 
printf("\n");

}
}
void menu(void)
{
system("cls");
printf("|==========================================\n");
printf("|   student's information list             |\n");
printf("|==========================================\n");
printf("|   [1] creat linklist                     |\n");
printf("|   [2]  search                            |\n");
printf("|   [3]  insert                            |\n");
printf("|   [4]  delete                            |\n");
printf("|   [5]  printf                            |\n");
printf("|   [6]  exit                              |\n");
printf("|==========================================|\n");
printf("please input your choose(1-6)\n");


}
void main()
{
stud *h=NULL;
int choose,num;
while(1)
{
menu();
scanf("%d",&choose);
switch(choose)
{
case 1:
system("cls");//清屏函数
h=creat();
puts("\nLinklist created successfully!");
system("pause");
getchar();
break;
case 2:
system("cls");
printf("Input the student's num which you want to find!\n");
scanf("%d",&num);
search(h,num);
puts("\npress any key to return...");
getchar();
getchar();
break;
case 3:
system("cls");
Insert(h);
display(h);
puts("\nPress any key to return...");
getchar();
getchar();
break;
case 4:
system("cls");
display(h);
  Del(h);
puts("\nDelete successfully! press any key to return...");
getchar();
getchar();
break;
case 5:
display(h);
puts("\nPress any key to return...");
getchar();
getchar();
break;
case 6:
exit(0);
break;
default:
system("cls");
puts("Illegal letter! Press any key to return...");
menu();
getchar();
}

}
}
0 0
原创粉丝点击