c++双向链表

本文详细介绍了C++中如何实现双向链表,包括节点结构定义、插入、删除、遍历等操作,同时探讨了双向链表在内存管理和效率上的特点。
摘要由CSDN通过智能技术生成
#include<iostream>
using namespace std;
struct Note
{
	Note *Prev;
	Note *Next;
	int m_iData;
};

class MyListNote
{
public:
	Note *m_pHead;
	Note *m_pTail;
	//判断链表是不是空
	bool isEmpty() { return !m_pHead; };	
	int m_iCount;
	//插入数据,包括头插,尾插,及任意位置插入
	void insertDataAtNote(int index, int iData);
	//随机删除一个数	
	void DeleteDataRandomNote(int index);	
	//改变任意链表上的数据
	void ChangeDataAtNote(int index, int iData);
	//寻找某个数出现的位置,及该数出现的次数
	int FindDataAtNote(int iData,int & Num);	
	//从头到尾遍历输出
	void showHeadToTailNote();	
	//从尾到头遍历输出
	void showTailToHeadNote();			
	MyListNote() { m_pHead = m_pTail = nullptr; m_iCount = 0; };
};

void MyListNote::insertDataAtNote(int index, int iData)
{
	if (isEmpty())
	{
		Note *pNote = new Note();
		pNote->m_iData = iData;
		pNote->Next = NULL;

		m_pHead = m_pTail = pNote;
		m_iCount++;
		return;
	}

	 if (index <= 1)
	{
		Note *pNote = new Note();
		pNote->m_iData = iData;
		pNote->Next = NULL;

		pNote->Next = m_pHead;
		m_pHead->Prev = pNote;

		m_pHead = pNote;
		m_iCount++;
	}
	else if (index >= m_iCount)
	{
		Note *pNote = new Note();
		pNote->m_iData = iData;
		pNote->Next = NULL;

		m_pTail->Next = pNote;
		pNote->Prev = m_pTail;

		m_pTail = pNote;
		m_iCount++;		
	}
	else
	{
		Note *pNote = new Note();
		pNote->m_iData = iData;
		pNote->Next = NULL;
		Note *tempNote = m_pHead;
		for (int i = 0; i < index-2; i++)
		{
			tempNote = tempNote->Next;		//找到需要插入的前一个
		}
		tempNote->Next->Prev = pNote;
		pNote->Next = tempNote->Next;
		tempNote->Next = pNote;
		pNote->Prev = tempNote;
		m_iCount++;
	}
}

void MyListNote::DeleteDataRandomNote(int index)
{
	if (isEmpty())
	{
		cout << "空链" << endl;
	}
	if (index <= 1)
	{
		m_pHead = m_pHead->Next;
		delete m_pHead->Prev;
		m_pHead->Prev = NULL;
		m_iCount--;
	}
	else if (index >= m_iCount)
	{	
		m_pTail = m_pTail->Prev;
		delete m_pTail->Next;
		m_pTail->Next = NULL;
		m_iCount--;
	}
	else
	{
		Note *tempNote = m_pHead;
		for (int i = 0; i < index - 1; i++)
		{
			tempNote = tempNote->Next;
		}
		tempNote->Next->Prev = tempNote->Prev;
		tempNote->Prev->Next = tempNote->Next;
		delete tempNote;
		m_iCount--;
	}
	
}

void MyListNote::ChangeDataAtNote(int index, int iData)
{
	if (isEmpty())
	{
		cout << "这是个空链" << endl;
	}
	if (index <= 1)
	{
		m_pHead->m_iData = iData;
	}
	else if(index>=m_iCount)
	{
		m_pTail->m_iData = iData;
	}
	else
	{
		Note *tempNote = m_pHead;
		for (int i = 0; i < index-1; i++)
		{
			tempNote = tempNote->Next;
		}
		tempNote->m_iData = iData;
	}
}

int MyListNote::FindDataAtNote(int iData, int & Num)
{
	int firstPos = 0;
	firstPos = m_iCount-1;
	int Tally = 0;
	if (isEmpty())
	{
		cout << "这是个空链" << endl;
	}
	Note *tempNote = m_pHead;

	while (tempNote)
	{
		++Tally;
		if (tempNote->m_iData == iData)
		{
			if (firstPos == m_iCount - 1)
				firstPos=Tally;				//第一次找到的位置	
			++Num;						//找到iData的个数
		}
		tempNote = tempNote->Next;	
	}
	return firstPos;
}

void MyListNote::showHeadToTailNote()
{
	if (isEmpty())
	{
		cout << "这是个空链表" << endl;
	}
	else
	{
		Note *pNote = m_pHead;
		while (pNote)
		{
			cout << pNote->m_iData << endl;
			pNote = pNote->Next;
		}	
	}
}

void MyListNote::showTailToHeadNote()
{
	if (isEmpty())
	{
		cout << "这是个空链表" << endl;
	}
	else
	{
		Note *pNote = m_pTail;
		while (pNote)
		{
			cout << pNote->m_iData << endl;
			pNote = pNote->Prev;
		}
	}
}

void main()
{
	MyListNote note;
	for (int i = 0; i < 10; i++)
	{
		note.insertDataAtNote(0, i);
	}
	note.showHeadToTailNote();
	cout << "-------------" << endl;
	note.insertDataAtNote(5, 100);

	note.DeleteDataRandomNote(1);
	note.DeleteDataRandomNote(12);
	note.DeleteDataRandomNote(5);
	note.showHeadToTailNote();
	cout << "---------------" << endl;
	note.ChangeDataAtNote(5,5000);
	cout << "--------------" << endl;
	int a = 0;		//寻找函数的个数引用
	cout << "第一次出现的位置:"<<note.FindDataAtNote(4, a)<< endl;
	cout <<"这个数一共出现"<<a<<"次" << endl;
	note.showHeadToTailNote();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值