日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C语言的单链表实现队列

發(fā)布時間:2023/11/27 生活经验 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言的单链表实现队列 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

隊列是一種FIFO(先入先出)的數(shù)據(jù)結(jié)構(gòu)
C++的STL std::queue q; 相關(guān)的隊列操作,包括

q.empty() 判讀隊列是否為空
q.front() 返回隊列的首元素
q.back() 返回隊列的末尾元素
q.pop() 彈出隊列的頭部
q.push(x) 將x添加至隊列
q.size() 返回隊列的大小

本文使用C語言的鏈表,實現(xiàn)隊列以上操作(文末有測試代碼)

  • empty
    bool empty(Queue *head){if (head == NULL)return true;else return false;
    }
    
  • front 取隊列的頭元素,即返回鏈表的首節(jié)點
    Queue front(Queue *head) {Queue fro;if (NULL == head) {fro.next = NULL;return fro;}fro.data = head->data;//返回鏈表的首節(jié)點fro.next = head;return fro;
    }
    
  • back 返回隊列的尾元素,即返回鏈表的尾節(jié)點
    Queue back(Queue *head) {Queue back;if (NULL == head) {back.next = NULL;return back;}while (head -> next) //獲取到鏈表的尾節(jié)點{head = head ->next;}back.data = head->data;back.next = head;return back;
    }
    
  • pop 彈出隊列的首元素,即刪除鏈表的頭節(jié)點
    Queue *pop(Queue *head) {if (head ->next == NULL || head == NULL)head = NULL;head = head ->next; //刪除鏈表的頭節(jié)點return head;
    }
    
  • push 插入指定元素到隊列,即向使用尾插法插入新節(jié)點到鏈表
    Queue *push(Queue *head, Queue node) {if (head == NULL) {return NULL;}Queue *r = head;while(r -> next) { //獲取到鏈表的尾節(jié)點的上一個節(jié)點r = r -> next;}node.next = NULL;r ->next = &node; //使用尾插法,插入節(jié)點r = &node;return head;
    }
    
  • size 返回隊列的大小,計算鏈表的長度
    int size(Queue *head) {int s_num = 0;if (head == NULL) {return s_num;}while (head){s_num ++;head = head->next;       }return s_num;
    }
    

測試代碼如下

#include <stdlib.h>
#include <stdio.h>typedef struct  List
{int data;struct List *next;
}Queue;void print_list(Queue *p) {if (NULL == p) {printf("link list is NULL\n");return ;}while (p){printf("%d ", p -> data);p = p->next;}printf("\n");
}Queue *insert_tail(int n) {Queue *head = (Queue *)malloc(sizeof(Queue));head ->next = NULL;Queue *r = head;while (n --){Queue *p = (Queue *)malloc(sizeof(Queue));int tmp ;scanf("%d",&tmp);p -> data = tmp;p -> next = NULL;r -> next = p;r = p;}return head ->next;
}Queue *pop(Queue *head) {if (head ->next == NULL || head == NULL)head = NULL;head = head ->next;return head;
}Queue front(Queue *head) {Queue fro;if (NULL == head) {fro.next = NULL;return fro;}fro.data = head->data;fro.next = head;return fro;
}Queue back(Queue *head) {Queue back;if (NULL == head) {back.next = NULL;return back;}while (head -> next){head = head ->next;}back.data = head->data;back.next = head;return back;
}Queue *push(Queue *head, Queue node) {if (head == NULL) {return NULL;}Queue *r = head;while(r -> next) {r = r -> next;}node.next = NULL;r ->next = &node;r = &node;return head;
}bool empty(Queue *head){if (head == NULL)return true;else return false;
}int size(Queue *head) {int s_num = 0;if (head == NULL) {return s_num;}while (head){s_num ++;head = head->next;       }return s_num;
}int main() {printf("constuct queue \n");Queue *p = insert_tail(3);Queue node;node.data = 10;printf("push \n");Queue *q = push(p,node);print_list(q);printf("constuct queue \n");p = insert_tail(3);printf("pop \n");Queue *po = pop(p);print_list(po);printf("front %d\n",front(po).data);printf("size %d\n",size(po));printf("back %d\n",back(po).data);return 0;
}

輸出如下:

constuct queue 
1 2 3
push 
1 2 3 10 
constuct queue 
1 2 3
pop 
2 3 
front 2
size 2
back 3

總結(jié)

以上是生活随笔為你收集整理的C语言的单链表实现队列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。