数据结构--链表(C language)

这篇博客介绍了如何使用C语言实现链表的基本操作,包括在指定位置插入节点、删除指定下标节点、链表反转以及遍历输出链表。代码详细展示了每个操作的过程,并在最后进行了测试,包括创建链表、输出链表、反转链表、删除节点以及清理链表。
摘要由CSDN通过智能技术生成
#include<stdio.h>
#include<stdlib.h>
typedef struct Node {
	int data;
	struct Node* next;
}Node,*LinkedList;

LinkedList insert(LinkedList head, Node* node, int index) { //插入:把node插入到第index下标处
	if (head == NULL) { //空链表的情况下
		if (index != 0) {  //该情况下,只能在index=0处插入元素
			return head;
		}
		head = node;  //让node为表头
		return head;
	}
	if (index == 0) { //不为空链表的情况下,插入的下标为0
		node->next = head;
		head = node;
		return head;
	}
	//接下来要遍历链表, 找到下标前一个节点  ,或者遍历到最后一个节点退出
	Node* current_node = head;
	int count = 0;
	while (current_node->next != NULL && count < index - 1) {
		current_node = current_node->next;
		count++;
	}
	if (count == index - 1) {   //害怕万一index很大,那个while循环是按照第一个情况退出的,同样是插入失败的;
		node->next = current_node->next; //只有count==index-1,才插入成功
		current_node->next = node;
	}
	return head;
}
LinkedList delete_node(LinkedList head, int index) { //删除index下标的节点
	if (head == NULL) {
		return head;
	}
	Node* current_node = head;
	int count = 0;
	if (index == 0) {
		head = head->next;
		free(current_node);
		return head;
	}
	while (current_node->next != NULL && count < index - 1) { //遍历链表,遍历到最后一个节点退出 ,或者找到下标前一个节点退出
		current_node = current_node->next;
		count++;
	}
	if (current_node->next != NULL && count == index - 1) { //删除最后一个节点就得遍历到倒数第二个,
		Node* delete_node = current_node->next;		         //如果遍历到最后一个(也就是current_node == NULL)是不可以的
		current_node->next = delete_node->next;
		free(delete_node);
	}
	return head;
}
LinkedList reverse(LinkedList head) {  //反转
	if (head == NULL) {
		return head;
	}
	Node* current_node, * next_node;
	current_node = head->next;
	head->next = NULL;
	while (current_node != NULL) {
		next_node = current_node->next;
		current_node->next = head;
		head = current_node;
		current_node = next_node;
	}
	return head;
}
void output(LinkedList head) {
	if (head == NULL) {
		return;
	}
	Node* node = head;
	while (node != NULL) { //用while循环挨个遍历  直到node节点为 NULL
		printf("%d ", node->data);
		node = node->next; //往下走一个节点
	}
	printf("\n");
	return;
}
void clear(LinkedList head) {
	if (head == NULL) {
		return;
	}
	Node* node = head;
	while (node != NULL){ //用while循环挨个遍历  直到node节点为 NULL
		Node* dele = node;
		node = node->next;//往下走一个节点
		free(dele);
	}
	return;
}
int main() {
	//链表是结构体变量和结构体变量通过指针联系在一起
	//动态创建一个链表   :动态内存申请+模块化设计

	LinkedList linkedlist = NULL;   //测试
	for (int i = 0; i < 10; i++) {
		Node* node = (Node*)malloc(sizeof(Node));
		node->next = NULL;
		node->data = i;
		linkedlist = insert(linkedlist, node, i);//插入
	}
	output(linkedlist); //遍历
	linkedlist = reverse(linkedlist);  //反转
	output(linkedlist);
	linkedlist = delete_node(linkedlist, 0);//删除第0个位置
	output(linkedlist);
	clear(linkedlist);
	system("pause");
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值