数据结构与算法1--单链表常见操作

数据结构与算法1--单链表常见操作

 

单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。以下是笔者根据单链表结构实现的几个常见功能,后续将添加常见其它功能。

 

1、功能

01-新建链表方法1
02-新建链表方法2
03-追加链表
04-删除链表中某个节点
05-清空链表
06-销毁链表
07-链表长度
08-打印链表
09-输出链表中倒数第k个节点
10-反转链表

 

2、代码

/*
该文件夹包含list的增删查改,以及常见list算法的实现
*/
#include <iostream>
#include <vector>
using namespace std;
typedef struct Node{
	int val;
	Node* next;
}ListNode;

/*Menu
01-新建链表方法1
02-新建链表方法2
03-追加链表
04-删除链表中某个节点
05-清空链表
06-销毁链表
07-链表长度
08-打印链表
09-输出链表中倒数第k个节点
10-反转链表
*/

ListNode* CreateListByNum(int n);//01
ListNode* CreateListByVector(vector<int>&v);//02
void AppendToTail(ListNode **pHead, int val);//03
void DeleteNode(ListNode **pHead, int val);//04
void ClearList(ListNode *pHead);//05
void DestroyList(ListNode **pHead);//06
int GetLength(ListNode *pHead);//07
void PrintList(ListNode *pHead);//08
void PrintKthNodeToTail(ListNode *pHead, int k);//09
ListNode* ReverseList(ListNode* pHead);//10

