当前位置:网站首页>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;
}
边栏推荐
- 【代码扫描修复】MD5加密弱HASH漏洞
- 2022 China Eye Expo, Shandong Eye Health Exhibition, Vision Correction Instrument Exhibition, Eye Care Products Exhibition
- 2022山东国际青少年眼睛健康产业展会,视力健康展,眼视光展
- 服务间歇性停顿问题优化|得物技术
- 主流定时任务解决方案全横评
- 2022杭电多校第一场(K/L/B/C)
- Jmeter二次开发实现rsa加密
- 十年架构五年生活-03作为技术组长的困扰
- MySQL的多表查询(1)
- Rasa 3.x 学习系列- Rasa - Issues 4792 socket debug logs clog up debug feed学习笔记
猜你喜欢
解决错误:Optional int parameter ‘pageSize‘ is present but cannot be translated into a null value due to
心电记录电路设计(框图/波形以及信号放大器的选择)
NLP常用Backbone模型小抄(1)
markdown语法
【问题征集】向 iPod 之父、iPhone 联合设计者、Google Nest 创始人 Tony Fadell 提问啦
CKAN教程之在 AWS 上部署 CKAN 应用程序
2022杭电多校第一场(K/L/B/C)
js基础知识整理之 —— Math
程序员如何优雅地解决线上问题?
智能电视竞争白热化,利用小程序共建生态突围
随机推荐
flutter空安全问题,平时用到的数据一定要注意
Let's talk about the charm of code language
Week 7 - Distributional Representations
flutter 时间戳转日期
如何突破测试/开发程序员思维?一种不一样的感觉......
令人心动的AI综述(1)
同一份数据,Redis为什么要存两次?
新公链时代的跨链安全性解决方案
DataGuard日常维护常见问题之数据同步异常
HCIP(16)
Merge two excel spreadsheet tools
【系统架构设计师】第三章 数据库系统
1 - vector R language self-study
nmap: Bad CPU type in executable
Connect the Snowflake of CKAN tutorial CKAN to release to open data portal
Strict feedback nonlinear systems based on event trigger preset since the immunity of finite time tracking control
Swift中的类型相关内容
Day117.尚医通:生成挂号订单模块
vant-swipe adaptive picture height + picture preview
airflow db init 报错