Queue-C++template1

 

队列特点:只在队首删除数据,只在队尾添加数据

数组队列和链表队列的区别:数组队列大小有限,链表队列大小无限制 

1.队列模板的链表实现  TEST CODE IDE:VC++6.0

2.队列模板的数组实现  TEST CODE IDE:VC++6.0

 

1.队列模板的C++链表实现

//=============================================================================
//  MR.yao workshop Copyright (C) 2007-2008.
// 
//  module: queue.h
//  
//  Modify History:
//
//  MR.yao           Create:09/13/07   
//=============================================================================
#ifndef  H_queue
#define  H_queue
#include <iostream>
#include <cassert>
using namespace std;
//=============================================================================
//  FUNCTION: queue class template ADT
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
struct node
{
 T data;
 node<T> *link;
};


template<class T>
class queue
{
public:
 queue();
 ~queue();
 void add(const T& newitem);
 void del();
 T    gethead();
 T  gettail();
 void print();


private:
 node<T> *head;
 node<T> *tail;
};

 

//=============================================================================
//  FUNCTION: queue()
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
queue<T>::queue()
{
 head=NULL;
 tail=NULL;
}//end constructor

 

//=============================================================================
//  FUNCTION: ~queue()
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
queue<T>::~queue()
{
 node<T> *temp;
 while(head!=NULL)
 {
 temp=head;
 head=head->link;
 delete temp;
 }
 tail=NULL;
}//end destructor

 

//=============================================================================
//  FUNCTION: add()
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
void queue<T>::add(const T& newitem)
{
 node<T> *newnode;
 newnode=new node<T>;
 assert(newnode!=NULL);
 newnode->data=newitem;
 newnode->link=NULL;
 if (head==NULL)
 {
  head=newnode;
  tail=newnode;
 }
 else
 {
  tail->link=newnode;
  tail=newnode;
 }
}//end add


//=============================================================================
//  FUNCTION: del()
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
void queue<T>::del()

 if(head==NULL)
  {cout<<"queue is empty."<<endl;}
 else
 {
  node<T> *temp;
  temp=head;
  head->link=head->link;
  head->data=head->data;
  head=head->link;
  delete temp;
 }
}//end del

 

//=============================================================================
//  FUNCTION: gethead()
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
T queue<T>::gethead()

 if(head==NULL)
 {
 cout<<"head is empty."<<endl;
 return NULL;
 }
 else
 return head->data;
}//end gethead

 

//=============================================================================
//  FUNCTION: gettail()
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
T queue<T>::gettail()

 if(tail==NULL)
 {
 cout<<"tail is empty."<<endl;
 return NULL;
 }
 else
 return tail->data;
}//end gettail

 

//=============================================================================
//  FUNCTION: print()
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
void queue<T>::print()
{
if(head==NULL)
{cout<<"this queue is empty."<<endl;}
else 
{ node<T> *current;
 current=head;
 while(current!=NULL)
 {
  cout<<current->data<<" ";
  current=current->link;
 }
 cout<<endl;
}
}//end print

#endif

//=============================================================================

//demo.cpp 链表堆栈测试
#include <iostream>
#include "queue.h"
using namespace std;

int main()
{
    queue<int> ATM;
    //queue<char> ATM;
    int choice;
    while (1)
 {
     cout<<"----------------------------------------/n";
     cout<<"**队列模板测试******/n";
     cout<<"1 添加队列/n";
     cout<<"2 删除队列/n";
     cout<<"3 打印队列/n";
     cout<<"4 队首数据/n";
     cout<<"5 队尾数据/n";
     cout<<"0 退出/n";
     cout<<"选择(0,1,2,3,4):/n";
     cout<<"----------------------------------------/n";
     cin >> choice;
     if (choice==0)
  break;
     switch (choice)
  {
case 1:
    cout << "Enter a int data into queue: /n";
    int value;
    //char value;

   cin >> value;
    ATM.add(value);
    system("pause");
    system("cls");
    break;
case 2:
    ATM.del();
    system("pause");
    system("cls");
    break;
case 3:
    ATM.print();
    system("pause");
    system("cls");
    break;
case 4:
    cout<<ATM.gethead()<<endl;
    system("pause");
    system("cls");
    break; 
case 5:
    cout<<ATM.gettail()<<endl;
    system("pause");
    system("cls");
    break;     
default:
    cout << "choose error." << endl;
    system("pause");
    system("cls");
                break;
      }
    }

return 0;
}

 

 

 

