C语言~单链表

动态创建一个链表:动态内存申请+模块化设计

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");
}

下面是简单改造的学生管理系统代码:

#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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Luckys-Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值