B站C语言——链表

静态链表

#include<stdio.h>

typedef struct Teacher
{
	int data;
	struct Teacher *next;
}Teacher;

int main(void)
{
	//静态链表
	//固定个数 ,结点的内存生命周期
	Teacher t1, t2, t3;
	t1.data = 1;
	t2.data = 2;
	t3.data = 3;
	
	t1.next = &t2;
	t2.next = &t3;
	t3.next = NULL;
	
	return 0; 
} 

动态创建一个链表(创建一个表头表示整个链表)

  1. 创建链表(创建一个表头表示整个链表)
  2. 创建结点
  3. 插入结点
  4. 删除结点
  5. 打印遍历链表(测试)
#include<stdio.h>
#include<stdlib.h>

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

struct Node* creatList()
{
	struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
	//headNode 成为了结构体变量
	//变量使用前必须被初始化

	//headNode->data = 1;
	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 printList(struct Node *headNode)
{
	struct Node* pMove = headNode->next; 
	while (pMove)
	{
		printf("%d\t", pMove->data);
		pMove = pMove->next;
	}
	printf("\n");

}

//插入结点
void insertNodeByHead(struct Node* headNode, int data)
{
	//创建插入的结点
	struct Node* newNode = creatNode(data);
	newNode->next = headNode->next;
	headNode->next = newNode;
}

//删除指定结点
void deleteNodeByAppoin(struct Node* headNode, int posData)
{
	struct Node* posNode = headNode->next;  //posNode要从表头的下一个结点开始找
	struct Node* posNodeFront = headNode;
	if (posNode == NULL)
		printf("无法删除,链表为空\n");
	else
	{
		while (posNode->data != posData)
		{
			posNodeFront = posNode;
			posNode = posNodeFront->next;
			if (posNode == NULL)
			{
				printf("没有找到相关信息,无法删除\n");
				return;
			}
		}
		posNodeFront->next = posNode->next;
		free(posNode);
	}
	
}

int main(void)
{
	struct Node* list = creatList();
	insertNodeByHead(list, 1);
	insertNodeByHead(list, 2);
	insertNodeByHead(list, 3);
	printList(list);
	deleteNodeByAppoin(list, 2);
	printList(list);
	return 0;
}

学以致用——学生成绩管理系统

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

struct student
{
	char name[20];
	int num;
	int math;
};

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

struct Node* creatList()
{
	struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
	//headNode 成为了结构体变量
	//变量使用前必须被初始化

	//headNode->data = 1;
	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 printList(struct Node* headNode)
{
	struct Node* pMove = headNode->next;
	printf("name\tnum\tmath\n");
	while (pMove)
	{
		printf("%s\t%d\t%d\n", pMove->data.name, pMove->data.num, pMove->data.math);
		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 deleteNodeByAppoinNum(struct Node* headNode, int num)
{
	struct Node* posNode = headNode->next;  //posNode要从表头的下一个结点开始找
	struct Node* posNodeFront = headNode;
	if (posNode == NULL)
		printf("无法删除,链表为空\n");
	else
	{
		while (posNode->data.num != num)
		{
			posNodeFront = posNode;
			posNode = posNodeFront->next;
			if (posNode == NULL)
			{
				printf("没有找到相关信息,无法删除\n");
				return;
			}
		}
		posNodeFront->next = posNode->next;
		free(posNode);
	}

}

int main(void)
{
	struct Node* list = creatList();
	/*insertNodeByHead(list, 1);
	insertNodeByHead(list, 2);
	insertNodeByHead(list, 3);
	printList(list);
	deleteNodeByAppoinNum(list, 2);
	printList(list);*/
	struct student info;
	while (1)
	{
		printf("请输入学生姓名 学号 数学成绩:");
		setbuf(stdin, NULL);
		scanf("%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("%d", &info.num);
	deleteNodeByAppoinNum(list, info.num);
	printList(list);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值