鏈隊列的基本操作

來源:互聯網
上載者:User

#include<iostream>
#define SIZE    10
using namespace std;
typedef int Type;

typedef struct qnode{
        Type    data;
        qnode   *next;
        }qnode,*queue_pp;
typedef struct {
        queue_pp front;
        queue_pp rear;
        int      qlen;
}linkqueue;

int initqueue(linkqueue &q){ //構造空隊列
    q.front = q.rear = new qnode;
    if(!q.front) return 0;
    q.front->next = NULL;
    q.qlen = 0;
    return 1;
}
int destroyqueue(linkqueue &q) { //撤銷隊列
    while(q.front) {
    q.rear = q.front->next;
    delete q.front;
    q.front = q.rear;
}
    return 1;
}
int insertqueue(linkqueue &q,Type e) { //插入元素e為新的隊尾元素
    qnode *p;
    p = new qnode;
    if(!p) return 0;
    p->data = e; p->next = NULL;
    q.rear->next = p;
    q.rear = p;
    ++q.qlen;
    return 1;
}
int deletequeue(linkqueue &q,Type e) { //刪除隊頭元素
    if(q.front == q.rear) return 0;
    qnode *p;
    p = q.front->next;
    e = p->data;
    q.front->next = p->next;
    if(q.rear == p) q.rear = q.front;
    delete q.front;
    --q.qlen;
    return 1;
}
int queueexist(linkqueue &q) {return q.front == 0;} //隊列是否存在
int queueclear(linkqueue &q) { //置為空白隊
    qnode *e;
    e = q.front->next;
    delete q.front;
    q.front = e;
}
int queueempty(linkqueue &q){return q.front == q.rear;} //隊列空為真
int queuelength(linkqueue q){cout<<q.qlen<<endl;}//隊列元素個數
int queueshow(linkqueue &q)   //輸出隊列元素
{
    if(queueempty(q) || queueexist(q))
    cout<<"隊中沒有元素"<<endl;
    else {
    qnode *p;
    p = q.front->next;
    while(p) {
    cout<<p->data<<" ";
    p = p->next;}
}
}

int main()
{
    cout<<"□□□□隊列基本操作□□□□"<<endl;
    cout<<"□□□ 1.建立空隊列 □□□□"<<endl;
    cout<<"□□□ 2.插入隊列 □□□□□"<<endl;
    cout<<"□□□ 3.刪除隊頭元素 □□□"<<endl;
    cout<<"□□□ 4.返回隊列長度 □□□"<<endl;
    cout<<"□□□ 5.判斷隊列是否為空白 □"<<endl;
    cout<<"□□□ 6.清空隊列 □□□□□"<<endl;
    cout<<"□□□ 7.撤銷隊列 □□□□□"<<endl;
    cout<<"□□□ 8.輸出隊列 □□□□□"<<endl;
    cout<<"□□□ 0.退出 □□□□□□□"<<endl;
   
    linkqueue qq;
    int ch;
    Type e;
    while(1) {
    cout<<endl;
    cout<<"請輸入你的選擇"<<endl;
    cin>>ch;
         switch(ch)
         {
              case 1:
                   initqueue(qq);
                   cout<<"隊列已建立"<<endl;
                   break;
              case 2:
                   if(queueexist(qq))break;
                   cout<<"你入隊元素是:";
                   cin>>e;
                   insertqueue(qq,e);
                   cout<<"隊中元素:";
                   queueshow(qq);
                   break;
              case 3:
                   if(queueexist(qq))
                   break;
                   deletequeue(qq,e);
                   cout<<"您刪除的元素是"<<e;
                   queueshow(qq);
                   break;
              case 4:
                   queuelength(qq);
                   break;
              case 5:
                   if(queueempty(qq))
                   cout<<"隊為空白"<<endl;
                   else {
                   cout<<"隊未空"<<endl;}
                   break;
              case 6:
                   queueclear(qq);
                   break;
              case 7:
                   destroyqueue(qq);
                   break;
              case 8:
                   queueshow(qq);
                   break;
              case 0:
                   exit(0);
                   break;
              default:
                 cout<<"輸入錯誤,請重新輸入選擇"<<endl;
                 break;                          
        }       
    }
           system("pause");   
           return 1;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.