单链表的所有操作,检查是否有环,合并链表,逆置链表,排序链表......

单链表我能想起来的所有操作,这里是带头结点的单链表:

主要包括,创建,增加,删除,查找,逆置,排序,合并,检测是否有环,等等。不顾有一个没有实现,就是找到环的入口地址,这个和找环是一样的,找到有环了,一个慢指针重新开始从头走,一个从检测到环的那个点走。等他们相遇,就是环的入口地址。


#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;

//带头结点的单链表
struct Node
{
	int data;
	Node *next;
};
void Init(Node *p)
{
	if(p != NULL)
		p->next = NULL;
}
bool Insert_head(Node *p,int key)
{
	bool end = false;
	if(p != NULL)
	{
		Node *tmp = new Node;
		tmp->data = key;
		tmp->next = p->next;
		p->next = tmp;
		end = true;
	}
	return end;
}
bool Delete_head(Node *p)
{
	bool end = false;
	if(p != NULL)
	{
		if(p->next != NULL)
		{
			Node *tmp = p->next;
			p->next = tmp->next;
			delete tmp;
			end = true;
		}
	}
	return end;
}
bool Delete_first_key(Node *p,int key)
{
	bool end = false;
	if(p != NULL)
	{
		Node *tmp = p->next;
		while(tmp != NULL && tmp->data != key)
		{
			p = tmp;
			tmp = tmp->next;
		}
		if(tmp !=NULL)
		{
			p->next = tmp->next;
			delete tmp;
			end = true;
		}
	}
	return end;
}
Node *Find_value(Node *p,int data)
{
	if(p != NULL)
	{
		p = p->next;
		while(p != NULL && p->data != data)
			p = p->next;
	}
	return p;
}
void Show(Node *p)
{
	if(p != NULL)
	{
		Node *tmp = p->next;
		while(tmp)
		{
			cout<<tmp->data<<" ";
			tmp = tmp->next;
		}
		cout<<endl;
	}
}
void Merge(Node *p1,Node *p2)
{
	if(p1 == NULL || p2 == NULL || p1 == p2)
		return ;
	Node *tmp1 = p1->next;
	Node *tmp2 = p1;
	Node *tmp3 = p2->next;
	while(tmp1 != NULL && tmp3 != NULL)
	{
		if(tmp1->data < tmp3->data)
		{
			tmp2 = tmp1;
			tmp1 = tmp1->next;
		}
		else
		{
			p2->next = tmp3->next;
			tmp3->next = tmp1;
			tmp2->next = tmp3;
			tmp3 = p2->next;
			tmp2 = tmp2->next;
		}
	}
	while(tmp3 != NULL)
	{
		p2->next = tmp3->next;
		tmp3->next = tmp1;
		tmp2->next = tmp3;
		tmp3 = p2->next;
		tmp2 = tmp2->next;
	}
	p2->next = NULL;
}
void Show_fin_k(Node *p,int k)
{
	if(p != NULL)
	{
		Node *tmp = p->next;
		int i = 0;
		while(i < k && tmp != NULL)
		{
			i++;
			tmp = tmp->next;
		}
		if(i == k)
		{
			Node *tmp1 = p->next;
			while(tmp != NULL)
			{
				tmp = tmp->next;
				tmp1 = tmp1->next;
			}
			cout<<"fin_k:"<<tmp1->data<<endl;
		}
	}
}
bool Is_round(Node *p)
{
	bool end = false;
	if(p != NULL)
	{
		Node *tmp1 = p;
		Node *tmp2 = p->next;
		while(tmp2 != NULL && tmp2 != tmp1)
		{
			tmp1 = tmp1->next;
			if(tmp2->next == NULL)
				break;
			tmp2 = tmp2->next->next;
		}
		if(tmp2 == tmp1)
			end = true;
	}
	return end;
}
void Reverse(Node *p)
{
	if(p != NULL)
	{
		Node *tmp1 = p;
		Node *tmp2 = p->next;
		if(tmp2 != NULL)
		{
			Node *tmp3;
			while(tmp2 != NULL)
			{
				tmp3 = tmp2->next;
				if(tmp1 == p)
					tmp2->next = NULL;
				else
					tmp2->next = tmp1;
				tmp1 = tmp2;
				tmp2 = tmp3;
			}
			p->next = tmp1;
		}
	}
}
void Sort(Node *p)
{
	if(p != NULL)
	{
		for(Node *p1 = p->next; p1 != NULL; p1 = p1->next)
		{
			for(Node *p2 = p1->next; p2 != NULL; p2 = p2->next)
			{
				if(p1->data > p2->data)
				{
					int tmp = p1->data;
					p1->data = p2->data;
					p2->data = tmp;
				}
			}
		}
	}
}
void main()
{
	int ar[] = {3,8,9,0,4,1,6,8,2,};
	int br[] = {6,13,16,2,5,4,0,11};
	int blen = sizeof(br)/sizeof(br[0]);
	int alen = sizeof(ar)/sizeof(ar[0]);

	Node head;
	Init(&head);
	Node head2;
	Init(&head2);

	for(int i = 0; i < alen; ++i)
	{
		cout<<Insert_head(&head,ar[i]);
	}
	for(int i = 0; i < blen; ++i)
	{
		cout<<Insert_head(&head2,br[i]);
	}
	cout<<endl;
	Show(&head);
	Show(&head2);
	int key;
	Sort(&head);
	Sort(&head2);

	Merge(&head,&head2);
	Show(&head);
	
	Reverse(&head);
	Show(&head);

	//Node *p1 = Find_value(&head,16);
	//Node *p2 = Find_value(&head,8);
	//p1->next = p2;

	//Show(&head2);
	//Show_fin_k(&head,4);
	//cout<<Is_round(&head)<<endl;

	//while(cin>>key,key != -1)
	//{
	//	//cout<<Delete_first_key(&head,key)<<endl;
	//	cout<<Delete_head(&head)<<endl;
	//	Show(&head);
	//}
	//while(cin>>key,key != -1)
	//{
	//	if(Find_value(&head,key))
	//		cout<<"yes"<<endl;
	//	else
	//		cout<<"no"<<endl;
	//}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值