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。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM