当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

科研用Cholesterol-PEG-NHS,NHS-PEG-CLS,胆固醇-聚乙二醇-活性酯

What is the matter that programmers often say "the left hand is knuckled and the right hand is hot"?

程序员如何优雅地解决线上问题?

Rebound shell principle and implementation

Moco of Mock tools use tutorial

我们来浅谈代码语言的魅力

用了TCP协议,就一定不会丢包吗?

Heartwarming AI Review (1)

CKAN教程之将 Snowflake 连接到 CKAN 以发布到开放数据门户

程序员的七夕浪漫时刻
随机推荐
Test | ali internship 90 days in life: from the perspective of interns, talk about personal growth
resubmit 渐进式防重复提交框架简介
【QT】自定义工程封装成DLL并如何调用(带ui界面的)
Apache Doris 1.1 特性揭秘:Flink 实时写入如何兼顾高吞吐和低延时
Controller层代码这么写,简洁又优雅!
HCIP(16)
The CTF command execution subject their thinking
Week 7 - Distributional Representations
微信小程序(一)
漫画:怎么证明sleep不释放锁,而wait释放锁?
淘宝商品销量接口/淘宝商品销量监控接口/商品累计销量接口代码对接分享
airflow db init 报错
The latest real software test interview questions are shared. Are you afraid that you will not be able to enter the big factory after collecting them?
记一次mysql查询慢的优化历程
九零后程序员心声:互联网的同行们,别卷了,再卷人都卷没了
【斯坦福计网CS144项目】Lab5: NetworkInterface
IDEA多线程调试
flutter空安全问题,平时用到的数据一定要注意
Rasa 3.x 学习系列- Rasa - Issues 4792 socket debug logs clog up debug feed学习笔记
几种常见的跨域解决方法