隊列的鏈式儲存結構的實現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;}