链表的实现以及合并,排序,逆序,等

本文介绍了链表的基础知识,包括链表结构、初始化、节点创建、插入操作(头插、尾插)、查找、删除操作。重点讲解了如何合并两个有序链表保持有序、逆序链表、找到链表中间节点、删除倒数第k个节点以及链表排序的方法。同时提供了测试代码和完整实现供参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1。链表的结构


2,链表的初始化


3,创建节点


4,尾插


5,头插


6,查找



7,头删


8,移除


9,合并两个有序链表,合并后依然有序


Plist merge(Plist head3,Plist head4)
{
	if (head3 == NULL)
		return head4;
	if (head4 == NULL)
		return head3;
	Plist p = NULL;
	Plist cur1 = head3;
	Plist cur2 = head4;
	Plist tail = NULL;
	if (cur1->data < cur2->data)
	{
		p = cur1;
		cur1 = cur1->next;
		tail = p;
	}
	else
	{
		p = cur2;
		cur2 = cur2->next;
		tail = p;
	}
	while (cur1&&cur2)
	{
		if (cur1->data< cur2->data)
		{
			tail->next=cur1;
			cur1 = cur1->next;
			tail = tail->next;
		}
		else
		{
			tail->next = cur2;
			cur2 = cur2->next;
			tail = tail->next;
		}
	}
	if (cur1)
	{
		tail->next = cur1;
		return p;
	}
	if (cur2)
	{
		tail->next=cur2;
		return p;
	}
}


10,逆制一个链表


11,查找链表的中间节点


12,删除倒数第k个节点



13,链表的排序


14,把奇数放在偶数前面

Plist jishu_front(Plist head)//把链表奇数放在偶束前面
{
	if (head == NULL)
	{
		printf("the chain is null\n");
		return NULL;
	}
	Plist tmp = NULL;
	Plist cur = head;
	Plist newHead = NULL;
	Plist oushuhead = NULL;
	while (cur)
	{
		tmp = cur;
		cur = cur->next;
		if (tmp->data % 2 == 1)
		{
			tmp->next = newHead;
			newHead = tmp;
		}
		if (tmp->data % 2 == 0)
		{
			tmp->next = oushuhead;
			oushuhead = tmp;
		}
	}
	Plist head1 = newHead;
	while (head1->next != NULL)
	{
		head1 = head1->next;
	}
	head1->next = oushuhead;
	return newHead;


}

测试代码


完整代码


