链表各种基本操作

简介

        假结点:不存放数据的节点,使用假结点方便对链表的操作

        在这里使用一个假结点(myHead)作为链表头部,但是链表的实际有效头部是假结点的下一

项((myHead->next),将假结点的下一项(myHead->next)作为链表的下标为0的位置

链表各种基本操作_第1张图片

 

定义

struct ListNode
{
	//数据域
	int val;
	//指针域
	struct ListNode* next;
	//构造函数
	struct ListNode(int val,struct ListNode*next)
		:val(val),next(next)
	{

	}
};

1)链表长度

//求链表的长度
int length(ListNode*myHead)
{
	int len = 0;
	for (ListNode*cur=myHead->next;cur!=nullptr;cur=cur->next)
	{
		len++;
	}
	return len;
}

2)判断链表是否为空 

//判断链表是否为空
bool empty(ListNode*myHead)
{
	return myHead->next == nullptr;
}

3)插入

  3.1)头插

//头部插入数据
void push_front(ListNode*myHead,int data)
{
	myHead->next= new ListNode(data,myHead->next);
}

  3.2)尾插

//尾部插入数据
void push_back(ListNode*myHead,int data)
{
	ListNode* cur = myHead;
	while (cur->next != nullptr)
	{
		cur = cur->next;
	}
	cur->next = new ListNode(data,nullptr);
}

  3.3)指定位置插

//指定位置插入
bool insert(ListNode*myHead,int pos,int data)
{
	if (pos<0 || pos>length(myHead))
	{
		cout << "插入失败,位置不合法......" << endl;
		return false;
	}
	else
	{
		//找到目标位置的前一个节点
		ListNode* pre = myHead;
		for (int p=0;pnext;
		}
		pre->next = new ListNode(data,pre->next);
		return true;
	}
}

4)获取指定下标数据

//获取特定位置的数据
int getPosData(ListNode*myHead,int pos)
{
	if (pos<0 || pos>=length(myHead))
	{
		cout << "获取失败,位置不合法......" << endl;
		return -1;
	}
	else
	{
		ListNode* cur = myHead->next;
		for (int i=0;inext;
		}
		return cur->val;
	}
}

5)删除

  5.1)删除头部

//删除头部节点
bool delete_front(ListNode*myHead)
{
	if (empty(myHead)==true)
	{
		cout << "链表为空,删除头部失败......" << endl;
		return false;
	}
	else
	{
		ListNode* delNode = myHead->next;
		myHead->next = delNode->next;
		delete delNode;
		return true;
	}
}

  5.2)删除尾部

//删除尾部节点
bool delete_back(ListNode*myHead)
{
	if (empty(myHead) == true)
	{
		cout << "链表为空,删除尾部失败......" << endl;
		return false;
	}
	else
	{
		ListNode* pre = myHead;
		while (pre->next->next!=nullptr)
		{
			pre = pre->next;
		}
		ListNode* delNode = pre->next;
		pre->next = nullptr;
		delete delNode;
		return true;
	}
}

  5.3)删除任意位置

//删除指定位置节点
bool delete_pos(ListNode*myHead,int pos)
{
	if (pos < 0 || pos >= length(myHead))
	{
		cout << "删除失败,位置不合法......" << endl;
		return false;
	}
	else
	{
		ListNode* cur = myHead;
		while (pos>0)
		{
			cur = cur->next;
		}
		ListNode* delNode = cur->next;
		cur->next = delNode->next;
		delete delNode;
		return true;
	}
}

  5.4)删除特定值

//删除特定值的节点
void delete_value(ListNode*myHead,int value)
{
	for (ListNode*cur=myHead;cur->next!=nullptr;)
	{
		if (cur->next->val!=value)
		{
			cur = cur->next;
		}
		else
		{
			ListNode* delNode = cur->next;
			cur->next = delNode->next;
			delete delNode;
		}
	}
}

6)查找

//搜索某个值在链表中的位置
int search(ListNode*myHead,int data)
{
	int pos = 0;
	for (ListNode*cur=myHead->next;cur!=nullptr;cur=cur->next)
	{
		if (cur->val == data)
			return pos;
		pos++;
	}
	cout << "未找到相关信息......" << endl;
	return -1;
}

7)修改指定位置数据

//修改指定位置的数据
bool modify(ListNode*myHead,int pos,int setVal)
{
	if (pos < 0 || pos >= length(myHead))
	{
		cout << "修改失败,位置不合法......" << endl;
		return false;
	}
	else
	{
		ListNode* cur = myHead->next;
		while (pos>0)
		{
			cur = cur->next;
			pos--;
		}
		cur->val = setVal;
		return true;
	}
}

8)反转

//反转链表
ListNode* reverse(ListNode*myHead)
{
	ListNode* pre = nullptr;
	ListNode* cur = myHead->next;
	while (cur != nullptr)
	{
		ListNode* Next = cur->next;
		cur->next = pre;
		pre = cur;
		cur = Next;
	}
	myHead->next = pre;
	return myHead;
}

9)排序

//对链表从小到大排序
ListNode*sort(ListNode*myHead)
{
	for (ListNode*cur1=myHead->next;cur1!=nullptr;cur1=cur1->next)
	{
		for (ListNode*cur2=cur1->next;cur2!=nullptr;cur2=cur2->next)
		{
			if (cur2->valval)
			{
				int t = cur1->val;
				cur1->val = cur2->val;
				cur2->val=t;
			}
		}
	}
	return myHead;
}

10)合并

//将两个升序链表合并
ListNode* merge(ListNode*myHead1,ListNode*myHead2)
{
	ListNode* myHead = new ListNode(-1,nullptr);
	ListNode* cur = myHead;

	ListNode* cur1 = myHead1->next;
	ListNode* cur2 = myHead2->next;
	while (cur1 != nullptr && cur2 != nullptr)
	{
		if (cur1->valval)
		{
			cur->next = cur1;
			cur = cur->next;
			cur1 = cur1->next;
		}
		else
		{
			cur->next = cur2;
			cur = cur->next;
			cur2 = cur2->next;
		}
	}
	cur->next = (cur1 != nullptr) ? cur1 : cur2;
	delete myHead1;
	delete myHead2;
	return myHead;
}

11)销毁

//销毁链表
void delete_list(ListNode*list)
{
	ListNode* cur = list;
	while (cur!=nullptr)
	{
		ListNode* delNode = cur;
		cur = cur->next;
		delete delNode;
	}
}

 

12)遍历输出

//遍历,打印链表
void printList(ListNode*myHead)
{
	for (ListNode*cur=myHead->next;cur!=nullptr;cur=cur->next)
	{
		cout << cur->val << " ";
	}
	cout << endl;
}

你可能感兴趣的:(数据结构与算法,链表,数据结构)