模块化的链表案例:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<windows.h>
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
struct student {
char name[20];
int num;
int math;
};
//单链表:一般由结构体变量和结构体变量连接在一起组成,主要是通过指针连接在一起
////结构体变量:
//struct Node Node1 = { 1,NULL };
//struct Node Node2 = { 2,NULL };
//struct Node Node3 = { 3,NULL };//没有连接起来的结构体变量
//Node1.next = &Node2;//把Node2的入口地址给到Node1的指针域里面去
//Node2.next = &Node3;//把Node3的入口地址给到Node2的指针域里面去
////以上这几行是静态链表,静态链表的用处不大
//动态创建一个链表:就是动态内存申请 + 模块化设计
//1.创建一个链表(创建一个表头表示整个链表)
//2.创建节点(不断形成新的结点)
//3.插入结点
//4.删除节点
//5.遍历打印结点(测试)
struct Node {
struct student date; //数据域
struct Node* next; //指针域
};
struct Node* creatList()//创建一个表头表示整个链表
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));//headNode成为了结构体变量
//变量使用前必须被初始化,第一个变量的数据域不需要初始化,指针域需要初始化
//headNode->date = 1;
headNode->next = NULL;
return headNode;
}
//创建节点;也是为下面的插入结点章节做准备;
struct Node* creatNode(struct student date)//创建节点
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));//结构体指针变成结构体变量
newNode->date = date;
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->date.name,pMove->date.num,pMove->date.math);
pMove = pMove->next;
}
printf("\n");
}
//插入链表,插入哪个结点,插入结点的数据
void insertNodeByHead(struct Node* headNode, struct student date)
{
struct Node* newNode = creatNode(date);
newNode->next = headNode->next;
headNode->next = newNode;
}
//链表的删除:指定位置删除
void deleteNodeByAppointNum(struct Node* headNode, int num)
{
struct Node* posNode = headNode->next;
struct Node* posNodeFront = headNode;
if (posNode == NULL)
{
printf("链表为空,无法删除\n");
}
else
{
while (posNode->date.num != num)
{
posNodeFront = posNode;
posNode = posNodeFront->next;
if (posNode == NULL)
{
printf("无法找到相关信息无法删除");
return;
}
}
posNodeFront->next = posNode->next;
free(posNode);
}
}
int main()
{
struct Node* list = creatList();//创建一个头指针
//创建一个结构体变量的节点
//插入一个结点
struct student info;
while (1)
{
printf("输入学生的姓名,学号,成绩\n");
setbuf(stdin, NULL);
scanf_s("%s%d%d", info.name, &info.num, &info.math);
insertNodeByHead(list, info);
printf("continue(Y\N)?\n");
setbuf(stdin, NULL);
int choice = getchar();
if (choice == 'N' || choice == 'n')
{
break;
}
}
printList(list);
printf("请输入要删除的学生的学号:");
scanf_s("%d", &info.num);
deleteNodeByAppointNum(list, info.num);
printList(list);
system("pause");
return 0;
}
增删学生数据的简单案例:
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<windows.h>
struct student {
char name[20];
int num;
int math;
};
struct Node {
struct student date; //数据域
struct Node* next; //指针域
};
struct Node* creatList()//创建一个表头表示整个链表
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));//headNode成为了结构体变量
//变量使用前必须被初始化,第一个变量的数据域不需要初始化,指针域需要初始化
//headNode->date = 1;
headNode->next = NULL;
return headNode;
}
//创建节点;也是为下面的插入结点章节做准备;
struct Node* creatNode(struct student date)//创建节点
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));//结构体指针变成结构体变量
newNode->date = date;
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->date.name,pMove->date.num,pMove->date.math);
pMove = pMove->next;
}
printf("\n");
}
//插入链表,插入哪个结点,插入结点的数据
void insertNodeByHead(struct Node* headNode, struct student date)
{
struct Node* newNode = creatNode(date);
newNode->next = headNode->next;
headNode->next = newNode;
}
//链表的删除:指定位置删除
void deleteNodeByAppointNum(struct Node* headNode, int num)
{
struct Node* posNode = headNode->next;
struct Node* posNodeFront = headNode;
if (posNode == NULL)
{
printf("链表为空,无法删除\n");
}
else
{
while (posNode->date.num != num)
{
posNodeFront = posNode;
posNode = posNodeFront->next;
if (posNode == NULL)
{
printf("无法找到相关信息无法删除");
return;
}
}
posNodeFront->next = posNode->next;
free(posNode);
}
}
int main()
{
struct Node* list = creatList();//创建一个头指针
//创建一个结构体变量的节点
//插入一个结点
struct student info;
while (1)
{
printf("输入学生的姓名,学号,成绩\n");
setbuf(stdin, NULL);
scanf_s("%s%d%d", info.name, 20,&info.num, &info.math);
insertNodeByHead(list, info);
printf("continue(Y\N)?\n");
setbuf(stdin, NULL);
int choice = getchar();
if (choice == 'N' || choice == 'n')
{
break;
}
}
printList(list);
printf("请输入要删除的学生的学号:");
scanf_s("%d", &info.num);
deleteNodeByAppointNum(list, info.num);
printList(list);
system("pause");
return 0;
}
|