类模板之队列

         阅读(583)  2017-04-16 17:52:56

// Queue.h

#ifndef _QUEUE_H_
#define _QUEUE_H_

template<class T>
class Queue
{
public:
	Queue(int size = 10);
	~Queue();

	bool IsEmpty() const;
	bool IsFull() const;
	T First() const;
	T Last() const;
	Queue<T>& Add(const T &x);
	Queue<T>& Delete(T &x);

private:
	int front;
	int rear;
	int MaxSize;
	T *queue;
};

#endif // _QUEUE_H_


// Queue.cpp

#include "Queue.h"
#include <iostream>
using namespace std;

template<class T>
Queue<T>::Queue(int size = 10)
{
	MaxSize = size + 1;
	queue = new T[MaxSize];
	front = rear = 0;
}

template<class T>
Queue<T>::~Queue()
{
	delete [] queue;
}

template<class T>
bool Queue<T>::IsEmpty() const
{
	return (front == rear);
}

template<class T>
bool Queue<T>::IsFull() const
{
	return ( ((rear+1) % MaxSize == front) ? 1 : 0 );
}

template<class T>
T Queue<T>::First() const
{
	if (IsEmpty())
	{
		cout << "Queue is empty!" << endl;
	}

	return queue[(front+1) % MaxSize];
}

template<class T>
T Queue<T>::Last() const
{
	if (IsEmpty())
	{
		cout << "Queue is empty!" << endl;
	}

	return queue[rear];
}

template<class T>
Queue<T>& Queue<T>::Add(const T &x)
{
	if (IsFull())
	{
		cout << "Queue is full!" << endl;
		reutrn *this;
	}
	rear = (rear+1) % MaxSize;
	queue[rear] = x;

	return *this;
}

template<class T>
Queue<T>& Queue<T>::Delete(T &x)
{
	if (IsEmpty())
	{
		cout << "Queue is empty!" << endl;
		return *this;
	}
	front = (front+1) % MaxSize;
	x = queue[front];

	return *this;
}


// Queue.cpp

#include "Queue.h"
#include <iostream>
using namespace std;

template<class T>
Queue<T>::Queue(int size = 10)
{
	MaxSize = size + 1;
	queue = new T[MaxSize];
	front = rear = 0;
}

template<class T>
Queue<T>::~Queue()
{
	delete [] queue;
}

template<class T>
bool Queue<T>::IsEmpty() const
{
	return (front == rear);
}

template<class T>
bool Queue<T>::IsFull() const
{
	return ( ((rear+1) % MaxSize == front) ? 1 : 0 );
}

template<class T>
T Queue<T>::First() const
{
	if (IsEmpty())
	{
		cout << "Queue is empty!" << endl;
	}

	return queue[(front+1) % MaxSize];
}

template<class T>
T Queue<T>::Last() const
{
	if (IsEmpty())
	{
		cout << "Queue is empty!" << endl;
	}

	return queue[rear];
}

template<class T>
Queue<T>& Queue<T>::Add(const T &x)
{
	if (IsFull())
	{
		cout << "Queue is full!" << endl;
		reutrn *this;
	}
	rear = (rear+1) % MaxSize;
	queue[rear] = x;

	return *this;
}

template<class T>
Queue<T>& Queue<T>::Delete(T &x)
{
	if (IsEmpty())
	{
		cout << "Queue is empty!" << endl;
		return *this;
	}
	front = (front+1) % MaxSize;
	x = queue[front];

	return *this;
}


// main.cpp

#include "Queue.h"
#include "Queue.cpp"
#include <iostream>
using namespace std;

int main(void)
{
	Queue<int> queue(10);

	for (int i = 1; i <= 10; i++)
	{
		queue.Add(i);
	}

	cout << "first = " << queue.First() << endl;
	cout << "last = " << queue.Last() << endl;

	cin.get();
	return 0;
}

2011-08-15

文章评论

Keep it simple,stupid
文章数
284
总访问量
263234
今日访问
147
最近评论

ningto : 请到next.ningto.com里发表评论。
tujiaw : 抱歉csdn code服务关闭了,这个代码我也找不到了
于淞 : 你好,这个文章的源码能分享一下吗,songsong9181@163.com,谢谢了 上面的写错了
于淞 : 你好,这个文章的源码能分享一下吗,838106303@163.com,谢谢了 上面的链接不能用了
tujiaw : 多谢多谢
essaypinglun college-paper.org : 很好的博客,赞赞
Folly : 这个实现有点奇怪,Qt为什么没有统一的比对方法。
过多s : alert("hello, world!")
tujiaw : 还不错哦
回到顶部