隊列——假設以帶頭結點的循環鏈表表示隊列,並且只設一個指針指向隊尾元素結點(注意:不設頭指針), * 試編寫相應的置空隊列、判斷隊列是否為空、入隊和出隊等算法。


 

簡單的流程圖

 

#include<iostream>
using namespace std;

#define ElemType int

/*
假設以帶頭結點的循環鏈表表示隊列,並且只設一個指針指向隊尾元素結點(注意:不設頭指針),
*     試編寫相應的置空隊列、判斷隊列是否為空、入隊和出隊等算法。
*/

typedef struct QNode{
    ElemType data;
    struct QNode *next;
}QNode,*QueuePtr;


typedef struct{
    QueuePtr rear;
    QNode head;
}LinkQueue;

string InitQueue(LinkQueue &Q){
    Q.rear = new QNode;                //Q.rear代表一個節點,該節點默認為頭結點
    Q.rear->next = Q.rear;             //循環隊列,尾指針指向頭結點
    return "OK";
}

string EnQueue(LinkQueue &Q,ElemType e){ //在隊尾插入
    QueuePtr S = new QNode; 
    S->data = e;
    S->next=Q.rear->next;                
    Q.rear->next=S;
    Q.rear=S;
    return "OK";   
}


string DeQueue(LinkQueue &Q,ElemType &e){

    QNode *q = Q.rear->next->next;
    e = q->data;
    Q.rear->next->next=q->next;
    if(Q.rear->next->next == Q.rear->next) Q.rear=Q.rear->next; //若刪除的是最后一個元素時,
    delete q;                                                   //將尾指針歸位,將尾指針代表頭結點
    return "OK"; 
}

int IsEmpty(LinkQueue Q){
    if(Q.rear->next == Q.rear) return 1;
    else return 0;
}


int main(){

    LinkQueue Q;
    InitQueue(Q);
    EnQueue(Q,2);
    cout << IsEmpty(Q)<<endl;
    ElemType a;
    DeQueue(Q,a);
    cout <<a;
    cout << IsEmpty(Q)<<endl;
    system("pause");
    return 0;
}

 

 參閱其他博客,並在其基礎上進行修補

https://blog.csdn.net/JxufeCarol/article/details/83057994

 


免責聲明!

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



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