最简单的C链表应用-------学生成绩管理
记录自己的学习,也希望能对大家的学习有帮助!
在没有学习链表之前我们定义数组来实现对数据的存储,但是数组通常有很多的弊端。
1.无法动态申请内存,数组的长度固定,定义后不能更改;
2.数组存储的数据类型固定,是浮点型就只能存浮点型;
3.对数组内数据的增删改需要大量的移动数组元素,感觉很麻烦;
而链表则可以动态的申请内存,并且以可以存储五花八们各种变量,同时增删改也变得简单了。
接下来看一下如何写一个简单的链表。
我们把功能模块化,我来介绍每个模块
1.首先定义结构体;
#include <stdio.h>
#include <stdlib.h>
struct student
{
char name[20];
int num;
int math;
};
struct Node
{
struct student data; //定义数据域
struct Node*next; //定义指针域,存储直接后继的结点信息
};
students用来记录学生的信息,而Node定义及确定结点的后继信息。
2.创建链表
struct Node* createList()
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));//创建头结点信息,实现内存的动态申请
headNode->next =NULL; //头结点的指向为空
return headNode; //返回头结点信息
};
3.创建结点
struct Node* createNode(struct student data)
{
struct Node* newNode =(struct Node*)malloc(sizeof(struct Node));//创建新结点,完成内存的动态申请
newNode->data=data;//对新结点进行信息存储
newNode->next=NULL;//确定新结点的后继信息
return newNode; //返回新结点
};
4.定义打印结点函数
void printList(struct Node* headNode)
{
struct Node* pMove=headNode->next;//定义pMove读取结点信息
printf("name\tnum\tmath\n");
while(pMove)
{
printf("%s\t%d\t%d\n",pMove->data.name,pMove->data.num,pMove->data.math);
pMove =pMove->next;//后移pMove的指向
}
printf("\n");
}
5.插入节点
void insertNodeByHead(struct Node* headNode,struct student data)
{
struct Node* newNode=createNode(data);//调用函数创建结点
newNode->next=headNode->next;
headNode->next=newNode;//将新结点插入到头结点和头结点的后继结点之间
}
原理为
建立后的链表如图;
插入新结点后
如此完成新结点在头结点和后继结点间的插入
6.定义删除结点函数
void deleteNodeByAppoinNum(struct Node* headNode,int num)
{
struct Node* posNode = headNode->next;
struct Node* posNodeFront = headNode;
if(posNode==NULL)
printf("链表为空");
else
{
while(posNode->data.num!=num)
{
posNodeFront=posNode;
posNode=posNodeFront->next;//遍历链表查找
if(posNode==NULL)
{
printf("没找到");
return;
}
}
}
posNodeFront->next=posNode->next;//使删除的结点的前一个结点指向后一个结点
free(posNode);//释放要删除结点的内存
}
该删除结点的方法为指定位置删除,也是最常用的删除方法。头删除法、伟删除法同理,只需改变条件。重新调整各个结点之间的链接
7.主函数部分
void clear() //定义函数用来清楚缓冲区,防止跳步
{
int ch;
while(ch=getchar()!='\n' && ch!=EOF);
}
int main()
{
struct Node*list =createList(); //创建链表
struct student info;//定义临时结构体变量,临时存储输入的信息
while(1)
{
printf("请输入姓名 学号 数学\n");
scanf("%s%d%d",&info.name,&info.num,&info.math);
clear();
insertNodeByHead(list,info);//将输入的信息插入链表
printf("continue(y/n)?");
int choice =getchar();
clear();
if(choice=='N'||choice=='n')
break;
}
printList(list);//打印链表
printf("请输入要删除的序号");
scanf("%d",&info.num);
clear();
deleteNodeByAppoinNum(list,info.num);//删除指定节点
printList(list);
system("pause");//暂停程序防止代码闪过
return 0;
}
完整代码如下
#include <stdio.h>
#include <stdlib.h>
struct student
{
char name[20];
int num;
int math;
};
struct Node
{
struct student data;
struct Node*next;
};
struct Node* createList()
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
headNode->next =NULL;
return headNode;
};
struct Node* createNode(struct student data)
{
struct Node* newNode =(struct Node*)malloc(sizeof(struct Node));
newNode->data=data;
newNode->next=NULL;
return newNode;
};
void printList(struct Node* headNode)
{
struct Node* pMove=headNode->next;
printf("name\tnum\tmath\n");
while(pMove)
{
printf("%s\t%d\t%d\n",pMove->data.name,pMove->data.num,pMove->data.math);
pMove =pMove->next;
}
printf("\n");
}
void insertNodeByHead(struct Node* headNode,struct student data)
{
struct Node* newNode=createNode(data);
newNode->next=headNode->next;
headNode->next=newNode;
}
void deleteNodeByAppoinNum(struct Node* headNode,int num)
{
struct Node* posNode = headNode->next;
struct Node* posNodeFront = headNode;
if(posNode==NULL)
printf("链表为空");
else
{
while(posNode->data.num!=num)
{
posNodeFront=posNode;
posNode=posNodeFront->next;
if(posNode==NULL)
{
printf("没找到");
return;
}
}
}
posNodeFront->next=posNode->next;
free(posNode);
}
void clear()
{
int ch;
while(ch=getchar()!='\n' && ch!=EOF);
}
int main()
{
struct Node*list =createList();
struct student info;
while(1)
{
printf("请输入姓名 学号 数学\n");
scanf("%s%d%d",&info.name,&info.num,&info.math);
clear();
insertNodeByHead(list,info);
printf("continue(y/n)?");
int choice =getchar();
clear();
if(choice=='N'||choice=='n')
break;
}
printList(list);
printf("请输入要删除的序号");
scanf("%d",&info.num);
clear();
deleteNodeByAppoinNum(list,info.num);
printList(list);
system("pause");
return 0;
}