【学习笔记】C语言链表

1.什么是链表?

        链表是多个结构体变量通过结构体指针连接在一起的统一整体。

2.动态创建一个链表,即动态内存申请+模块化设计

        模块分类 1.创建链表(创建一个表头表示整个列表)

                        2.创建节点

                        3.插入节点

                        4.删除节点

                        5.打印遍历链表(测试)

【一】创建链表

        1.head如何定义使其成为结构体变量——动态内存申请。

//创建链表
struct Node* createlist() {
	struct Node* head = (struct Node*)malloc(sizeof(struct Node));
	//head成为结构体变量
	//变量使用前初始化
	//head->data = 1;
	head->next = NULL;
	return head;
};

 【二】创建节点

struct Node* creatNode(int date)
{
	struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
	newNode->data = date;//变量初始化
	newNode->next = NULL;//指针初始化
	return newNode;
};

【三】插入节点(尾插,头插,指定位置插入)

       1.头插

void insertNodeByHead(struct Node* head,int data)
{
	struct Node* newNode = createNode(data);
	newNode->next = head->next;
	head->next = newNode;
};

【四】指定位置删除

int deletNode(struct Node* head,int posData)
{
	struct Node* posNode = head-> next;
	struct Node* posNodeFront = head;
	if (posNode == NULL) printf("无法删除链表为空\n");
	else{
		while (posNode->data != posData)
		{
			posNodeFront = posNode;
			posNode = posNode->next;
			if (posNode == NULL) {
				printf("未找到相关信息,无法删除\n");
				return 0;
			}
		}
			posNodeFront->next = posNode->next;
			free(posNode);
		}
}

【五】打印

void printList(struct Node* headNode)
{
	struct Node* Pmove = headNode->next;
	while (Pmove)//节点不为空
	{
		printf("%d", Pmove->data);
		Pmove = Pmove->next;//指针移动,循环遍历
	}
	printf("\n");
}

整体代码

include<stdio.h>
#include<stdlib.h>
struct Node {
	int data;         //数据
	struct Node* next;//指针
};

struct Node* createlist() {
	struct Node* head = (struct Node*)malloc(sizeof(struct Node));
	//head成为结构体变量
	//变量使用前初始化
	//head->data = 1;
	head->next = NULL;
	return head;
};
//为了插入,要实现创建节点功能
struct Node* createNode(int date)
{
	struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
	newNode->data = date;//变量初始化
	newNode->next = NULL;//指针初始化
	return newNode;
};
//创建一个可以移动的指针
void printList(struct Node* head)
{
	struct Node* Pmove = head->next;
	while (Pmove)//节点不为空
	{
		printf("%d\n", Pmove->data);
		Pmove = Pmove->next;//指针移动,循环遍历
	}
	printf("\n");
}
//插入数据是什么
void insertNodeByHead(struct Node* head,int data)
{
	struct Node* newNode = createNode(data);
	newNode->next = head->next;
	head->next = newNode;
};

//指定位置删除(指定数据删除)——遍历
int deletNode(struct Node* head,int posData)
{
	struct Node* posNode = head-> next;
	struct Node* posNodeFront = head;
	if (posNode == NULL) printf("无法删除链表为空\n");
	else{
		while (posNode->data != posData)
		{
			posNodeFront = posNode;
			posNode = posNode->next;
			if (posNode == NULL) {
				printf("未找到相关信息,无法删除\n");
				return 0;
			}
		}
			posNodeFront->next = posNode->next;
			free(posNode);
		}
}

int main() {
	struct Node* list = createlist();//创建完成
	insertNodeByHead(list, 1);
	insertNodeByHead(list, 2);
	insertNodeByHead(list, 3);
	printList(list);
	deletNode(list, 2);
	printList(list);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值