6把二级链表展开成一级单链表

//有个二级单链表,其中每个元素都含有一个指向一个单链表的指针。写程序把这个二级链表展开称一级单链表

#include <iostream>
using namespace std;

struct LinkNode
{
	int nValue;
	LinkNode *pNext;
	LinkNode(int Value = 0):nValue(Value),pNext(NULL){}
};

class Linklist;
class CascadeList
{
public:
	friend class Linklist;
	CascadeList(int n = 10);
	~CascadeList() { delete []pArr; }
	void InsertValue(int n, int nValue);
	void Print();
private:
	int nLen;
	LinkNode* pArr;
};


CascadeList::CascadeList(int n)
{
	if (n <= 0)
	{
		return;
	}
	nLen = n;
	pArr = new LinkNode[nLen];
}

void CascadeList::InsertValue(int n, int nValue)
{
	if (n <= 0 || n> nLen)
	{
		return;
	}
	LinkNode *pCur = &pArr[n - 1];
	while (pCur->pNext)
	{
		pCur = pCur->pNext;
	}
	LinkNode *pTmp = new LinkNode(nValue);
	pCur->pNext = pTmp;
}

void CascadeList::Print()
{
	if (nLen <= 0)
	{
		return;
	}
	int i;
	for (i = 0; i < nLen; i++)
	{
		LinkNode *pCur = pArr[i].pNext;
		while (pCur)
		{
			cout<<pCur->nValue<<" ";
			pCur = pCur->pNext;
		}
		cout<<endl;
	}
}

class Linklist
{
public:
	Linklist(){ pHead = new LinkNode(); };
	~Linklist(){ delete pHead;}; 
	void InsertValue(int nValue);
	void Reverse();
	void Reverse2();
	LinkNode *FindLastNum(int nNum);
	LinkNode *FindMidNode(bool &IsOdd);
	void DeleteNode(LinkNode *pDel);
	Linklist* Mergelist(const Linklist &list) const;
	void CascadeToList(const CascadeList &clist);
	void Print();

private:
	LinkNode *pHead;
};


void Linklist::InsertValue(int nValue)
{
	if (pHead == NULL)
	{
		return;
	}
	LinkNode *pCur = pHead;
	while (pCur->pNext != NULL)
	{
		pCur = pCur->pNext;
	}

	LinkNode *pTmp = new LinkNode(nValue);
	pCur->pNext = pTmp;
}

void Linklist::Reverse()
{
	if (pHead == NULL)
	{
		return;
	}

	//特殊情况:如果链表为空或者只有一个元素,直接返回
	if (pHead->pNext == NULL || pHead->pNext->pNext == NULL)
	{
		return;
	}

	LinkNode *pPre = pHead;
	LinkNode *pCur = pHead->pNext;
	LinkNode *pNext;
	
	while (pCur != NULL)
	{
		pNext = pCur->pNext;
		pCur->pNext = pPre;
		pPre = pCur;
		pCur = pNext;
	}
	
	pHead->pNext->pNext = NULL;
	pHead->pNext = pPre;
}

void Linklist::Reverse2()
{
	if (pHead == NULL)
	{
		return;
	}

	//特殊情况:如果链表为空或者只有一个元素,直接返回
	if (pHead->pNext == NULL || pHead->pNext->pNext == NULL)
	{
		return;
	}

	LinkNode *pCur = pHead->pNext;
	LinkNode *pNext = NULL;
	LinkNode *pNextNext = NULL;
	
	while (pCur->pNext != NULL)
	{
		pNext = pCur->pNext;
		pNextNext = pNext->pNext;
		
		pNext->pNext = pHead->pNext;
		pHead->pNext = pNext;
		pCur->pNext = pNextNext;
	}
}

LinkNode *Linklist::FindLastNum(int nNum)
{
	if (pHead == NULL)
	{
		return NULL;
	}
	LinkNode *pFirst = pHead;
	int i = 0;
	while (i < nNum)
	{
		if (pFirst == NULL)
		{
			return NULL;
		}
		pFirst = pFirst->pNext;
		i++;
	}
	LinkNode *pSecond = pHead;
	while (pFirst != NULL)
	{
		pFirst = pFirst->pNext;
		pSecond = pSecond->pNext;
	}

	return pSecond;
}

//考虑链表个数为奇数Odd、偶数Even两种情况
LinkNode* Linklist::FindMidNode(bool &IsOdd)
{
	if (pHead == NULL)
	{
		return NULL;
	}
	LinkNode *pFirst = pHead;
	LinkNode *pSecond = pHead;

	while (pFirst != NULL && pFirst->pNext != NULL)
	{
		pFirst = pFirst->pNext->pNext;
		pSecond = pSecond->pNext;
	}
	if (pFirst == NULL)
	{
		IsOdd = true;
	}
	else
	{
		IsOdd = false;
	}
	return pSecond;
}

void Linklist::DeleteNode(LinkNode *pDel)
{
	if (pHead == NULL || pDel == NULL)
	{
		return;
	}

	//如果要删除的节点不是最后一个节点
	if (pDel->pNext != NULL)
	{
		pDel->nValue = pDel->pNext->nValue;
		LinkNode *pTmp = pDel->pNext;
		pDel->pNext = pTmp->pNext;
		
		delete pTmp;
	}
	else
	{
		LinkNode *pCur = pHead->pNext;
		while (pCur != NULL)
		{
			if (pCur->pNext = pDel)
			{
				pCur->pNext = NULL;
				delete pDel;
			}
			else
			{
				pCur = pCur->pNext;
			}
		}
	}
}

Linklist*  Linklist::Mergelist(const Linklist &list) const
{
	Linklist *pList = new Linklist;
	if (pList == NULL || pHead == NULL || list.pHead == NULL)
	{
		return NULL;
	}

	LinkNode *pCur1 = pHead->pNext;
	LinkNode *pCur2 = list.pHead->pNext;
	LinkNode *pCur3 = pList->pHead;

	while (pCur1 && pCur2)
	{
		if (pCur1->nValue > pCur2->nValue)
		{
			pList->InsertValue(pCur2->nValue);
			pCur2 = pCur2->pNext;
		}
		else
		{
			pList->InsertValue(pCur1->nValue);
			pCur1 = pCur1->pNext;
		}
	}
	while (pCur1)
	{
		pList->InsertValue(pCur1->nValue);
		pCur1 = pCur1->pNext;
	}
	while (pCur2)
	{
		pList->InsertValue(pCur2->nValue);
		pCur2 = pCur2->pNext;
	}

	return pList;
}

void Linklist::CascadeToList(const CascadeList &clist)
{
	int i;
	if ( pHead == NULL|| clist.nLen <= 0)
	{
		return;
	}
	for (i = 0; i < clist.nLen; i++)
	{
		LinkNode *pCur = clist.pArr[i].pNext;
		while (pCur)
		{
			InsertValue(pCur->nValue);
			pCur = pCur->pNext;
		}
	}
}
void Linklist::Print()
{
	if (pHead == NULL)
	{
		return;
	}
	LinkNode *pCur = pHead->pNext;
	while (pCur != NULL)
	{
		cout<<pCur->nValue<<" ";
		pCur = pCur->pNext;
	}
	cout<<endl;
}


int main()
{
	CascadeList clist(10);
	clist.InsertValue(1, 1);
	clist.InsertValue(1, 2);
	clist.InsertValue(2, 3);
	clist.InsertValue(3, 4);
	clist.InsertValue(4, 5);
	clist.InsertValue(5, 6);
	clist.InsertValue(6, 7);
	clist.InsertValue(7, 8);
	clist.InsertValue(8, 9);
	clist.InsertValue(9, 10);
	clist.InsertValue(10, 11);
	clist.InsertValue(11, 12);
	clist.Print();

	Linklist list;
	list.CascadeToList(clist);
	list.Print();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值