STL-queue和循环队列基本操作的实现

2018-11-13-17:53:44  

1.可增长循环队列

    队列是一种特殊的线性表,是一种先进先出(FIFO)的数据结构。它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。

    下面是我用顺序结构实现的可增长循环队列,当队列元素的个数达到QueueSize-1时,队列的最大储存长度会定量增长。

 

 

  1 /*********************************************************
  2     循环队列的基本操作的实现。
  3         1.当队列元素的个数达到QueueSize-1时,队列的最大储存长度会定量增长。
  4     mian函数操作:
  5         1.输入一个字符串。
  6         2.输入字符,如果字符为'+'或者'-'则进行入队或者出队操作。
  7         3.每次入队出队后打印出现有的队列。
  8 **********************************************************/
  9 #include <cstdio>
 10 #include <cstring>
 11 #include <cstdlib>
 12 #include <iostream>
 13 using namespace std;
 14 #define INITQUEUESIZE 100
 15 #define QUEUEINCREAMENT 40
 16 #define OverFlow -1
 17 typedef char QElemtype;
 18 typedef struct{
 19     QElemtype*Elem;//存放队列元素数组的首地址
 20     int Front,Rear;//储存队头和队尾的位置
 21     int QueueSize;//队列当前的最大储存长度
 22 }Queue;
 23 bool Init_Queue(Queue&Q);
 24 bool Queue_Empty(Queue Q);
 25 bool Queue_Full(Queue Q);
 26 QElemtype Get_Front(Queue Q);
 27 bool Pop(Queue&Q,QElemtype&Elem);
 28 bool Push(Queue&Q,QElemtype Elem);
 29 void PrintQueue(Queue Q);
 30 //main函数内所有数据均为测试数据,读者可根据自己测试方式自行调换
 31 
 32 int main()
 33 {
 34     Queue Q;
 35     Init_Queue(Q);
 36     QElemtype Elem;
 37     string s;
 38     cin>>s;
 39     for(int i=0;i<s.length();i++){
 40         char c=getchar();
 41         if(c=='+'){
 42             Push(Q,s[i]);
 43             PrintQueue(Q);
 44         }
 45         else if(c=='-'){
 46             if(!Queue_Empty(Q)){
 47                 Pop(Q,c);
 48                 PrintQueue(Q);
 49             }
 50             else
 51                 cout<<"Havn't Elem in this Queue"<<endl<<"Please repeate input:"<<endl;
 52         }
 53         else    i--;//如果输入!'+'||!'-'则重新开始此步骤
 54     }
 55     PrintQueue(Q);
 56 }
 57 bool Init_Queue(Queue&Q){
 58     Q.Elem=(QElemtype*)malloc(INITQUEUESIZE*sizeof(Queue));
 59     if(!Q.Elem)
 60         exit(OverFlow);
 61     Q.Front=Q.Rear=0;
 62     Q.QueueSize=INITQUEUESIZE;
 63     return true;
 64 }
 65 
 66 bool Queue_Empty(Queue Q){
 67     if(Q.Front==Q.Rear)
 68        return true;
 69     else
 70         return false;
 71 }
 72 
 73 int Size_Queen(Queue Q){
 74     return (Q.Rear-Q.Front+Q.QueueSize)%Q.QueueSize;
 75 }
 76 
 77 bool Queue_Full(Queue Q){
 78     if((Q.Rear+1)%Q.QueueSize==Q.Front)
 79        return true;
 80     else
 81         return false;
 82 }
 83 
 84 QElemtype Get_Front(Queue Q){
 85     if(!Queue_Empty(Q)){
 86         return Q.Elem[Q.Front];
 87     }
 88 }
 89 
 90 bool Pop(Queue&Q,QElemtype&Elem){
 91     if(!Queue_Empty(Q)){
 92         Elem=Q.Elem[Q.Front];
 93         Q.Front=(Q.Front+1)%Q.QueueSize;
 94         return true;
 95     }
 96     return false;
 97 }
 98 
 99 bool Push(Queue&Q,QElemtype Elem){
100     if(Queue_Full(Q)){
101         Q.Elem=(QElemtype*)realloc(Q.Elem,(Q.QueueSize+QUEUEINCREAMENT)*sizeof(QElemtype));
102         if(!Q.Elem)
103             exit(OverFlow);
104         Q.Rear=Q.Front+Q.QueueSize;
105         Q.QueueSize+=QUEUEINCREAMENT;
106     }
107     Q.Elem[Q.Rear]=Elem;
108     Q.Rear=(Q.Rear+1)%Q.QueueSize;
109 }
110 void PrintQueue(Queue Q){
111      QElemtype Elem1,Elem2;
112     for(int i=Q.Front;i<Q.Rear;i++){
113         Elem1=Get_Front(Q);
114         Pop(Q,Elem2);
115         if(Elem1==Elem2)
116             cout<<Elem2<<'\t';
117     }
118     cout<<"The size of Q is "<<Q.QueueSize<<endl;
119     if(Queue_Full(Q)) cout<<"Yes"<<endl;
120     else    cout<<"No"<<endl;
121 }
122 
123 /****************************************
124     Author:CRUEL_KING
125     Time:2018/11/13
126     Program name:循环队列的基本操作的实现.cpp
127 ****************************************/

 

2.STL之Queue队列

    C++中通常通过STL模板类定义队列,queue是一个容器适配器,具体而言,他是一个先进先出(FIFO)的数据结构。

      头文件:#include<queue>

      原型:templateclass T, class Container =std::deque<T> > class queue;

        如上,这对尖括号中有两个参数,第一个是T,表示队列中存放的数据的类型,比如int,double,或者结构体之类。

      第二个参数指明底层实现的容器类型,也就是指明这个栈的内部实现方式,比如vector,deque,list。如果不指明它,默认使用deque(双端队列)。

      队列的成员函数和基本操作:

        定义一个队列:   

1 queue<char>q;//定义一个数据类型为char变量名为q的队列

 

        back()返回最后一个元素:

1 q.back();//访问最后被压入队列的元素。

 

        empty()如果队列空则返回真:

1 q.empty();//当队列空时,返回true。

 

        front()返回第一个元素:

q.front();//访问队列的第一个元素

 

        pop()删除第一个元素:

1 q.pop();// 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。

 

        push()在末尾加入一个元素:

1 q.push(x); //将x 接到队列的末端。

 

        size()返回队列中元素的个数:

1 length=q.size();//将q队列的元素个数赋值给一个变量length。

 

posted @ 2018-11-13 18:30  Cruel_King  阅读(4816)  评论(1编辑  收藏  举报