棧與隊列--判斷棧/隊列為空白/滿__資料結構

來源:互聯網
上載者:User

數組棧
完成int IsEmpty(Stack S)函數,該函數判斷棧是否已空,如果空返回1,否則返回0。
完成int IsFull(Stack S)函數,該函數判斷棧是否已滿,如果滿返回1,否則返回0。

typedef int ElemType;struct StackRecord;typedef struct StackRecord *Stack;struct StackRecord{    int Capacity; //棧容量    int Top; //棧頂,初始為1    ElemType *Array;};int IsEmpty(Stack S){    if(S‐>Top==‐1)    {        return 1;    }     else    {        return 0;    }}int IsFull(Stack S){    if(S‐>Top==S‐>Capacity‐1)    {        return 1;    }     else    {        return 0;    }}

鏈棧
完成int IsEmpty(Stack S);函數,該函數判斷棧S是否為空白,空棧返回1,否則返回0,已知S是帶頭結點的鏈棧。

typedef int ElemType;struct Node;typedef struct Node * PtrToNode;typedef PtrToNode Stack;struct Node{    ElemType data;    PtrToNode next;};int IsEmpty(Stack S){    return S‐>next==NULL?1:0;}

數組隊列
完成int IsEmpty(Queue Q);函數,該函數判斷隊列Q是否已空,如果已空返回1,否則返回0,其中Q是基於數組的非迴圈隊列。
完成int IsFull(Queue Q)函數,該函數判斷隊列Q是否已滿,如果已滿返回1,否則返回0,其中Q是基於數組的非迴圈隊列。

typedef int ElemType;struct QueueRecord;typedef struct QueueRecord * Queue;struct QueueRecord{    int Capacity; //隊列總容量    int Front; //隊首 初始值為0    int Rear; //隊尾,初始值為1    int Size; //隊列中資料數,初始值為0    ElemType *Array;};int IsEmpty(Queue Q){    return Q‐>Size==0;}int IsFull(Queue Q){    return Q‐>Rear==Q‐>Capacity‐1?1:0;}

鏈隊列
完成int IsEmpty(Queue q)函數,該函數判定基於鏈表的隊列q是否為空白,空隊列返回1,非空隊列返回0,其中q是不帶前端節點的鏈表隊列。

typedef int ElemType;struct node;typedef struct node Node;struct queue;typedef struct queue * Queue;struct node{    ElemType data;    Node * next;};struct queue{    Node * front; //隊首    Node * rear; //隊尾    int size; //隊列中資料數};int IsEmpty(Queue q){    return q‐>size==0?1:0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.