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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

队列(单链表)

發布時間:2024/9/27 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 队列(单链表) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

?


頭文件

#pragma once//利用帶頭節點的單鏈表實現隊列,隊頭為第一個數據節點typedef struct Node {int data;struct Node *next; }Node;//數據節點typedef struct HNode {struct Node *front;//隊頭指針struct Node *rear;//隊尾指針 }HNode,*PLQueue;//頭節點void InitQueue(PLQueue pl);//入隊 bool Push(PLQueue pl,int val);//獲取隊頭的值,但不刪除 bool GetTop(PLQueue pl,int *rtval);//獲取隊頭的值,且刪除 bool Pop(PLQueue pl,int *rtval);bool IsEmpty(PLQueue pl);void Destroy(PLQueue pl);

cpp文件

#include <stdio.h> #include <assert.h> #include <stdlib.h> #include "lqueue.h" //利用帶頭節點的單鏈表實現隊列,隊頭為第一個數據節點void InitQueue(PLQueue pl) {assert(pl != NULL);pl->front = NULL;pl->rear = NULL; }//入隊 bool Push(PLQueue pl,int val) {Node *p = (Node *)malloc(sizeof(Node));p->data = val;p->next = NULL;if(IsEmpty(pl)){pl->front = p;pl->rear = p;}else{pl->rear->next = p;pl->rear = p;}return true; }//獲取隊頭的值,但不刪除 bool GetTop(PLQueue pl,int *rtval) {if(IsEmpty(pl)){return false;}if(rtval != NULL){*rtval = pl->front->data;}return true; }//獲取隊頭的值,且刪除 bool Pop(PLQueue pl,int *rtval) {if(IsEmpty(pl)){return false;}if(rtval != NULL){*rtval = pl->front->data;}Node *p = pl->front;pl->front = p->next;free(p);if(pl->front == NULL) //已經刪除最后一個節點{pl->rear = NULL;}return true; }bool IsEmpty(PLQueue pl) {return pl->front == NULL; }void Destroy(PLQueue pl) {Node *q;for(Node *p = pl->front;p->next != NULL;p = p-> next){q = p;free(q);}pl -> front = NULL;pl -> rear = NULL; }

主函數

#include <stdio.h> #include "lqueue.h"int main() {HNode head;InitQueue(&head);for(int i=0;i<15;i++){Push(&head,i);}int tmp;while(!IsEmpty(&head)){Pop(&head,&tmp);printf("%d\n",tmp);}return 0; }

?

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。