2.队列模板的数组实现

//=============================================================================
//  MR.yao workshop Copyright (C) 2007-2008.
//
//  module: queue.h
//
//  Modify History:
//
//  MR.yao           Create:09/13/07   
//=============================================================================
#ifndef  H_queue
#define  H_queue

#include <iostream>
#include <cassert>
using namespace std;
//=============================================================================
//  FUNCTION: queue class template ADT
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
class queue
{
public:
 queue(int size=100);
 ~queue();
 bool isempty();
 bool isfull();
 void add(const T& newitem);
 void del();
 T    gethead();
 T  gettail();
 int  getcount();
 void print();

private:
 T   *list;
 int maxsize;
 int head;
 int tail;
 int count;
};

 

//=============================================================================
//  FUNCTION: queue()
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
queue<T>::queue(int size)
{
 if (size<=0)
  maxsize=100;
 else
  maxsize=size;

 head=0,count=0;
 tail=maxsize - 1;
 list=new T[maxsize];
 assert(list!=NULL);
}//end constructor

 

//=============================================================================
//  FUNCTION: ~queue()
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
queue<T>::~queue()
{
 delete [] list;
}//end destructor


//=============================================================================
//  FUNCTION: isempty()
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
bool queue<T>::isempty()
{ return (count==0);}

 

//=============================================================================
//  FUNCTION: isfull()
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
bool queue<T>::isfull()
{ return (count==maxsize);}

 

//=============================================================================
//  FUNCTION: add()
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
void queue<T>::add(const T& newitem)
{
 if(!isfull())
 {
  tail=(tail+1)%maxsize;
  list[tail]=newitem;
  count++;
 }
 else
 cout<<"this queue is full..."<<endl;
}//end add


//=============================================================================
//  FUNCTION: del()
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
void queue<T>::del()

 if(!isempty())
 { 
//  list[head -1]=list[head];
  head=(head+1)%maxsize;
  count--;
 }
 else
 cout<<"this queue is empty..."<<endl;
}//end del

 

//=============================================================================
//  FUNCTION: gethead()
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
T queue<T>::gethead()

 assert(!isempty());
 return list[head];
}//end gethead

 

//=============================================================================
//  FUNCTION: gettail()
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
T queue<T>::gettail()

 assert(!isfull());
 return list[tail];
}//end gettail

 

//=============================================================================
//  FUNCTION: getcount()
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
int queue<T>::getcount()
{ return count;}

 

//=============================================================================
//  FUNCTION: print()
//
//  Modify History:
//                              
//  MR.yao           Create:09/13/07   
//=============================================================================
template<class T>
void queue<T>::print()

 if(isempty() )
  cout<<"queue is empty."<<endl;
 else
 {
  for(int i=0;i<count;i++)
  cout<<list[i]<<" ";
 }
 cout<<endl;
}

#endif

 

//=============================================================================
//demo.cpp 数组队列测试

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

int main()
{
    queue<int> ATM(10);
    //queue<char> ATM;
    int choice;
    while (1)
 {
     cout<<"----------------------------------------/n";
     cout<<"**数组队列模板测试******/n";
     cout<<"1 添加队列/n";
     cout<<"2 删除队列/n";
     cout<<"3 打印队列/n";
     cout<<"4 队首数据/n";
     cout<<"5 队尾数据/n";
     cout<<"0 退出/n";
     cout<<"选择(0,1,2,3,4):/n";
     cout<<"----------------------------------------/n";
     cin >> choice;
     if (choice==0)
  break;
     switch (choice)
  {
case 1:
  cout << "Enter a int data into queue: /n";
  int value;
  //char value;
  cin >> value;
  ATM.add(value);
  system("pause");
  system("cls");
  break;
case 2:
  ATM.del();
  system("pause");
  system("cls");
  break;
case 3:
  ATM.print();
  system("pause");
  system("cls");
  break;
case 4:
  cout<<ATM.gethead()<<endl;
  system("pause");
  system("cls");
  break; 
case 5:
  cout<<ATM.gettail()<<endl;
  system("pause");
  system("cls");
  break;     
default:
  cout << "choose error." << endl;
  system("pause");
  system("cls");
 break;
      }
    }

return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值