//01-新建链表方法1,节点依次为	1->2->3-···->n
ListNode* CreateListByNum(int n)
{
	if(n<=0)
		return NULL;
	ListNode *pHead = new ListNode();
	pHead->val = 1;
	pHead->next = NULL;
	ListNode *p = pHead;
	for(int i=2;i<=n;i++)
	{
		ListNode *tmp = new ListNode();
		tmp->val = i;
		tmp->next = NULL;
		p->next = tmp;
		p = p->next;
	}
	return pHead;
}
void TestCreateListByNum()
{
	ListNode *pHead = CreateListByNum(10);
	PrintList(pHead);
}
//02-新建链表方法2,节点依次为	v[0]->v[1]->v[2]-···->v[v.size()-1]
ListNode* CreateListByVector(vector<int>&v)
{
	if(v.size()==0)
		return NULL;
	ListNode *pHead = new ListNode();
	pHead->val = v[0];
	pHead->next = NULL;
	ListNode *p = pHead;
	for(int i=1;i<v.size();i++)
	{
		ListNode *tmp = new ListNode();
		tmp->val = v[i];
		tmp->next = NULL;
		p->next = tmp;
		p = p->next;
	}
	return pHead;
}
void TestCreateListByVector()
{
	int arr[10] = {1,2,3,4,5,6,7,8,9,10};
	vector<int>v(arr,arr+10);
	ListNode *pHead = CreateListByVector(v);
	PrintList(pHead);
}
//03-追加链表,追加val到list尾部
void AppendToTail(ListNode **pHead,int val)
{
	ListNode *tmp = new ListNode();
	tmp->val = val;
	tmp->next = NULL;
	if(*pHead==NULL)
	{
		*pHead = tmp;//若传入参数为ListNode *pHead,则无法修改pHead
	}else
	{
		ListNode *cur = *pHead;
		while(cur->next!=NULL)//尾节点的next为NULL
		{
			cur = cur->next;
		}
		cur->next = tmp;
	}
}
void TestAppendToTail()
{
	ListNode *pHead = NULL;
	PrintList(pHead);
	AppendToTail(&pHead,0);
	PrintList(pHead);
	ListNode *pHead1 = CreateListByNum(10);
	PrintList(pHead1);
	AppendToTail(&pHead1,11);
	PrintList(pHead1);
}
//04-删除链表中某个节点
void DeleteNode(ListNode **pHead, int val)
{
	if(pHead==NULL || *pHead==NULL)
		return ;
	ListNode *p = NULL;
	if((*pHead)->next==NULL && (*pHead)->val==val) //删除唯一的头节点
	{
		p = *pHead;
		delete p;
		*pHead = NULL;
		return ;
	}
	ListNode *cur = *pHead;
	while(cur!=NULL)
	{
		if(cur->next!=NULL)
			p = cur;//若某次pHead刚好为要删除的最后一个节点,则p刚好保存其前一个节点
		if(cur->val==val && cur->next!=NULL)//若pHead不为尾节点,使用其下一个节点覆盖当前节点,然后删除其下一个节点即可
		{
			ListNode *pNode = cur->next;
			cur->val = pNode->val;
			cur->next = pNode->next;
			delete pNode;
			break;//此处只删除一个节点
		}else if(cur->val==val && cur->next==NULL)//若pHead为尾节点,删除该节点,令其前一个节点next为NULL
		{
			ListNode *pNode = cur;
			delete pNode;
			p->next = NULL;
			break;//删除后退出循环
		}else
		{
			cur = cur->next;
		}
	}
}
void TestDeleteNode()
{
	ListNode *pHead = CreateListByNum(10);
	PrintList(pHead);
	DeleteNode(&pHead,1);
	PrintList(pHead);
	DeleteNode(&pHead,5);
	PrintList(pHead);
	DeleteNode(&pHead,10);
	PrintList(pHead);
}
//05-清空链表,头节点保留,其它节点全部删除
void ClearList(ListNode *pHead)
{
	if(pHead==NULL)
		return;
	while(pHead->next!=NULL)
	{
		ListNode *tmp = pHead->next->next;
		delete pHead->next;
		pHead->next = tmp;
	}
}
void TestClearList()
{
	ListNode *pHead = CreateListByNum(10);
	PrintList(pHead);
	ClearList(pHead);
	PrintList(pHead);
}
//06-销毁链表,包括头节点在内全部删除
void DestroyList(ListNode **pHead)
{
	if(*pHead==NULL || pHead==NULL)
		return;
	while(*pHead!=NULL)
	{
		ListNode *tmp = (*pHead)->next;
		delete *pHead;
		*pHead = tmp;
	}
}
void TestDestroyList()
{
	ListNode *pHead = CreateListByNum(10);
	PrintList(pHead);
	DestroyList(&pHead);
	PrintList(pHead);
	cout<<"Length: "<<GetLength(pHead)<<endl;
}
//07-链表长度
int GetLength(ListNode *pHead)
{
	int ret = 0;
	while(pHead!=NULL)
	{
		pHead = pHead->next;
		ret ++;
	}
	return ret;
}
void TestGetLength()
{
	ListNode *pHead = CreateListByNum(10);
	PrintList(pHead);
	cout<<"Length: "<<GetLength(pHead)<<endl;
}
//08-打印链表
void PrintList(ListNode *pHead)
{
	if(pHead==NULL)
		cout<<"Null List"<<endl;
	else{
		while(pHead!=NULL)
		{
			pHead->next==NULL?(cout<<pHead->val<<endl):(cout<<pHead->val<<"->");
			pHead = pHead->next;
		}
	}
}
void TestPrintList()
{
	ListNode *pHead = CreateListByNum(10);
	PrintList(pHead);
}
//09-输出链表中倒数第k个节点
void PrintKthNodeToTail(ListNode *pHead, int k)
{
	int len = GetLength(pHead);
	if(k>len || k<0)
	{
		cout<<"K is improper"<<endl;
		return;
	}
	ListNode *p1 = pHead;
	for(int i=1;i<=k-1;i++)
		p1 = p1->next;
	ListNode *p2 = pHead;
	while(p1->next!=NULL)
	{
		p1 = p1->next;
		p2 = p2->next;
	}
	cout<<p2->val<<endl;
}
void TestPrintKthNodeToTail()
{
	ListNode *pHead = CreateListByNum(10);
	PrintList(pHead);
	PrintKthNodeToTail(pHead,1);
	PrintKthNodeToTail(pHead,5);
	PrintKthNodeToTail(pHead,10);
}
//10-反转链表
ListNode* ReverseList(ListNode* pHead)
{
	if(pHead==NULL)
		return NULL;
	if(pHead->next==NULL)
		return pHead;
	ListNode *pre = NULL;//保存当前节点的前一个节点
	ListNode *cur = pHead;//保存当前节点
	ListNode *pHeadReverse = NULL;//保存反转节点的头节点
	while(cur!=NULL)
	{
		ListNode *pNext = cur->next;//保存当前节点的下一个节点,以防更改cur的next节点后链表断开
		if(pNext==NULL)//尾节点为反转节点的头节点
			pHeadReverse = cur;
		cur->next = pre;//将当前节点下一个节点指向其前一个节点
		pre = cur;//cur节点的next链接好后,将其pre位置向后移动
		cur = pNext;//pre节点链接好并后移一个节点后,将cur节点向后移动
	}
	return pHeadReverse;
}
void TestReverseList()
{
	ListNode *pHead = CreateListByNum(10);
	PrintList(pHead);
	ListNode *pReverse = ReverseList(pHead);
	PrintList(pReverse);
}

int main(int argc, char const *argv[])
{
	/* code */
// 01-新建链表方法1
	TestCreateListByNum();
// 02-新建链表方法2
	//TestCreateListByVector();
// 03-追加链表
	//TestAppendToTail();
// 04-删除链表中某个节点
	//TestDeleteNode();
// 05-清空链表
	//TestClearList();
// 06-销毁链表
	//TestDestroyList();
// 07-链表长度
	//TestGetLength();
// 08-打印链表
	//TestPrintList();
// 09-输出链表中倒数第k个节点
	//TestPrintKthNodeToTail();
// 10-反转链表
	//TestReverseList();
	return 0;
}

 

3、说明

当前已在mingw32(gcc 4.9.2)上测试通过。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

昕光xg

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

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

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

打赏作者

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

抵扣说明:

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

余额充值