码迷,mamicode.com
首页 > 编程语言 > 详细

利用C++实现一个链表结构

时间:2020-02-12 11:08:44      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:include   oid   cpp   windows   编程   链表   delete   queue   耦合   

利用C++实现链表结构

1.定义链表的数据结构

 

技术图片

 

 

 

 

 

 

 

CList作为一个链表类,它的成员是由CNode组成

CNode有两个属性,tElement用于指向当前的节点,next用于指向下一个节点

 

/********************************************************
**	CList链表结构实现
**
**
**
***********************************************************/


#ifndef _LIST_
#define _LIST_

#include <Windows.h>

template <class T>  //利用泛型编程笔迷避免链表节点与链表耦合
//链表节点
class CNode
{
public:
	CNode(T* tElement) : tElement(tElement), next(0) { }
	T* Element() const { return tElement; }
	CNode*& Next(){ return next; }
private:
	T* tElement;//用于指示当前节点
	CNode* next;//用于指示下一个节点
};
template <class T>
class CList
{
public:
	CList() : dwCount(0), head(0){ }
	CList(T* tElement) : dwCount(1), head(new CNode<T>(tElement)){ }
	virtual ~CList(){ }
	void Append(CNode<T>*& node, T* tElement);//添加
	void Insert(T* tElement);//插入
	bool Remove(T* tElement);//溢出
	DWORD Count() const { return dwCount; }//节点数量
	CNode<T>*& Head() { return head; }//节点头
	T* GetFirst(){ return head != NULL ? head->Element() : NULL; }
	T* GetLast();
	T* GetNext(T* tElement);
	T* Find(DWORD(*Function)(T* tParameter), DWORD dwValue);
protected:
	CList(const CList& list);
	CList& operator = (const CList& list);
private:
	CNode<T>* head;
	DWORD dwCount;
};

template <class T>
void CList<T>::Append(CNode<T>*& node, T* tElement)
{
	if (node == NULL)
	{
		dwCount++;
		node = new CNode<T>(tElement);

		return;
	}

	Append(node->Next(), tElement);
}

template <class T>
void CList<T>::Insert(T* tElement)
{
	dwCount++;
	if (head == NULL)
	{
		head = new CNode<T>(tElement);
		return;
	}

	CNode<T>* tmp = head;
	head = new CNode<T>(tElement);
	head->Next() = tmp;
}

template <class T>
bool CList<T>::Remove(T* tElement)
{
	if (head == NULL)
	{
		return NULL;
	}

	if (head->Element() == tElement)
	{
		CNode<T>* tmp = head;
		head = head->Next();

		delete tmp;
		dwCount--;

		return true;
	}

	CNode<T>* tmp = head;
	CNode<T>* lst = head->Next();

	while (lst != NULL)
	{
		if (lst->Element() == tElement)
		{
			tmp->Next() = lst->Next();

			delete lst;
			dwCount--;

			return true;
		}

		lst = lst->Next();
		tmp = tmp->Next();
	}

	return false;
}

template <class T>
T* CList<T>::GetLast()
{
	if (head)
	{
		CNode<T>* tmp = head;
		while (tmp->Next())
		{
			tmp = tmp->Next();
		}
		return tmp->Element();
	}

	return NULL;
}

template <class T>
T* CList<T>::GetNext(T* tElement)
{
	if (head == NULL)
	{
		return NULL;
	}

	if (tElement == NULL)
	{
		return GetFirst();
	}

	if (head->Element() == tElement)
	{
		return head->Next() != NULL ? head->Next()->Element() : NULL;
	}

	CNode<T>* lst = head->Next();

	while (lst != NULL)
	{
		if (lst->Element() == tElement)
		{
			return lst->Next() != NULL ? lst->Next()->Element() : NULL;
		}

		lst = lst->Next();
	}

	return NULL;
}

template <class T>
T* CList<T>::Find(DWORD(*Function)(T* tParameter), DWORD dwValue)
{
	try
	{
		T* tElement = NULL;
		while (tElement = GetNext(tElement))
		{
			if (Function(tElement) == dwValue)
			{
				return tElement;
			}
		}
	}
	catch (...) {}

	return NULL;
}

#endif

  

 

 

 

CQueue定义代码如下

#ifndef __QUEUE__
#define __QUEUE__

#include "CList.h"

template<class T>
class CQueue : CList<T>
{
public:
	CQueue() : CList<T>(){ }
	CQueue(T* tElement) : CList<T>(tElement){ }
	virtual ~CQueue(){ }
	virtual void Enqueue(T* tElement)
	{
		Append(Head(), tElement);
	}
	virtual T* Dequeue()
	{
		T* tElement = GetFirst();
		Remove(tElement);
		return tElement;
	}
	virtual T* Peek()
	{
		return GetFirst();
	}
	CList<T>::Count;
protected:
	CQueue(const CQueue<T>& cQueue);
	CQueue<T>& operator = (const CQueue<T>& cQueue);
};

#endif

  

技术图片

 

 

 

#include <iostream>

using namespace std;

#include "CQueue.h"
#include "CStack.h"

int _main()
{
	CQueue<int>* cQueue = new CQueue<int>();
	CStack<double>* cStack = new CStack<double>();

	for (int i = 0; i < 10; i++)
	{
		cQueue->Enqueue(new int(i));
		cStack->Push(new double(i / 10.0));
	}

	cout << "Queue - integer collection:" << endl;
	for (; cQueue->Count();)
	{
		cout << *cQueue->Dequeue() << " ";
	}

	cout << endl << endl << "Stack - double collection:" << endl;
	for (; cStack->Count();)
	{
		cout << *cStack->Pop() << " ";
	}

	delete cQueue;
	delete cStack;

	cout << endl << endl;
	return system("pause");
}

  

 

利用C++实现一个链表结构

标签:include   oid   cpp   windows   编程   链表   delete   queue   耦合   

原文地址:https://www.cnblogs.com/yuanshijie/p/12297995.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!