c++双向链表

#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();
}

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页