C语言之链表

写在前边

C语言链表是一种很常见的数据结构,熟练掌握链表对理解和运用C语言有着很大的作用。在其他的博客中很容易搜到链表的相关操作。在这里就不在赘述了。

github上链接:https://github.com/myzcl/C-pratice
里面还有很多c语言的练习。

代码

下面是头结点的链表示例。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct node
{
	int value;
	struct node *next;
}node;

node *head = NULL;

//创建节点
node *mk_node(int value)
{
	node *p = (node *)malloc(sizeof(node));
	if (p == NULL)
	{
		printf("malloc failed!\n");
		exit(1);
	}
	
	p->value = value;
	p->next = NULL;
	
	return p;
}

//释放节点
void free_node(node *p)
{
	free(p);
}

//插入节点
void insert_node(node *p)
{
	p->next = head;
	head = p;
}

//遍历节点
void traverse()
{
	node *p = head;
	
	while (p != NULL)
	{
		printf("%d ", p->value);
		p = p->next;		
	}
	printf("\n");
}

//销毁链表
void destroy_link()
{
	node *p = head;
	
	while (head != NULL)
	{
		p = head;
		head = head->next;
		free_node(p);
	}
}

//查找节点
node *search_node(int value)
{
	node *p = head;
	
	while (p != NULL)
	{
		if (p->value == value)
		{
			return p;
		}

		p = p->next;
	}
	
	return NULL;
}

//删除节点(rm意思是移除,不需要释放,单独释放)
void rm_node(node *p)
{
	node *pre = head;
	
	if (p == head)
	{
		head = head->next;
		p->next = NULL;
		//free_node(p);
		return;
	}
	
	while (pre->next != NULL)
	{
		if (pre->next == p)
		{
			pre->next = p->next;
			p->next = NULL;
			//free_node(p);
			return;
		}
		
		pre = pre->next;
	}
}

//链表逆序
void reverse_list()
{
	node *p = head;
	node *nhead = NULL;
	
	if (p == NULL || p->next == NULL)//注意次序,短路
	{
		return;
	}
	
	while (head != NULL)
	{
		p = head;
		head = head->next;
		
		p->next = nhead;
		nhead = p;
	}
	
	head = nhead;
}

//删除value = x的节点(需要释放)
void dele_x_node1(int x)
{
	node *p;

	while ((p = search_node(x)) != NULL)
	{
		rm_node(p);
		free_node(p);
	}
}

//删除value = x的节点(需要释放)
void dele_x_node2(int x)
{
	node *pre = head;
	node *p;
	
	if (head == NULL)
	{
		return;
	}
	
	while (pre->next != NULL)
	{
		p = pre->next;
		if (p->value == x)
		{
			pre->next = p->next;
			free_node(p);
			continue;
		}
		
		pre = pre->next;
	}
	
	if (head->value == x)
	{
		p = head;
		head = head->next;
		free_node(p);
	}
}


//顺序插入
void insert_node_sort(node *p)
{
	//node *p;
	node *pre = head;
	
	if (head == NULL || p->value < head->value)
	{
		p->next = head;
		head = p;
		return;
	}
	
	while (pre->next != NULL)
	{
		if (p->value < pre->next->value)
		{
			break;
		}
			
		pre = pre->next;
	}
	
	p->next = pre->next;
	pre->next = p;
}

//无序变有序
void insert_sort()
{
	node nhead = {0, NULL};
	node *pre = head;
	node *p = head;
	
	while (head != NULL)
	{
		p = head;
		head = head->next;
		
		pre = &nhead;
		while (pre->next != NULL)
		{
			if (p->value < pre->next->value)
			{
				break;
			}
			
			pre = pre->next;
		}
		p->next = pre->next;
		pre->next = p;
	}
	
	head = nhead.next;
}

int main()
{
	int i = 0;
	int value = 0;
	node *p;
	
	srand(time(NULL));
	for (i = 0; i < 10; i++)
	{
		value = rand() % 100 + 1;
		if (i < 5)
		{
			value = 20;
		}
		else
		{
			value = i + 1;			
		}

		p = mk_node(value);
		insert_node(p);
		//insert_node_sort(p);
	}
	traverse(head);
	
	insert_sort(head);
	traverse(head);
	
	p = search_node(9);
	if (p == NULL)
	{
		printf("can't find!\n");
	}
	else
	{
		rm_node(p);	
		free_node(p);
	}
	traverse();
	
	reverse_list();
	traverse();
	
	dele_x_node2(20);
	traverse();
	
	
	destroy_link();
	traverse();
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

chasentech

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

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

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

打赏作者

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

抵扣说明:

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

余额充值