文章目录
-
-
- 单链表
-
-
- 单链表的定义
- 定义单链表的结构体
- 初始化函数
- 添加数据
- 删除数据
- 清空内存
- 完整代码(简单版)
-
-
单链表
单链表的定义
链式存储的线性表
定义单链表的结构体
typedef struct student { int m_id; char m_name[20]; int m_score; struct student *pNext; }STU;
初始化函数
STU* Init() { STU* pNew = (STU*)malloc(sizeof(STU)); pNew->pNext = NULL; return pNew; }
添加数据
void Insert(STU* pHead, STU * pData) { STU* pNew = Init(); pNew->m_id = pData->m_id; memcpy(pNew->m_name, pData->m_name, 20); //字符串不能直接赋值 pNew->m_score = pData->m_score; //中间插入要遍历链表找到插入位置,这里只介绍头插和尾插 #if 0 //头插 pNew->pNext = pHead->pNext; //① pHead->pNext = pNew; //② #else //尾插 STU* pTemp = pHead; while (pTemp->pNext != NULL) { pTemp = pTemp->pNext; } pNew->pNext = pTemp->pNext; //① pTemp->pNext = pNew; //② #endif }
删除数据
void DeleteNode(STU* pHead, char name[]) { STU* pTemp = pHead; while (pTemp->pNext != NULL) { if (strcmp(pTemp->pNext->m_name, name) == 0) { STU* pDelete = pTemp->pNext; pTemp->pNext = pDelete->pNext; free(pDelete); break; } pTemp = pTemp->pNext; } printf("\n删除成功...\n\n"); }
清空内存
void FreeAll(STU * pHead) { STU* pTemp = pHead; while (pTemp->pNext != NULL) { STU* pDelete = pTemp->pNext; pTemp->pNext = pDelete->pNext; free(pDelete); } }
完整代码(简单版)
main文件:
//main.c #include<stdio.h> #include"linked.h" int main() { STU* pHead = Init(); char name[20]; while (1) { switch (Menu()) { case 1: //添加学生信息 { STU* pNew = Init(); printf("请输入学生ID:"); scanf("%d", &pNew->m_id); printf("请输入学生Name:"); scanf("%s", pNew->m_name); printf("请输入学生Score:"); scanf("%d", &pNew->m_score); Insert(pHead, pNew); } break; case 2: //删除学生信息 printf("请输入要删除的学生姓名:"); scanf("%s", name); DeleteNode(pHead, name); break; case 3: //修改学生信息 printf("请输入要修改的学生姓名:"); scanf("%s", name); ChangeNode(pHead, name); break; case 4: //查找学生信息 printf("请输入要查询的学生姓名:"); scanf("%s", name); FindNode(pHead, name); break; case 5: //显示学生信息 PrintList(pHead); break; case 6: //退出并删除 FreeAll(pHead); return 0; break; default: break; } } return 0; }
linked.h文件:
#pragma once #include<stdlib.h> #include<stdio.h> #include<string.h> #include<conio.h> typedef struct student { int m_id; char m_name[20]; int m_score; struct student *pNext; }STU; STU* Init(); void Insert(STU* pHead, STU* pData); void PrintList(STU* pHead); void DeleteNode(STU* pHead, char name[]); void ChangeNode(STU* pHead, char name[]); void FindNode(STU* pHead, char name[]); void FreeAll(STU* pHead); int Menu();
linked.c文件:
//linked.c #include "linked.h" STU* Init() { STU* pNew = (STU*)malloc(sizeof(STU)); pNew->pNext = NULL; return pNew; } void Insert(STU* pHead, STU * pData) { STU* pNew = Init(); pNew->m_id = pData->m_id; memcpy(pNew->m_name, pData->m_name, 20); //字符串不能直接赋值 pNew->m_score = pData->m_score; #if 0 //头插 pNew->pNext = pHead->pNext; pHead->pNext = pNew; #else //尾插 STU* pTemp = pHead; while (pTemp->pNext != NULL) { pTemp = pTemp->pNext; } pNew->pNext = pTemp->pNext; pTemp->pNext = pNew; #endif } void PrintList(STU* pHead) { STU* pTemp = pHead->pNext; //头结点不储存数据 printf("-------------------------\n"); printf("ID\tName\tScore\n"); while (pTemp != NULL) { printf("%d\t%s\t%d\n", pTemp->m_id, pTemp->m_name, pTemp->m_score); pTemp = pTemp->pNext; } printf("-------------------------\n"); } void DeleteNode(STU* pHead, char name[]) { STU* pTemp = pHead; while (pTemp->pNext != NULL) { if (strcmp(pTemp->pNext->m_name, name) == 0) { STU* pDelete = pTemp->pNext; pTemp->pNext = pDelete->pNext; free(pDelete); break; } pTemp = pTemp->pNext; } printf("\n删除成功...\n\n"); } void ChangeNode(STU* pHead, char name[]) { STU* pTemp = pHead->pNext; while (pTemp != NULL) { if (strcmp(pTemp->m_name, name) == 0) { printf("-------------------------\n\n"); printf("请输入修改后的学生ID:"); scanf("%d", &pTemp->m_id); printf("请输入修改后的学生Name:"); scanf("%s", pTemp->m_name); printf("请输入修改后的学生Score:"); scanf("%d", &pTemp->m_score); printf("\n-------------------------\n"); } pTemp = pTemp->pNext; } printf("\n修改成功...\n\n"); } void FindNode(STU* pHead, char name[]) { STU* pTemp = pHead->pNext; printf("-------------------------\n"); printf("\n查询学生信息表:\n"); printf("ID\tName\tScore\n"); while (pTemp != NULL) { if (strcmp(pTemp->m_name, name) == 0) { printf("%d\t%s\t%d\n\n", pTemp->m_id, pTemp->m_name, pTemp->m_score); printf("-------------------------\n"); return; } pTemp = pTemp->pNext; } } void FreeAll(STU * pHead) { STU* pTemp = pHead; while (pTemp->pNext != NULL) { STU* pDelete = pTemp->pNext; pTemp->pNext = pDelete->pNext; free(pDelete); } } int Menu() { char operate; do { printf("菜单:\n"); printf("1、添加学生信息:\n"); printf("2、删除学生信息:\n"); printf("3、修改学生信息:\n"); printf("4、查找学生信息:\n"); printf("5、显示学生信息:\n"); printf("6、退出并删除:\n"); operate = getch(); } while (operate > '5' || operate < '1'); return operate - '0'; }