#include<iostream>
using namespace std;
//创建学生结构变量
struct Student
{
long long Id;
char Name[20];
int chineseScore;
int mathScore;
int englishScore;
};
//结点结构体
struct Node
{
Student data;
struct Node* next;
};
//创建头结点
struct Node* create_headNode()
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
headNode->next = NULL;
return headNode;
}
//创建新结点
struct Node* create_newNode(struct Student data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
//结点个数
int sizeNode(struct Node* list)
{
struct Node* posNode = list->next;
int size = 0;
while (1)
{
if (posNode == NULL)
{
break;
}
size++;
posNode = posNode->next;
}
return size;
}
//在链表头部增加结点
void InsertNewNodeByHead(struct Node* headNode, Student data)
{
struct Node* newNode = create_newNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
//打印
void printNode(struct Node* headNode)
{
if (headNode->next == NULL)
{
cout << "无法打印,链表为空" << endl;
return;
}
else
{
struct Node* pMove = headNode->next;
while (pMove)
{
cout << "学号:" << pMove->data.Id << "\t" << "姓名:" << pMove->data.Name << "\t" << "语文成绩:" << pMove->data.chineseScore << "\t" << "数学成绩:" << pMove->data.mathScore << "\t"
<< "英语成绩:" << pMove->data.englishScore << endl;
pMove = pMove->next;
}
return;
}
}
//清空链表
void freeNode(struct Node* headNode)
{
//struct Node* posNode = headNode->next;//目标结点
//while (posNode != NULL)
//{
// struct Node* posNext = posNode->next;
// free(posNode);
// posNode = posNext;
//}
//free(headNode);
if (headNode->next == NULL)
{
cout << "链表已为空" << endl;
return;
}
while (headNode->next)
{
struct Node* posNode = headNode;
struct Node* posNext = posNode->next;
posNode->next = posNext->next;
free(posNext);
}
cout << "已经清空链表" << endl;