怎么定义队列类模板(c++)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么定义队列类模板(c++)相关的知识,希望对你有一定的参考价值。
有道题目 是要求定义队列类模板和数列类模板 求高手
#pragma once//用数组实现的循环队列
//SqQueue.h
#include "stdafx.h"
#include "Windows.h"
#include "stdlib.h"
#include "iostream.h"
template<class T>
class SqQueue
private:
T *queue;
int front,rear,MaxSize;
public:
SqQueue(int sz=30);//构造函数
SqQueue(SqQueue& sq);//拷贝构造函数
SqQueue(int arraysz,T *a,int sz=30);//构造函数
void MakeEmpty();//清空队列
bool IsEmpty();//队列是否为空
int GetLength();//获得长度
int GetMaxSize();//获得队列大小
T &GetFront();//获得队首元素
bool EnQueue(T &x);//入队
bool OutQueue(T &x);//出对
void show();
~SqQueue();//析构函数
;
template<class T>
SqQueue<T>::SqQueue(int sz)
queue=new T[sz];
MaxSize=sz;
front=rear=0;
template<class T>
SqQueue<T>::SqQueue(SqQueue& sq)
queue=new T[sq.MaxSize];
MaxSize=sq.MaxSize;
front=rear=0;
for(int i=0;i<sq.GetLength();i++)
queue[i]=sq.queue[i];
rear=(rear+1)%MaxSize;
template<class T>
SqQueue<T>::SqQueue(int arraysz,T *a,int sz)
if(arraysz>sz)
cerr<<"The Array is too Large";
exit(1);
queue=new T[sz];
MaxSize=sz;
front=rear=0;
for(int i=0;i<arraysz;i++)
queue[i]=a[i];
rear=(rear+1)%MaxSize;
template<class T>
void SqQueue<T>::MakeEmpty()
front=rear=0;
template<class T>
bool SqQueue<T>::IsEmpty()
return front==rear;
template<class T>
int SqQueue<T>::GetLength()
return ((rear-front)+MaxSize)%MaxSize;
template<class T>
int SqQueue<T>::GetMaxSize()
return MaxSize;
template<class T>
T & SqQueue<T>::GetFront()
if(IsEmpty())
MessageBox(0,"Empty Queue!",0,0);
exit(1);
return queue[front];
template<class T>
bool SqQueue<T>::EnQueue(T &x)
if((rear+1)%MaxSize==front)
MessageBox(0,"Queue is Full",0,0);
return 0;
queue[rear]=x;
rear=(rear+1)%MaxSize;
template<class T>
bool SqQueue<T>::OutQueue(T &x)
if(rear==front)
MessageBox(0,"Queue is Empty",0,0);
return 0;
x=queue[front];
front=(front+1)%MaxSize;
template<class T>
void SqQueue<T>::show()
while(front!=rear)
cout<<queue[front]<<" ";
front=(front+1)%MaxSize;
template<class T>
SqQueue<T>::~SqQueue()
delete []queue;
rear=front;
..............................
//用链表实现的循环队列
//LinkQueueNode.h
#pragma once
//链队结点类
#include "stdafx.h"
template<class T>
class LinkQueueNode
private:
LinkQueueNode<T>* next;
T data;
public:
LinkQueueNode(T &x):data(x),next(0)
void SetData(T &x)data=x;
void SetLink(LinkQueueNode<T>* p)next=p;
LinkQueueNode<T>* GetLink()return next;
T & GetData()return data;
;
...............
#pragma once
//链队类
//LinkQueue.h
#include "stdafx.h"
#include "LinkQueueNode.h"
#include "Windows.h"
#include "stdlib.h"
#include "iostream.h"
template<class T>
class LinkQueue
private:
LinkQueueNode<T>* front,*rear;
public:
LinkQueue():front(0),rear(0)//构造函数
LinkQueue(LinkQueue<T> &lq);//拷贝构造函数
virtual void InitQueue(int n,T *arr);//初始化队列
virtual int GetLength();//获得长度
virtual bool EnQueue(T &x);//入队
virtual bool OutQueue(T &x);//出队
bool IsEmpty();//队列是否为空
LinkQueueNode<T>* GetFront();//获得队首指针
void MakeEmpty();//清空队列
//输出队列
friend ostream& operator<<(ostream& output,LinkQueue<T>& lq);
virtual ~LinkQueue();//析构函数
;
template<class T>
LinkQueue<T>::LinkQueue(LinkQueue<T> &lq)
front=rear=0;
LinkQueueNode<T>* p=lq.front;
while(p)
LinkQueueNode<T>* newp=new LinkQueueNode<T>(p->GetData());
if(!newp)
MessageBox(0,"Allocate Failed!",0,0);
exit(1);
if(IsEmpty())
front=rear=newp;
rear->SetLink(newp);
rear=newp;
p=p->GetLink();
template<class T>
void LinkQueue<T>::InitQueue(int n,T* a)
if(n<0)
MessageBox(0,"Wrong Number",0,0);
return;
int i=0;
while(i<n)
LinkQueueNode<T>* p=new LinkQueueNode<T>(a[i]);
if(IsEmpty())
front=rear=p;
else
rear->SetLink(p);
rear=p;
i++;
template<class T>
int LinkQueue<T>::GetLength()
int Length=0;
LinkQueueNode<T>* p=front;
while(p)
p=p->GetLink();
Length++;
return Length;
template<class T>
bool LinkQueue<T>::EnQueue(T &x)
LinkQueueNode<T>* p=new LinkQueueNode<T>(x);
if(!p)
MessageBox(0,"Allocate Failed!",0,0);
return 0;
if(!rear&&!front)
front=rear=p;
else
rear->SetLink(p);
rear=rear->GetLink();
return 1;
template<class T>
bool LinkQueue<T>::OutQueue(T &x)
if(IsEmpty())
return 0;
LinkQueueNode<T>* p=front;
if(front==rear)
rear=0;
front=front->GetLink();
x=p->GetData();
delete p;
return 1;
template<class T>
bool LinkQueue<T>::IsEmpty()
if(front==0&&rear==0)
return 1;
else return 0;
template<class T>
LinkQueueNode<T>* LinkQueue<T>::GetFront()
return front;
template<class T>
void LinkQueue<T>::MakeEmpty()
T x;
while(front)
OutQueue(x);
rear=0;
template<class T>
ostream& operator<<(ostream& output,LinkQueue<T> &lq)
if(lq.IsEmpty())
output<<"The Queue is Empty!"<<endl;
exit(1);
output<<"The Queue Members are below:"<<endl;
LinkQueueNode<T>* p=lq.front;
while(p)
output<<p->GetData()<<" ";
p=p->GetLink();
output<<endl;
return output;
template<class T>
LinkQueue<T>::~LinkQueue()
MakeEmpty();
参考技术A #pragma
once
//用数组实现的循环队列
//SqQueue.h
#include
"stdafx.h"
#include
"Windows.h"
#include
"stdlib.h"
#include
"iostream.h"
template<class
T>
class
SqQueue
private:
T
*queue;
int
front,rear,MaxSize;
public:
SqQueue(int
sz=30);//构造函数
SqQueue(SqQueue&
sq);//拷贝构造函数
SqQueue(int
arraysz,T
*a,int
sz=30);//构造函数
void
MakeEmpty();//清空队列
bool
IsEmpty();//队列是否为空
int
GetLength();//获得长度
int
GetMaxSize();//获得队列大小
T
&GetFront();//获得队首元素
bool
EnQueue(T
&x);//入队
bool
OutQueue(T
&x);//出对
void
show();
~SqQueue();//析构函数
;
template<class
T>
SqQueue<T>::SqQueue(int
sz)
queue=new
T[sz];
MaxSize=sz;
front=rear=0;
template<class
T>
SqQueue<T>::SqQueue(SqQueue&
sq)
queue=new
T[sq.MaxSize];
MaxSize=sq.MaxSize;
front=rear=0;
for(int
i=0;i<sq.GetLength();i++)
queue[i]=sq.queue[i];
rear=(rear+1)%MaxSize;
template<class
T>
SqQueue<T>::SqQueue(int
arraysz,T
*a,int
sz)
if(arraysz>sz)
cerr<<"The
Array
is
too
Large";
exit(1);
queue=new
T[sz];
MaxSize=sz;
front=rear=0;
for(int
i=0;i<arraysz;i++)
queue[i]=a[i];
rear=(rear+1)%MaxSize;
template<class
T>
void
SqQueue<T>::MakeEmpty()
front=rear=0;
template<class
T>
bool
SqQueue<T>::IsEmpty()
return
front==rear;
template<class
T>
int
SqQueue<T>::GetLength()
return
((rear-front)+MaxSize)%MaxSize;
template<class
T>
int
SqQueue<T>::GetMaxSize()
return
MaxSize;
template<class
T>
T
&
SqQueue<T>::GetFront()
if(IsEmpty())
MessageBox(0,"Empty
Queue!",0,0);
exit(1);
return
queue[front];
template<class
T>
bool
SqQueue<T>::EnQueue(T
&x)
if((rear+1)%MaxSize==front)
MessageBox(0,"Queue
is
Full",0,0);
return
0;
queue[rear]=x;
rear=(rear+1)%MaxSize;
template<class
T>
bool
SqQueue<T>::OutQueue(T
&x)
if(rear==front)
MessageBox(0,"Queue
is
Empty",0,0);
return
0;
x=queue[front];
front=(front+1)%MaxSize;
template<class
T>
void
SqQueue<T>::show()
while(front!=rear)
cout<<queue[front]<<"
";
front=(front+1)%MaxSize;
template<class
T>
SqQueue<T>::~SqQueue()
delete
[]queue;
rear=front;
..............................
//用链表实现的循环队列
//LinkQueueNode.h
#pragma
once
//链队结点类
#include
"stdafx.h"
template<class
T>
class
LinkQueueNode
private:
LinkQueueNode<T>*
next;
T
data;
public:
LinkQueueNode(T
&x):data(x),next(0)
void
SetData(T
&x)data=x;
void
SetLink(LinkQueueNode<T>*
p)next=p;
LinkQueueNode<T>*
GetLink()return
next;
T
&
GetData()return
data;
;
...............
#pragma
once
//链队类
//LinkQueue.h
#include
"stdafx.h"
#include
"LinkQueueNode.h"
#include
"Windows.h"
#include
"stdlib.h"
#include
"iostream.h"
template<class
T>
class
LinkQueue
private:
LinkQueueNode<T>*
front,*rear;
public:
LinkQueue():front(0),rear(0)//构造函数
LinkQueue(LinkQueue<T>
&lq);//拷贝构造函数
virtual
void
InitQueue(int
n,T
*arr);//初始化队列
virtual
int
GetLength();//获得长度
virtual
bool
EnQueue(T
&x);//入队
virtual
bool
OutQueue(T
&x);//出队
bool
IsEmpty();//队列是否为空
LinkQueueNode<T>*
GetFront();//获得队首指针
void
MakeEmpty();//清空队列
//输出队列
friend
ostream&
operator<<(ostream&
output,LinkQueue<T>&
lq);
virtual
~LinkQueue();//析构函数
;
template<class
T>
LinkQueue<T>::LinkQueue(LinkQueue<T>
&lq)
front=rear=0;
LinkQueueNode<T>*
p=lq.front;
while(p)
LinkQueueNode<T>*
newp=new
LinkQueueNode<T>(p->GetData());
if(!newp)
MessageBox(0,"Allocate
Failed!",0,0);
exit(1);
if(IsEmpty())
front=rear=newp;
rear->SetLink(newp);
rear=newp;
p=p->GetLink();
template<class
T>
void
LinkQueue<T>::InitQueue(int
n,T*
a)
if(n<0)
MessageBox(0,"Wrong
Number",0,0);
return;
int
i=0;
while(i<n)
LinkQueueNode<T>*
p=new
LinkQueueNode<T>(a[i]);
if(IsEmpty())
front=rear=p;
else
rear->SetLink(p);
rear=p;
i++;
template<class
T>
int
LinkQueue<T>::GetLength()
int
Length=0;
LinkQueueNode<T>*
p=front;
while(p)
p=p->GetLink();
Length++;
return
Length;
template<class
T>
bool
LinkQueue<T>::EnQueue(T
&x)
LinkQueueNode<T>*
p=new
LinkQueueNode<T>(x);
if(!p)
MessageBox(0,"Allocate
Failed!",0,0);
return
0;
if(!rear&&!front)
front=rear=p;
else
rear->SetLink(p);
rear=rear->GetLink();
return
1;
template<class
T>
bool
LinkQueue<T>::OutQueue(T
&x)
if(IsEmpty())
return
0;
LinkQueueNode<T>*
p=front;
if(front==rear)
rear=0;
front=front->GetLink();
x=p->GetData();
delete
p;
return
1;
template<class
T>
bool
LinkQueue<T>::IsEmpty()
if(front==0&&rear==0)
return
1;
else
return
0;
template<class
T>
LinkQueueNode<T>*
LinkQueue<T>::GetFront()
return
front;
template<class
T>
void
LinkQueue<T>::MakeEmpty()
T
x;
while(front)
OutQueue(x);
rear=0;
template<class
T>
ostream&
operator<<(ostream&
output,LinkQueue<T>
&lq)
if(lq.IsEmpty())
output<<"The
Queue
is
Empty!"<<endl;
exit(1);
output<<"The
Queue
Members
are
below:"<<endl;
LinkQueueNode<T>*
p=lq.front;
while(p)
output<<p->GetData()<<"
";
p=p->GetLink();
output<<endl;
return
output;
template<class
T>
LinkQueue<T>::~LinkQueue()
MakeEmpty();
参考技术B 问老师,谢谢。
C++ 标准模板库STL 队列 queue 使用方法与应用介绍
queuequeue模板类的定义在<queue>头文件中。
与stack模板类很相似,queue模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque类型。
定义queue对象的示例代码如下:
queue<int> q1;
queue<double> q2;
queue的基本操作有:
入队,如例:q.push(x); 将x接到队列的末端。
出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
访问队首元素,如例:q.front(),即最早被压入队列的元素。
访问队尾元素,如例:q.back(),即最后被压入队列的元素。
判断队列空,如例:q.empty(),当队列空时,返回true。
访问队列中的元素个数,如例:q.size()
std::queue
FIFO queue queues are a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the "back" of the specific container and popped from its "front".
The underlying container may be one of the standard container class template or some other specifically designed container class. The only requirement is that it supports the following operations:
- front()
- back()
- push_back()
- pop_front()
Therefore, the standard container class templates deque and list can be used. By default, if no container class is specified for a particular queue class, the standard container class template deque is used.
In their implementation in the C++ Standard Template Library, queues take two template parameters:
| |
Where the template parameters have the following meanings:
- T: Type of the elements.
- Container: Type of the underlying container object used to store and access the elements.
#include <iostream>
#include <queue>
#include <string>
using namespace std;
void test_empty()
queue<int> myqueue;
int sum (0);
for (int i=1;i<=10;i++) myqueue.push(i);
while (!myqueue.empty())
sum += myqueue.front();
myqueue.pop();
cout << "total: " << sum << endl;
//运行结果: total: 55
void test_pop()
queue<int> myqueue;
int myint;
cout << "\\nPlease enter some integers (enter 0 to end):\\n";
do
cin >> myint;
myqueue.push (myint);
while (myint);
cout << "myqueue contains: ";
while (!myqueue.empty())
cout << " " << myqueue.front();
myqueue.pop();
/********
运行结果:
Please enter some integers (enter 0 to end):
512
605
420
517
532
0
myqueue contains: 512 605 420 517 532 0
********/
void test_size()
queue<int> myints;
cout << "0. size: " << (int) myints.size() << endl;
for (int i=0; i<5; i++) myints.push(i);
cout << "1. size: " << (int) myints.size() << endl;
myints.pop();
cout << "2. size: " << (int) myints.size() << endl;
/****
运行结果:
0. size: 0
1. size: 5
2. size: 4
****/
int main()
test_empty();
cout<<"\\n***********************************************\\n";
test_size();
cout<<"\\n***********************************************\\n";
test_pop();
cout<<"\\n***********************************************\\n";
queue<string> q;
// insert three elements into the queue
q.push("These ");
q.push("are ");
q.push("more than ");
//cout << "number of elements in the queue: " << q.size()<< endl;
// read and print two elements from the queue
cout << q.front();
q.pop();
cout << q.front();
q.pop();
//cout << "number of elements in the queue: " << q.size()<< endl;
// insert two new elements
q.push("four ");
q.push("words!");
//cout << "\\nnumber of elements in the queue: " << q.size()<< endl;
// skip one element
q.pop();
// read and print two elements
cout << q.front();
q.pop();
cout << q.front() << endl;
q.pop();
// print number of elements in the queue
cout << "number of elements in the queue: " << q.size()<< endl;
/*******
*运行结果:
total: 55
***********************************************
0. size: 0
1. size: 5
2. size: 4
***********************************************
Please enter some integers (enter 0 to end):
512
605
420
517
532
0
myqueue contains: 512 605 420 517 532 0
***********************************************
These are four words!
number of elements in the queue: 0
Process returned 0 (0x0) execution time : 33.512 s
Press any key to continue.
********/
以上是关于怎么定义队列类模板(c++)的主要内容,如果未能解决你的问题,请参考以下文章