当前位置:网站首页>C语言:链表
C语言:链表
2022-08-02 23:30:00 【白的夜gxw】
链表的创建,插入,删除,查询,更改
链表创建
//创建链表
Node * CreateList()
{
//需要一个头指针---指向头节点
Node *head=NULL;
Node *prev,*pCurrent;
//开辟内存
prev=pCurrent=(Node*)malloc(sizeof(Node));
//判断申请是否成功
if(prev==NULL){
puts("fail");
exit(-1);
}
printf("请输入第%d个学生的姓名:",count+1);
scanf("%s",pCurrent->stu_name);
printf("请输入第%d个学生的分数:",count+1);
scanf("%f",&(pCurrent->stu_score));
/*此时,pCurrent指向第一个结构*/
while(strcmp(pCurrent->stu_name,"结束"))
{
count++;
//如果是头结点
if(count==1){
//让head也指向头节点
head=pCurrent;
}
//如果是普通节点
else{
//让前后节点连接起来
prev->next=pCurrent;
}
prev=pCurrent;
//开辟内存
pCurrent=(Node*)malloc(sizeof(Node));
printf("请输入第%d个学生的姓名:",count+1);
scanf("%s",pCurrent->stu_name);
printf("请输入第%d个学生的分数:",count+1);
scanf("%f",&(pCurrent->stu_score));
}
prev->next=NULL;
printf("\nlist Create success\n");
//返回头节点
return head;
}
链表的删除
//删除链表
Node * DeleteList(Node *head)
{
Node *prev,*pCurrent;
prev=pCurrent=(Node*)malloc(sizeof(Node));
char *name;
name=(char *)malloc(20*sizeof(char));
printf("请输入你要删除的名字:");
scanf("%s",name);
//判断是否是空链表
if(head==NULL)
{
puts("This is a NULL list\n");
return NULL;
}
else{
pCurrent=head;
while(strcmp(pCurrent->stu_name,name) && pCurrent->next)
{
prev=pCurrent;
pCurrent=pCurrent->next;
}
if(strcmp(pCurrent->stu_name,name)==0)
{
//判断是否删除的元素是头节点
if(pCurrent==head){
head=pCurrent->next;
free(pCurrent);
}
//删除的元素是普通节点
else{
prev->next=pCurrent->next;
}
}
else{
puts("NO Found need delete node\n");
}
printf("\nName:%s delete success\n",name);
count=count-1;
}
//释放内存
free(name);
return head;
}
链表的插入
中间插
//插入链表---中间插
Node * InserList(Node * head)
{
Node * target;
Node *prev,*pCurrent;
//开辟内存
target=(Node *)malloc(sizeof(Node));
pCurrent=head;
printf("请输入你要插入的姓名:");
scanf("%s",target->stu_name);
printf("请输入你要插入的分数:");
scanf("%f",&(target->stu_score));
//判断是否是空链表
if(head==NULL)
{
//目标节点就是头结点
head=target;
target->next=NULL;
}
else{
//不是空链表
while((target->stu_score > pCurrent->stu_score) && pCurrent->next)
{
prev=pCurrent;
pCurrent=pCurrent->next;
}
if(target->stu_score < pCurrent->stu_score)
{
if(pCurrent==head){
head=target;
}
else{
prev->next=target;
}
target->next=pCurrent;
}
//没有找到符合的条件
else{
pCurrent->next=target;
target->next=NULL;
}
}
printf("\nName:%s inser success\n",target->stu_name);
count=count+1;
return head;
}
尾插法
//插入链表--尾插法
Node * InserList(Node *head)
{
Node *pCurrent;
Node *target;
//开辟内存
target=(Node *)malloc(sizeof(Node));
pCurrent=head;
printf("请输入你要插入的姓名:");
scanf("%s",target->stu_name);
printf("请输入你要插入的分数:");
scanf("%f",&(target->stu_score));
//判断是否是空链表
if(head==NULL)
{
//目标节点就是头结点
head=target;
target->next=NULL;
}
else{
while(pCurrent->next)
{
pCurrent=pCurrent->next;
}
pCurrent->next=target;
target->next=NULL;
}
count=count+1;
return head;
}
头插法
//插入链表--头插法
Node * InserList(Node *head)
{
Node *pCurrent;
Node *target;
//开辟内存
target=(Node *)malloc(sizeof(Node));
pCurrent=head;
printf("请输入你要插入的姓名:");
scanf("%s",target->stu_name);
printf("请输入你要插入的分数:");
scanf("%f",&(target->stu_score));
//判断是否是空链表
if(head==NULL)
{
//目标节点就是头结点
head=target;
target->next=NULL;
}
else{
head=target;
target->next=pCurrent;
pCurrent=head;
}
count=count+1;
return head;
}
更改链表
//更改链表
Node * ChangeList(Node *head)
{
Node *pCurrent,*target;
//开辟内存
target=(Node *)malloc(sizeof(Node));
pCurrent=head;
printf("请输入你要更改的姓名:");
scanf("%s",target->stu_name);
//判断是否是空链表
if(head==NULL)
{
puts("error\n");
return NULL;
}
else{
while(strcmp(target->stu_name,pCurrent->stu_name) && pCurrent->next)
{
pCurrent=pCurrent->next;
}
if(strcmp(target->stu_name,pCurrent->stu_name)==0)
{
printf("你要更改的分数是:");
scanf("%f",&(pCurrent->stu_score));
}
else{
printf("查无此人,请重新输入!\n");
}
}
return head;
}
打印链表
//打印链表
void PrintList(Node *head)
{
//拷贝头指针
Node * temphead=head;
//判断是否是空链表
if(head)
{
puts("=======================================");
printf("一共打印%d个学生的信息\n",count);
//判断是否有链接
while(temphead)
{
printf("%s\t%.1f\n",temphead->stu_name,temphead->stu_score);
temphead=temphead->next;
}
puts("=======================================");
}
//空表
else{
puts("This is a NULL list\n");
}
}
释放链表
//释放链表
void FreeLinkList(Node*head)
{
Node * pCurrent;
pCurrent=head;
while(pCurrent)
{
head=pCurrent->next;
free(pCurrent);
pCurrent=head;
}
}
完整代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
//定义学生的结构体
typedef struct student {
char stu_name[20];
float stu_score;
struct student * next;
}Node;
//存放学生个数
int count=0;
Node * CreateList(); //创建链表
void PrintList(Node *head); //打印链表
Node * DeleteList(Node *head); //删除节点
Node * InserList(Node * head); //插入节点
void FreeLinkList(Node*head); //释放链表
Node * ChangeList(Node *head); //更改节点
//创建链表
Node * CreateList()
{
//需要一个头指针---指向头节点
Node *head=NULL;
Node *prev,*pCurrent;
//开辟内存
prev=pCurrent=(Node*)malloc(sizeof(Node));
//判断申请是否成功
if(prev==NULL){
puts("fail");
exit(-1);
}
printf("请输入第%d个学生的姓名:",count+1);
scanf("%s",pCurrent->stu_name);
printf("请输入第%d个学生的分数:",count+1);
scanf("%f",&(pCurrent->stu_score));
/*此时,pCurrent指向第一个结构*/
while(strcmp(pCurrent->stu_name,"结束"))
{
count++;
//如果是头结点
if(count==1){
//让head也指向头节点
head=pCurrent;
}
//如果是普通节点
else{
//让前后节点连接起来
prev->next=pCurrent;
}
prev=pCurrent;
//开辟内存
pCurrent=(Node*)malloc(sizeof(Node));
printf("请输入第%d个学生的姓名:",count+1);
scanf("%s",pCurrent->stu_name);
printf("请输入第%d个学生的分数:",count+1);
scanf("%f",&(pCurrent->stu_score));
}
prev->next=NULL;
printf("\nlist Create success\n");
//返回头节点
return head;
}
//删除链表
Node * DeleteList(Node *head)
{
Node *prev,*pCurrent;
prev=pCurrent=(Node*)malloc(sizeof(Node));
char *name;
name=(char *)malloc(20*sizeof(char));
printf("请输入你要删除的名字:");
scanf("%s",name);
//判断是否是空链表
if(head==NULL)
{
puts("This is a NULL list\n");
return NULL;
}
else{
pCurrent=head;
while(strcmp(pCurrent->stu_name,name) && pCurrent->next)
{
prev=pCurrent;
pCurrent=pCurrent->next;
}
if(strcmp(pCurrent->stu_name,name)==0)
{
//判断是否删除的元素是头节点
if(pCurrent==head){
head=pCurrent->next;
free(pCurrent);
}
//删除的元素是普通节点
else{
prev->next=pCurrent->next;
}
}
else{
puts("NO Found need delete node\n");
}
printf("\nName:%s delete success\n",name);
count=count-1;
}
//释放内存
free(name);
return head;
}
//插入链表---中间插
Node * InserList(Node * head)
{
Node * target;
Node *prev,*pCurrent;
//开辟内存
target=(Node *)malloc(sizeof(Node));
pCurrent=head;
printf("请输入你要插入的姓名:");
scanf("%s",target->stu_name);
printf("请输入你要插入的分数:");
scanf("%f",&(target->stu_score));
//判断是否是空链表
if(head==NULL)
{
//目标节点就是头结点
head=target;
target->next=NULL;
}
else{
//不是空链表
while((target->stu_score > pCurrent->stu_score) && pCurrent->next)
{
prev=pCurrent;
pCurrent=pCurrent->next;
}
if(target->stu_score < pCurrent->stu_score)
{
if(pCurrent==head){
head=target;
}
else{
prev->next=target;
}
target->next=pCurrent;
}
//没有找到符合的条件
else{
pCurrent->next=target;
target->next=NULL;
}
}
printf("\nName:%s inser success\n",target->stu_name);
count=count+1;
return head;
}
/* //插入链表--尾插法 Node * InserList(Node *head) { Node *pCurrent; Node *target; //开辟内存 target=(Node *)malloc(sizeof(Node)); pCurrent=head; printf("请输入你要插入的姓名:"); scanf("%s",target->stu_name); printf("请输入你要插入的分数:"); scanf("%f",&(target->stu_score)); //判断是否是空链表 if(head==NULL) { //目标节点就是头结点 head=target; target->next=NULL; } else{ while(pCurrent->next) { pCurrent=pCurrent->next; } pCurrent->next=target; target->next=NULL; } count=count+1; return head; }*/
/* //插入链表--头插法 Node * InserList(Node *head) { Node *pCurrent; Node *target; //开辟内存 target=(Node *)malloc(sizeof(Node)); pCurrent=head; printf("请输入你要插入的姓名:"); scanf("%s",target->stu_name); printf("请输入你要插入的分数:"); scanf("%f",&(target->stu_score)); //判断是否是空链表 if(head==NULL) { //目标节点就是头结点 head=target; target->next=NULL; } else{ head=target; target->next=pCurrent; pCurrent=head; } count=count+1; return head; }*/
//更改链表
Node * ChangeList(Node *head)
{
Node *pCurrent,*target;
//开辟内存
target=(Node *)malloc(sizeof(Node));
pCurrent=head;
printf("请输入你要更改的姓名:");
scanf("%s",target->stu_name);
//判断是否是空链表
if(head==NULL)
{
puts("error\n");
return NULL;
}
else{
while(strcmp(target->stu_name,pCurrent->stu_name) && pCurrent->next)
{
pCurrent=pCurrent->next;
}
if(strcmp(target->stu_name,pCurrent->stu_name)==0)
{
printf("你要更改的分数是:");
scanf("%f",&(pCurrent->stu_score));
}
else{
printf("查无此人,请重新输入!\n");
}
}
return head;
}
//打印链表
void PrintList(Node *head)
{
//拷贝头指针
Node * temphead=head;
//判断是否是空链表
if(head)
{
puts("=======================================");
printf("一共打印%d个学生的信息\n",count);
//判断是否有链接
while(temphead)
{
printf("%s\t%.1f\n",temphead->stu_name,temphead->stu_score);
temphead=temphead->next;
}
puts("=======================================");
}
//空表
else{
puts("This is a NULL list\n");
}
}
//释放链表
void FreeLinkList(Node*head)
{
Node * pCurrent;
pCurrent=head;
while(pCurrent)
{
head=pCurrent->next;
free(pCurrent);
pCurrent=head;
}
}
int main()
{
Node *stu;
int num;
do
{
printf("1:创建链表\t2:删除节点\n3:插入节点\t4:更改链表\n5:退出\n");
printf("请输入:");
scanf("%d",&num);
switch (num)
{
case 1://创建链表
stu=CreateList();
PrintList(stu);
break;
case 2: //删除节点
stu=DeleteList(stu);
PrintList(stu);
break;
case 3: //插入节点
stu=InserList(stu);
PrintList(stu);
break;
case 4://更改链表
stu=ChangeList(stu);
PrintList(stu);
break;
case 5://退出
break;
}
}while(num!=5);
FreeLinkList(stu);
system("pause");
return 0;
}
边栏推荐
- 微信小程序实现lot开发09 接入微信登录
- js基础知识整理之 —— Math
- C# 异步编程(async和await)
- 程序员英语自我介绍
- HCIP(17)
- CKAN教程之将 Snowflake 连接到 CKAN 以发布到开放数据门户
- 解决错误:Optional int parameter ‘pageSize‘ is present but cannot be translated into a null value due to
- 十年架构五年生活-05第一次出差
- Teach you to locate online MySQL slow query problem hand by hand, package teaching package meeting
- 5、Citrix云桌面初始化Storefront设置
猜你喜欢
MySQL最大建议行数2000w, 靠谱吗?
合并两个excel表格工具
浅谈I2C知识
Cholesterol-PEG-Acid,胆固醇-聚乙二醇-羧基保持在干燥、低温环境下
如何使用vlookup+excel数组公式 完成逆向查找?
科研用Cholesterol-PEG-NHS,NHS-PEG-CLS,胆固醇-聚乙二醇-活性酯
fifa将采用半自动越位技术计算进球
Cholesterol-PEG-Amine,CLS-PEG-NH2,胆固醇-聚乙二醇-氨基脂两亲性脂质衍生物
脂溶性胆固醇-聚乙二醇-叠氮,Cholesterol-PEG-Azide,CLS-PEG-N3
mPEG-Cholesterol,mPEG-CLS,甲氧基-聚乙二醇-胆固醇可用于脂质体制备
随机推荐
flutter 每个要注意的点
MySQL的多表查询(1)
Cholesterol-PEG-Amine,CLS-PEG-NH2,胆固醇-聚乙二醇-氨基脂两亲性脂质衍生物
dataBinding的import导入
基于STM32设计的老人防摔倒报警设备(OneNet)
DownMusic summary record
Heartwarming AI Review (1)
为了面试阿里,熬夜肝完这份软件测试笔记后,Offer终于到手了
@GetMapping、@PostMapping、@PutMapping、@DeleteMapping的区别
记一次mysql查询慢的优化历程
程序员如何优雅地解决线上问题?
Rasa 3.x study series - Rasa - Issues 4792 socket debug logs clog up debug feed study notes
Test | ali internship 90 days in life: from the perspective of interns, talk about personal growth
Day117.尚医通:生成挂号订单模块
Connect the Snowflake of CKAN tutorial CKAN to release to open data portal
机器学习-特征映射方法
Pytest配置项-pytest.ini
精心整理16条MySQL使用规范,减少80%问题,推荐分享给团队
年近30 ,4月无情被辞,想给划水的兄弟提个醒...
HCIP(17)