typedef int DataType;
typedef struct Listnode
{
	DataType data;
	struct Listnode *next;
}List,*Plist;
void Init(Plist&head)
{
	head = NULL;
}
Plist _CreatNode(DataType num)
{
	Plist p = (Plist)malloc(sizeof(List));
	if (p == NULL)
	{
		printf("申请失败。。。\n");
		 exit(EXIT_FAILURE);
	}
	p->data = num;
	return p;
}
void PushBack(Plist&head,DataType num)
{
	Plist p = _CreatNode(num);


	if (head == NULL)
	{
		head= p;
		p->next = NULL;
	}
	else
	{
		Plist cur = head;
		while (cur->next)
		{
			cur = cur->next;
		}
		cur->next = p;
		p->next = NULL;
	}
}
void Pushfront(Plist &head,DataType num)
{
	if (head == NULL)
	{
		PushBack(head,num);
	}
	else
	{
		Plist p=head;
		Plist s = _CreatNode(num);
		head = s;
		s->next = p;
		
	}
}
Plist find(Plist &head,DataType x)
{
	Plist cur = head;
	if (head->data == x)
		return head;
	while (cur->next)
	{
		if (cur->next->data == x)
			return cur;
		cur = cur->next;
	}
	return NULL;
}
void Popfront(Plist &head)
{
	if (head == NULL)
	{
		return;
	}
	Plist Head = head;
	if (head->next == NULL)
	{
		head = NULL;
	}
	else
	{
		head = head->next;
	}
	free(Head);


}
void remove(Plist &head,DataType x)
{
	if (head == NULL)
	{
		printf("this chain is NULL\n");
		return;
	}
	Plist p = find(head, x);


	if (head == p)
	{
		Popfront(head);
	}
	if (p == NULL)
	{
		printf("not the data\n");
		return;
	}
	p->next = p->next->next;
}
Plist merge(Plist head3,Plist head4)
{
	if (head3 == NULL)
		return head4;
	if (head4 == NULL)
		return head3;
	Plist p = NULL;
	Plist cur1 = head3;
	Plist cur2 = head4;
	Plist tail = NULL;
	if (cur1->data < cur2->data)
	{
		p = cur1;
		cur1 = cur1->next;
		tail = p;
	}
	else
	{
		p = cur2;
		cur2 = cur2->next;
		tail = p;
	}
	while (cur1&&cur2)
	{
		if (cur1->data< cur2->data)
		{
			tail->next=cur1;
			cur1 = cur1->next;
			tail = tail->next;
		}
		else
		{
			tail->next = cur2;
			cur2 = cur2->next;
			tail = tail->next;
		}
	}
	if (cur1)
	{
		tail->next = cur1;
		return p;
	}
	if (cur2)
	{
		tail->next=cur2;
		return p;
	}
}
Plist reverse(Plist head)
{
	Plist newHead = NULL;
	Plist cur = head;
	Plist tmp = NULL;
	while (cur)
	{
		tmp=cur;
		cur = cur->next;
		tmp->next = newHead;
		newHead = tmp;
	}
	
	return newHead;
}
Plist find_mid_node(Plist head)
{
	if (head == NULL)
	{
		printf("the chain is NULL\n");
		return NULL;
	}
	if (head->next == NULL||head->next->next==NULL)
	{
		return head;
	}
	Plist fast = head;
	Plist slow = head;


	while (fast->next&&fast->next->next)
	{
		fast = fast->next->next;
		slow = slow->next;
	}
	return slow;
}
void delete_k_node(Plist head,int k)
{
	Plist front = head;
	Plist back = head;
	while (front)
	{
		k--;
		if (k<0)
		{
			back = back->next;
		}
		front = front->next;
	}
	if (k < 0)
	{
		back->data = back->next->data;
		Plist del = back->next;
		back->next = back->next->next;
		free(del);
	}
}
Plist _Sort(Plist head)
{
	if (head == NULL)
	{
		printf("the chain is NULL\n");
		return NULL;
	}
	Plist tail = NULL;
	Plist cur1 = NULL;
	for (Plist cur = head; cur->next!= NULL; cur = cur->next)
	{
		for (cur1 = head; cur1->next != tail; cur1 = cur1->next)
		{
			if (cur1->data > cur1->next->data)
			{
				swap(cur1->data, cur1->next->data);


			}
		}
		tail = cur1;
	}
	return head;
}
Plist jishu_front(Plist head)//把链表奇数放在偶束前面
{
	if (head == NULL)
	{
		printf("the chain is null\n");
		return NULL;
	}
	Plist tmp = NULL;
	Plist cur = head;
	Plist newHead = NULL;
	Plist oushuhead = NULL;
	while (cur)
	{
		tmp = cur;
		cur = cur->next;
		if (tmp->data % 2 == 1)
		{
			tmp->next = newHead;
			newHead = tmp;
		}
		if (tmp->data % 2 == 0)
		{
			tmp->next = oushuhead;
			oushuhead = tmp;
		}
	}
	Plist head1 = newHead;
	while (head1->next != NULL)
	{
		head1 = head1->next;
	}
	head1->next = oushuhead;
	return newHead;


}
int main()
{
	Plist head2 ;
	Plist head1;
	Init(head2);
	Init(head1);
	PushBack(head1, 1);
	PushBack(head1, 2);
	PushBack(head1, 3);
	PushBack(head2,5);
	PushBack(head2, 4);
	PushBack(head2, 6);
	PushBack(head2, 7);


	Plist l = merge(head1,head2);	
	Plist h=reverse(l);
	Plist a = find_mid_node(h);
	Plist g=_Sort(h);
	Plist m=jishu_front(g);
	/*delete_k_node(h,2);*/
	printf("%d\n",a->data);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值