数据结构之单链表

动态创建一个链表:动态内存申请+模块化设计
1.创建链表(创建一个表头表示整个链表)
2.创建节点
3.插入节点
4.删除节点
5.打印遍历链表(测试)

什么是链表?
链表是结构体变量与结构体变量连接在一起。
链表结构体由两部分组成:数据域指针域.
结构指针通过内存分布申请变为结构体变量

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

struct Node {
	int data;//数据域
	struct Node* next;//指针域
};

//创建链表
struct Node* createList() {
	struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));//分配动态内存
	//headNode 成为了 结构体变量
	//变量使用前必须被初始化
	//headNode->data = 1; 
	headNode->next = NULL;
	return headNode;
}

//创建一个节点,节点其实是结构体变量(与链表的区别是多了一个数据域参数)
struct Node* createNode(int 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* pNode = headNode->next;//从第二个节点开始打印
	while (pNode) {
		printf("%d\t", pNode->data);
		pNode = pNode->next;
	}
	printf("\n");
}

//插入节点,插入的链表+节点数据
void insertNode(struct Node* headNode, int data) {
	//创建插入的节点
	struct Node* newNode = createNode(data);
	newNode->next = headNode->next;
	headNode->next = newNode;
}

//删除节点
void deleteNode(struct Node* headNode, int data) {
	struct Node* posNode = headNode->next;
	struct Node* posNodeFront = headNode;
	if (posNode == NULL) {
		printf("无法删除,链表为空");
	}
	else {
		while (posNode->data != data) {
			posNodeFront = posNode;
			posNode = posNodeFront->next;
			if (posNode == NULL) {
				printf("未找到相关信息,无法删除");
				break;
			}
		}
		posNodeFront->next = posNode->next;
		free(posNode);
	}
}

int main() {
	struct Node* list = createList();//创建完一个链表
	insertNode(list, 1);
	insertNode(list, 2);
	insertNode(list, 3);
	printList(list);
	deleteNode(list,2);
	printList(list);
	return 0;
}

在这里插入图片描述
如果链表结构体中的数据域是一个结构体呢?

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

struct student
{
	int stuid;
	char name[20];
	float score[3];
};

struct Node {
	struct student data;//数据域
	struct Node* next;//指针域
};

//创建链表
struct Node* createList() {
	struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));//分配动态内存
	//headNode 成为了 结构体变量
	//变量使用前必须被初始化
	//headNode->data = 1; 
	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* pNode = headNode->next;//从第二个节点开始打印
	printf("\n");
	printf("stuId\tname\tscore[1]\tscore[2]\tscore[3]\n");
	while (pNode) {
		printf("%d\t%s\t%5.2f\t\t%5.2f\t\t%5.2f\n", pNode->data.stuid,pNode->data.name,pNode->data.score[0], pNode->data.score[1], pNode->data.score[2]);
		pNode = pNode->next;
	}
	printf("\n");
}

//插入节点,插入的链表+节点数据
void insertNode(struct Node* headNode, struct student data) {
	//创建插入的节点
	struct Node* newNode = createNode(data);
	newNode->next = headNode->next;
	headNode->next = newNode;
}

void deleteNode(struct Node* headNode, int num) {
	struct Node* posNode = headNode->next;
	struct Node* posNodeFront = headNode;
	if (posNode == NULL) {
		printf("无法删除,链表为空");
	}
	else {
		while (posNode->data.stuid != num) {//按学生的学号来进行删除
			posNodeFront = posNode;
			posNode = posNodeFront->next;
			if (posNode == NULL) {
				printf("未找到相关信息,无法删除");
				break;
			}
		}
		posNodeFront->next = posNode->next;
		free(posNode);
	}
}

int main() {
	struct Node* list = createList();//创建完一个链表
	struct student stu;
	for (int i = 0; i < 3;i++) {
		printf("请输入第 %d 个学生的信息:\n",i+1);
		printf("学号+姓名+score 1+score 2+score 3:\n");
		scanf("%d%s%f%f%f",&stu.stuid, &stu.name, &stu.score[0], &stu.score[1], &stu.score[2]);

		insertNode(list, stu);
	}
	printList(list);
	printf("请输入你想删除的学生信息的学号:");
	scanf("%d",&stu.stuid);
	deleteNode(list,stu.stuid);
	printList(list);
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值