数据结构-单链表
动态创建一个链表:动态内存申请+模块化设计
1.创建链表(创建一个表头表示整个链表)
2.创建结点
3.插入结点
4.删除结点
5.打印遍历链表(测试)
创建 存放数据域和指针域的结构体
struct Node {
int data;
struct Node* next;
};
创建表头
表头不用初始化数据域
struct Node* createlist()
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));//头指针
headNode->next = NULL;
return headNode;
}
创建节点
//创建结点
struct Node* creatNode(int data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;//初始化数据域
newNode->next = NULL;//指向空
return newNode;//创建完成返回
}
插入元素
头插法
void insertNodeByHead(struct Node* headNode,int data)
{
//创建插入的结点
struct Node* newNode = creatNode(data);
newNode->next = headNode->next;//新的指向原来表头的下一个
headNode->next = newNode;//原来表头下一个指向新的
}
尾插法
void insertNodeByTail(struct Node* headNode, int data)
{
//创建插入的结点
struct Node* newNode = creatNode(data);
struct Node* tailNode = headNode;//创建一个结点指向头指针
while (tailNode->next != NULL)
{
tailNode = tailNode->next;//一直找,直到找到尾巴为NULL的前一个则找到
}
tailNode->next = newNode;//找到直接下一个指向新的,下面不用写指向NULL,因为在创建结点时他后面自动指向NULL
}
指定位置插入
void inserNodeNum(struct Node* headNode,int data,int num)
{
struct Node* newNode = creatNode(data);
struct Node* insNodeFront = headNode;//指定位置前面那个结点
struct Node* insNode = headNode->next;//指定位置结点
if (insNode== NULL)
{
printf("链表为空");
return;
}
else
{
while (insNode->data != num)
{
insNodeFront = insNode;
insNode = insNodeFront->next;
if (insNode == NULL)
{
printf("未找到指定位置");
return;
}
}
}
newNode->next = insNode;
insNodeFront->next = newNode;
}
指定位置删除
void deleteNode(struct Node* headNode, int posData)
{
struct Node* posNode = headNode->next;//只能从表头的下一个开始找
struct Node* posNodeFront = headNode;
if (posNode==NULL)
{
printf("空链表无法删除");
}
else
while (posNode->data != posData)
{
posNodeFront = posNode;
posNode = posNodeFront->next;
if (posNode == NULL)
{
printf("没找到相关信息,无法删除");
return;
}
}
posNodeFront->next = posNode->next;//前面那个结点的下一个指向后面那个结点的next
free(posNode);
}
打印遍历
void Print(struct Node* headNode)
{
struct Node* pMove = headNode->next;
while(pMove)
{
printf("%d\t", pMove->data);
pMove = pMove->next;
}
printf("\n");
}
简单改造的学生管理系统代码
main.cpp
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
struct student
{
char name[20];
int age;
double math;
int num;
};
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* creatNode(struct student data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
//打印遍历结点
void Print(struct Node* headNode)
{
struct Node* pMove = headNode->next;
printf("name\tage\tmath\tnum\t\n");
while(pMove)
{
printf("%s\t%d\t%lf\t%d\t\n", pMove->data.name,pMove->data.age,pMove->data.math,pMove->data.num);
pMove = pMove->next;
}
printf("\n");
}
//头插,插入哪个链表,插入结点数据是多少
void insertNodeByHead(struct Node* headNode,struct student data)
{
//创建插入的结点
struct Node* newNode = creatNode(data);
newNode->next = headNode->next;//新的指向原来表头的下一个
headNode->next = newNode;//原来表头下一个指向新的
}
//尾插
void insertNodeByTail(struct Node* headNode, struct student data)
{
//创建插入的结点
struct Node* newNode = creatNode(data);
struct Node* tailNode = headNode;//创建一个结点指向头指针
while (tailNode->next != NULL)
{
tailNode = tailNode->next;//一直找,直到直到尾巴为NULL则找到
}
tailNode->next = newNode;//找到直接下一个指向新的,后面不用写指向NULL,因为在创建结点时他后面自动指向NULL
}
//指定位置删除
void deleteNode(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;//前面那个结点的下一个指向后面那个结点的next
free(posNode);
}
int main()
{
struct Node* list = createlist();
struct student info;
while (1)
{
printf("请输入学生姓名 年龄 数学成绩 编号\n");
scanf("%s %d %lf %d", info.name, &info.age, &info.math, &info.num);
insertNodeByHead(list, info);
setbuf(stdin, NULL);//清空缓冲区
printf("是否继续y/n");
int c = getchar();
if (c=='n' || c == 'N')
{
break;
}
}
Print(list);
return 0;
}