⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lab5.cpp

📁 a queue class ,inclunde all of the basic function
💻 CPP
字号:
//template_class.cpp  D.Geva 
//class template demo - stack machanism

#include <stdafx.h>
#include <conio.h>
#include <iostream>
#include <string.h>
//#include <atltime.h>
using namespace std;
int Q_len;//length of queue 
//-------------------------------------------------------------------------------------
template <class T>
class Queue {
protected:
	T* a_start;    // Pointer to queue
	int r_start;   // Refernce start
	int len;       //length of queue
	int num;       // number of elments in queue
   
public:
	Queue (int=Q_len);                 // default Constructor            
	Queue (const Queue<T> &);       //copy constrauctor
	~Queue (void);                  //destructor
	int enqueue(const T& );         //store element in queue 
	int dequeue(T&) ;               //pull element from queue 
	void print();                   //print queue 
	int size() {return (num)}       //size of queue
	Queue<T> merge (const Queue<T>& );//merge 2 queue  into 1 and return it
	Queue<T> split (void);           //spilt 1 queue into 2 and return the second queue	
	
};
//----------------------------------------------------------------------------------
template <class T>
Queue<T>::Queue (int s )
// default Constructor
{
	len=s > 0 && s < 1000 ? s : 10 ;
	num=0;
	a_start=new T[len];
	r_start=0;
}

template <class T>
Queue<T>::Queue (const Queue<T> & a )
//copy constructor
{
	len=a.len;
	num=a.num;
	r_start=0;
	a_start=new T[len];
	for (int i=0;i<a.num;i++) *(a_start+i)=*(a.a_start+(a.r_start+i)%a.len);
}

template <class T>
Queue<T>::~Queue (void)
//destructor
{
 delete a_start;
}

template <class T>
int Queue<T>:: enqueue(const T& data )
//store element in queue
{
	if (num==len) return 0;
	*(a_start+(r_start+num)%len)=data;
	num++;
	return 1;	
}

template <class T>
int Queue<T>:: dequeue(T& ret)
//pull element from queue
{
if (num==0) return 0;
ret=*(a_start+r_start);
r_start=(((++r_start)*(num-1!=0))%len);
num--;
return 1;
}
	
template <class T>
void Queue<T>::print()
//print queue
{
	for (int i=0;i<num;i++) cout<<endl<<*(a_start+(r_start+i)%len);
	cout<<endl<<"num = "<<num<<endl<<"length = "<<len;
}


template <class T>
Queue<T> Queue<T>::merge (const Queue<T>& b)
//merge 2 queue  into 1 and return it
{
 Queue<T>  mer(len+b.len);
 for (int i=0;i<num;i++) *(mer.a_start+i)=*(a_start+(r_start+i)%len);
 for (int i=0;i<b.num;i++) *(mer.a_start+num+i)=*(b.a_start+(b.r_start+i)%b.len);
 mer.num=num+b.num;
 return (mer);
}

template <class T>
Queue<T> Queue<T>::split (void)
//spilt 1 queue into 2 and return the second queue
{
  Queue<T>  spl(len/2);
  T* arr;
  spl.num=num/2;
  for (int i=0;i<(num/2);i++) *(spl.a_start+i)=*(a_start+(r_start+i)%len);
  arr=new T[len-len/2];
  for (int i=num/2;i<num;i++) *(arr+i-num/2)=*(a_start+(r_start+i)%len);
  delete a_start;
  num=num-num/2;
  len=len-len/2;
  r_start=0;
  a_start=arr;
  arr=NULL;
  return spl;
}

//-----------------------------------------------------------------------------------------------
//
class Queue_int:public Queue<int>
{
public:
	static int i;
	Queue_int(int a=i):Queue<int>(a) { }
};
int Queue_int::i=27;

void main(void)
{

	Queue_int::i=13;
Queue_int f;
f.print();













//Queue<int>  arr[15](8);

//int arr[10]={;
//cout<<arr[5];



//j=10;
//while (Q_inb.enqueue(++j));
//Q_ina.dequeue(j);
//Q_ina.dequeue(j);
/*Q_ina.dequeue(j);
Q_ina.dequeue(j);
Q_ina.dequeue(j);
Q_ina.dequeue(j);
Q_inb.dequeue(j);
Q_inb.dequeue(j);
Q_inb.dequeue(j);
Q_ina.print();
Q_inb.print();
cout<<endl<<endl;

*/



//Q_ina.print();

//Queue<int> qc(Q_ina.merge(Q_ina));
//Q_ina.print();
//qc.print();

getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -