单链表多成员的插入、删除、查找、打印
来源:互联网 发布: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();
}
}
}
#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
- 单链表多成员的插入、删除、查找、打印
- 单链表的游标实现:插入、删除、查找、打印等功能
- 二叉排序树的查找、删除、插入
- BST的查找、插入、删除
- 单链表的查找、删除、插入!
- 哈希表的插入、查找、删除
- 二叉排序树的插入,查找,删除
- 单链表的插入,查找,删除
- 二叉排序树的查找-插入-删除
- 汇编实现的简单的通讯录系统,有插入,修改,删除,打印,查找功能
- 二叉查找树的插入,删除,查找
- 二叉查找树的插入,删除,查找
- 二叉查找树的插入,删除,查找
- 散列文件的插入、删除、查找和打印(C语言源代码)
- 散列文件的插入、删除、查找和打印(论文)
- 线性表的顺序存储格式基本操作:初始化、插入、删除、查找、打印
- 单链表的创建,表长,插入,查找,逆置,中间元素,删除节点,打印
- 单链表的测长和打印和节点查找、插入、删除、逆置
- 常见HTTP状态(304,503)
- Python学习笔记 函数
- 解决 越狱后ios 密码丢失的问题
- Josephus约瑟环问题
- IOI2014题解
- 单链表多成员的插入、删除、查找、打印
- 下面三个导航栏Item有什么区别啊???
- 快速部署安装rrdtool
- CentOS 7上安装翻译软件
- 黑马程序员-OC-自定义构造方法和description方法
- iOS开发网络篇—JSON介绍
- 我的数值计算第一天。
- SWFUpload综合运用。
- Java类生命周期详解--转载