隊列的鏈式儲存結構的實現2 —— 出隊查看隊首尾元素計算元素個數,鏈式首尾

來源:互聯網
上載者:User

隊列的鏈式儲存結構的實現2 —— 出隊查看隊首尾元素計算元素個數,鏈式首尾

//  Filename    :  list_queue.c//  Author      :   LupingChen//  Data        :   2015.05.30//  Content     :   create\destory\full\empty\push#include <stadio.h>#include <stdlib.h>//定義節點資料類型typedef struct Node {    int data;//節點資料    struct Node* next;//記錄下一節點地址} Node;//定義隊列資料類型typedef struct {    Node* head;//頭指標} Queue;//出隊int pop(Queue* pq);//查看隊首元素int get_head(Queue* pq);//查看隊尾元素int get_tail(Queue* pq);//計算隊列中所有的元素int size(Queue* pq);int main(void){    return 0;}//出隊int pop(Queue* pq){    if (empty(pq))    {        return -1;    }    //給頭指標找個替身    Node* p = pq->head;    p->head = p->next;    //儲存要釋放的節點資料    int temp = p->data;    free(p);    p = NULL;    //返回釋放節點資料    return temp;}//查看隊首元素int get_head(Queue* pq){    if (empty(pq))    {        return -1;//用-1代表錯誤    }    return pq->head->data;}//查看隊尾元素int get_tail(Queue* pq){    //隊列為空白    if (empty(pq))    {        return -1;    }    //隊列不為空白    //替身    Node* p = pq->head;    //迴圈移動指標,定位到尾節點    while (p->next != NULL)    {        p = p->next;    }    //返回尾節點資料    return p->data;    }//計算隊列中所有的元素個數int size(Queue* pq){    int cnt = 0;    Node* p = pq->head;    while (p != NULL)    {        cnt++;        p = p->next;    }    return cnt;}

聯繫我們

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