C语言课程设计——链表的相关操作
大家好我是初学c语言的菜鸟,同时也是一个想和大家一起变强和一起进步的人。
大一新生因为疫情所以c语言没有学完,到了指针及以后内容基本就是自学的,为创作这一次课程设计所谓是呕心沥血,总算千遍万遍的学习下还是写出了点东西。
这次设计有以下几个操作:
1:创建链表(创建一个表头表示整个链表)
2:创建结点
3:插入结点(头插入)
4:删除结点
5:搜素链表信息
6:打印遍历链表
7:释放链表内存
以下是对单链表进行各种操作的函数
struct Node* createlist(); //创建链表函数,将结点指针变成结构体变量,能够存储数据
struct Node* createNode(struct student data); //创建结点函数,供插入结点时使用
void printlist(struct Node* headNode); //打印遍历链表函数,输出所有结点的数据域
void insertNode(struct Node* headNode,struct student data); //插入结点函数,插入新的信息
struct Node* search(struct Node* info,char *tar); //搜索函数,查找与用户相匹配的结点信息
void printinfo(struct Node* info); //打印查找到的结点
void deleteNode(struct Node* headNode,int num); //删除结点函数
void releaselist(struct Node** list); //释放链表中所有结点占用的内存
在这特别强调在使用malloc()函数动态内存后,在程序结束时一定要释放内存,以下是我对链表占用内存释放的代码。
void releaselist(struct Node** list) //释放每一个结点的内存
{
struct Node* temp;
while(*list !=NULL)
{
temp=*list;
*list=(*list)->next;
free(temp);
}
}
因为时间关系我就没有录入太多信息,因为我选的题目是实现链表的一些操作就行。
贴出源代码:
struct student //学生信息
{
char name[100];
int num;
float math;
float yuwen;
float yingyu;
};
struct Node
{
struct student data;
//数据域
struct Node* next;
//指针域
};
struct Node* createlist(); //创建链表函数,将结点指针变成结构体变量,能够存储数据
struct Node* createNode(struct student data); //创建结点函数,供插入结点时使用
void printlist(struct Node* headNode); //打印遍历链表函数,输出所有结点的数据域
void insertNode(struct Node* headNode,struct student data); //插入结点函数,插入新的信息
struct Node* search(struct Node* info,char *tar); //搜索函数,查找与用户相匹配的结点信息
void printinfo(struct Node* info); //打印查找到的结点
void deleteNode(struct Node* headNode,int num); //删除结点函数
void releaselist(struct Node** list);
struct Node* createlist() //创建链表
{
struct Node* headNode=(struct Node*)malloc(sizeof(struct Node));
//headNode 成为了结构体变量
if(headNode==NULL)
{
printf("内存分配失败了\n");
exit(1);
}
//变量使用前必须被初始化
headNode->next=NULL;
return headNode;
}
struct Node* createNode(struct student data) //创建结点,插入结点时调用
{
struct Node* newNode;
newNode=(struct Node*)malloc(sizeof(struct Node));
if(newNode==NULL)
{
printf("内存分配失败了\n");
exit(1);
}
newNode->data=data;
newNode->next=NULL;
return newNode;
}
void printlist(struct Node* headNode) //打印链表
{
struct Node* pmove=headNode->next;
printf("名字\t学号\t数学\t语文\t英语\n");
while(pmove!=NULL)
{
printf("%s\t",pmove->data.name);
printf("%d\t",pmove->data.num);
printf("%.1f\t",pmove->data.math);
printf("%.1f\t",pmove->data.yuwen);
printf("%.1f\n",pmove->data.yingyu);
pmove=pmove->next;
}
printf("\n");
}
void insertNode(struct Node* headNode,struct student data)
{
//创建插入的结点
struct Node*newNode=createNode(data);
newNode->next=headNode->next;
headNode->next=newNode;
}
struct Node* search(struct Node* info,char *tar) //通过姓名来搜索信息的函数
{
while(info!=NULL)
{
if(!strcmp(info->data.name,tar))
{
break;
}
info=info->next;
}
return info;
}
void printinfo(struct Node* info) //打印查找到的信息
{
printf("名字\t学号\t数学\t语文\t英语\n");
printf("%s\t",info->data.name);
printf("%d\t",info->data.num);
printf("%.1f\t",info->data.math);
printf("%.1f\t",info->data.yuwen);
printf("%.1f\n",info->data.yingyu);
}
void deleteNode(struct Node* headNode,int num) //通过学号删除结点
{
struct Node* posNode=headNode->next;
struct Node* posNodeFront=headNode;
if(posNode==NULL)
printf("无法删除,链表为空\n");
else
{
while(posNode->data.num !=num)
{
posNodeFront=posNode;
posNode=posNodeFront->next;
if(posNode==NULL)
{
printf("没有找到相关信息,无法删除\n");
return;
}
}
posNodeFront->next=posNode->next;
//前面结点的指针指向后一个结点
free(posNode);
//删除结点
}
}
void releaselist(struct Node** list) //释放每一个结点的内存
{
struct Node* temp;
while(*list !=NULL)
{
temp=*list;
*list=(*list)->next;
free(temp);
}
}
main()
{
char choice;
char ch;
char c;
char get[100];
struct Node* info;
struct Node* list=createlist();
//创建链表并为其分配内存
struct student grade;
while(1)
{
printf("请录入学生的姓名,学号,数学语文英语成绩:");
setbuf(stdin,NULL); //清空缓冲区
scanf("%s%d%f%f%f",grade.name,&grade.num,&grade.math,&grade.yuwen,&grade.yingyu);
setbuf(stdin,NULL);
insertNode(list,grade);
printf("是否继续录入信息(Y/N)?\n");
setbuf(stdin,NULL);
choice=getchar();
while(choice!='Y'&&choice!='N'&&choice!='y'&&choice!='n')
{ setbuf(stdin,NULL);
printf("输入错误请重新输入(Y/N)?:");
choice=getchar();
}
if(choice=='Y'||choice=='y')
{
continue;
}
else
{
break;
}
}
printf("请问是否需要打印信息(Y/N):");
do
{
choice=getchar();
}while(choice!='Y'&&choice!='N'&&choice!='y'&&choice!='n');
if(choice=='Y'||choice=='y')
{
printlist(list);
}
printf("请问是否需要删除信息(Y/N):");
do
{
choice=getchar();
}while(choice!='Y'&&choice!='N'&&choice!='y'&&choice!='n');
if(choice=='Y'||choice=='y')
{
while(1)
{
printf("请输入要删除的学生的学号:");
scanf("%d",&grade.num);
deleteNode(list,grade.num);
printf("请问是否需要继续删除(Y/N)?:");
setbuf(stdin,NULL);
scanf("%c",&c);
if(c=='Y'||c=='y')
continue;
else
break;
}
printf("打印删除后的信息:\n");
printlist(list);
}
while(1)
{
printf("\n输入姓名来搜索同学信息:");
scanf("%s",get);
info=search(list,get);
if(info==NULL)
{
printf("没找到不好意思!\n");
}
else
{
do
{
printf("已找到符合条件的同学信息......\n");
printinfo(info);
}while((info=search(info->next,get))!=NULL);
//直到找到链表结尾位置才会退出循坏。
}
setbuf(stdin,NULL);
printf("按任意键继续搜索,按1结束搜素:");
scanf("%c",&ch);
setbuf(stdin,NULL);
if(ch=='1')
{
break;
}
}
releaselist(&list);
//释放内存
}