C++ 链表,队列和栈示例《C++多线程编程实战》

由于用的IDE是VS2017,书中的代码不全,自己随便实现了protected的6个函数,以及继承毛病添加上了 CList::

  • 先给出项目的文件列表
    在这里插入图片描述

CList.h

#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; } //返回链表的当前个数.DWORD为unsigned long类型
	CNode<T>*& Head() { return head; } //返回链表开始节点的引用
	T* GetFirst() { return NULL != head ? 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>
CList<T>::CList(const CList& list)
{
	head = new CList<T>;
	memcpy(head, list.head, CList<T>);
	dwCount = list.dwCount;
}

template <class T>
CList<T>& CList<T>::operator= (const CList& list)
{
	dwCount = list.dwCount;
	CList<T>* temp = new CList<T>;
	memcpy(temp, list.head, CList<T>);

	delete []head;
	head = temp;
	return *this;
}

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

template <class T>
void CList<T>::Insert(T* tElement)
{
	dwCount++;
	if (NULL == head)
	{
		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 (NULL == head)
	{
		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 (NULL != lst)
	{
		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 (NULL == head)
	{
		return NULL;
	}
	if (NULL == tElement)
	{
		return GetFirst();
	}
	if (head->Element() == tElement)
	{
		return NULL != head->Next() ? head->Next()->Element() : NULL;
	}

	CNode<T>* lst = head->Next();
	while (NULL != lst)
	{
		if (lst->Element() == tElement)
		{
			return NULL != lst->Next() ? 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 // !_LIST_

CQueue.h

#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)
	{
		CList<T>::Append(CList<T>::Head(), tElement);
	}
	virtual T* Dequeue()
	{
		T* tElement = CList<T>::GetFirst();
		CList<T>::Remove(tElement);
		return tElement;
	}
	virtual T* Peek()
	{
		return CList<T>::GetFirst();
	}
	CList<T>::Count;
protected:
	CQueue(const CQueue<T>& cQueue);
	CQueue<T>& operator= (const CQueue<T>& cQueue);
};

template <class T>
CQueue<T>::CQueue(const CQueue<T>& cQueue)
{
	CList<T>::head = new CQueue<T>;
	memcpy(CList<T>::head, cQueue.head, CQueue<T>);
	CList<T>::dwCount = cQueue.dwCount;
}

template <class T>
CQueue<T>& CQueue<T>::operator= (const CQueue<T>& cQueue)
{
	CList<T>::dwCount = cQueue.dwCount;
	CQueue<T>* temp = new CQueue<T>;
	memcpy(temp, cQueue.head, CQueue<T>);

	delete []CList<T>::head;
	CList<T>::head = temp;
	return *this;
}

#endif // !__QUEUE__

CStack.h

#ifndef __STACK__
#define __STACK__

#include "CList.h"

template<class T>
class CStack : CList<T>
{
public:
	CStack() : CList<T>(){ }
	CStack(T* tElement) : CList<T>(tElement){ }
	virtual ~CStack(){ }
	virtual void Push(T* tElement)
	{
		CList<T>::Insert(tElement);
	}
	virtual T* Pop()
	{
		T* tElement = CList<T>::GetFirst();
		CList<T>::Remove(tElement);
		return tElement;
	}
	virtual T* Peek()
	{
		return CList<T>::GetFirst();
	}
	CList<T>::Count;
protected:
	CStack(const CStack<T>& cStack);
	CStack<T>& operator= (const CStack<T>& cStack);
};

template <class T>
CStack<T>::CStack(const CStack<T>& cStack)
{
	CList<T>::head = new CStack<T>;
	memcpy(CList<T>::head, cStack.head, CStack<T>);
	CList<T>::dwCount = cStack.dwCount;
}

template <class T>
CStack<T>& CStack<T>::operator= (const CStack<T>& cStack)
{
	CList<T>::dwCount = cStack.dwCount;
	CStack<T>* temp = new CStack<T>;
	memcpy(temp, cStack.head, CStack<T>);

	delete []CList<T>::head;
	CList<T>::head = temp;
	return *this;
}

#endif // !__STACK__

LinkedList.cpp

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

运行结果

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值