当前位置:网站首页>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;
}
边栏推荐
- matplotlib中的3D绘图警告解决:MatplotlibDeprecationWarning: Axes3D(fig) adding itself to the figure
- MySQL的多表查询(1)
- 如何突破测试/开发程序员思维?一种不一样的感觉......
- Rasa 3.x 学习系列- Rasa - Issues 4792 socket debug logs clog up debug feed学习笔记
- flutter空安全问题,平时用到的数据一定要注意
- 公司招个程序员,34岁以上两年一跳的不要,开出工资以为看错了
- d实验新异常
- What is the matter that programmers often say "the left hand is knuckled and the right hand is hot"?
- 数据库审计 - 网络安全的重要组成部分
- WAF WebShell Trojan free to kill
猜你喜欢
随机推荐
基于STM32设计的老人防摔倒报警设备(OneNet)
如何使用vlookup+excel数组公式 完成逆向查找?
markdown语法
我们来浅谈代码语言的魅力
js基础知识整理之 —— 判断语句和三元运算符
简单聊聊MySQL中的六种日志
Visual Studio中vim模拟器
程序员常说的“左手锟斤拷,右手烫烫烫”是怎么回事?
CKAN教程之将 Snowflake 连接到 CKAN 以发布到开放数据门户
【QT】自定义工程封装成DLL并如何调用(带ui界面的)
No code development platform data ID introductory tutorial
VMware workstation program starts slowly
脂溶性胆固醇-聚乙二醇-叠氮,Cholesterol-PEG-Azide,CLS-PEG-N3
厌倦了安装数据库?改用 Docker
HVV红队 | 渗透测试思路整理
如何快速对接淘宝开放平台API接口(淘宝店铺订单明文接口,淘宝店铺商品上传接口,淘宝店铺订单交易接口)
What is the matter that programmers often say "the left hand is knuckled and the right hand is hot"?
Test | ali internship 90 days in life: from the perspective of interns, talk about personal growth
Cholesterol-PEG-Amine,CLS-PEG-NH2,胆固醇-聚乙二醇-氨基脂两亲性脂质衍生物
D experimental new anomaly









