首页 > 技术文章 > 链队列的初始化 判断队列是否为空 入队列 出队列

ITCoderW 2017-08-23 14:30 原文


链队列的初始化 判断队列是否为空 入队列 出队列 等等操作先上个示意图(图片来源 严蔚敏版数据结构) 可能更好理解
1
// 2 // main.cpp 3 // WWLinkQueueBasic 4 // 5 // Created by Live on 2017/8/22. 6 // Copyright © 2017年 ITCoderW. All rights reserved. 7 // 8 //参考书籍:严蔚敏版数据结构 数据结构高分阅读等 9 10 #include <iostream> 11 using namespace std; 12 13 #define maxSize 1000 14 #define WWStr(str) #str 15 #define WWLine "-------------" 16 17 18 /** 19 链队列结点结构体 20 */ 21 typedef struct LinkQueueNode{ 22 int data; //数据域 23 struct LinkQueueNode *next; //指针域 24 }LinkQueueNode; 25 26 27 /** 28 链队列类型定义(只有一个头尾指针而已) 29 */ 30 typedef struct{ 31 LinkQueueNode *front; //链队列头指针 32 LinkQueueNode *rear; //链队列尾指针 33 }LinkQueue; 34 35 36 /** 37 链队列相关操作 38 */ 39 void linkQueueOperation(); 40 /** 41 初始化链队列 42 43 @param linkQueue 链队列linkQueue 44 */ 45 void initLinkQueue(LinkQueue *&linkQueue); 46 47 /** 48 判断链队列是否为空 49 50 @param linkQueue 链队列 51 */ 52 53 /** 54 判断链队列是否为空 55 56 @param linkQueue 链队列 57 @return 为空返回1 不为空返回0 58 */ 59 int isEmptyLinkQueue(LinkQueue *linkQueue); 60 61 /** 62 入队列 63 64 @param linkQueue 链队列 65 @param x 要入队列的元素 66 */ 67 void enLinkQueue(LinkQueue *linkQueue,int x); 68 #pragma mark - 出队列 69 70 /** 71 链队列出队列 72 73 @param linkQu 要出队列的链队列 74 @param x 返回出队列的数据域的值 75 @return 返回能否出队列 76 */ 77 int outLinkQueue(LinkQueue *linkQu,int &x); 78 79 int main(int argc, const char * argv[]) { 80 linkQueueOperation(); 81 return 0; 82 } 83 84 85 #pragma mark - 链队列相关操作 86 void linkQueueOperation(){ 87 88 LinkQueue *linkQueue; 89 initLinkQueue(linkQueue); 90 isEmptyLinkQueue(linkQueue); 91 //1 2 3 依次入队列 92 enLinkQueue(linkQueue, 1); 93 enLinkQueue(linkQueue, 2); 94 enLinkQueue(linkQueue, 3); 95 // 96 int currentFrontData; 97 outLinkQueue(linkQueue, currentFrontData); 98 outLinkQueue(linkQueue, currentFrontData); 99 outLinkQueue(linkQueue, currentFrontData); 100 101 outLinkQueue(linkQueue, currentFrontData); 102 103 enLinkQueue(linkQueue, 4); 104 outLinkQueue(linkQueue, currentFrontData); 105 106 107 } 108 109 #pragma mark - 初始化链队列 110 void initLinkQueue(LinkQueue *&linkQueue){ 111 112 cout<<WWLine<<WWStr(链队列初始化操作地址)<<&linkQueue<<WWLine<<endl; 113 //之前一直是笔误导致了下边的这种情况的发生 还找了半天错 所以以后的话最好要注意别把参数名和类型的名字写得太像 114 // linkQueue = (linkQueue *)malloc(sizeof(linkQueue)); 115 linkQueue = (LinkQueue *)malloc(sizeof(LinkQueue)); 116 linkQueue->front = NULL; 117 linkQueue->rear = linkQueue->front; 118 119 } 120 121 #pragma mark - 判断队列是否为空 122 int isEmptyLinkQueue(LinkQueue *linkQueue){ 123 cout<<WWLine<<WWStr(判断链队列是否为空 队列地址)<<&linkQueue<<WWLine<<endl; 124 if (linkQueue->front == NULL || linkQueue->rear == NULL) { 125 return 1; 126 } 127 return 0; 128 } 129 130 #pragma mark - 入队列 131 void enLinkQueue(LinkQueue *linkQueue,int x){ 132 cout<<WWLine<<WWStr(进入链队列地址)<<linkQueue<<WWLine<<endl; 133 134 cout<<WWStr(当前入队列的元素)<<x<<endl; 135 LinkQueueNode *Node = (LinkQueueNode*)malloc(sizeof(LinkQueueNode)); 136 Node->data = x; 137 Node->next = NULL; 138 139 if(isEmptyLinkQueue(linkQueue)){ 140 //如果入的是空队列 则新结点是队首结点也是队列尾结点 141 linkQueue->front = Node; 142 linkQueue->rear = linkQueue->front; 143 }else{ 144 //将新结点链到队列尾部 rear指向它 145 linkQueue->rear->next = Node; 146 linkQueue->rear = Node; 147 } 148 149 } 150 151 #pragma mark - 出队列 152 int outLinkQueue(LinkQueue *linkQu,int &x){ 153 cout<<WWLine<<WWStr(出队列链队列地址)<<linkQu<<WWLine<<endl; 154 155 if (isEmptyLinkQueue(linkQu)) { 156 //链队列为空不能够出队列 157 cout<<WWStr(链队列为空,不能出队列)<<endl; 158 return 0; 159 } 160 161 //把当前链队列的头的数据赋值给x 162 LinkQueueNode *Node = linkQu->front; 163 x = Node->data; 164 165 cout<<WWStr(当前出队列的元素:)<<x<<endl; 166 167 //移动指针 如果有的话 front指向下一个结点 168 if(linkQu->front == linkQu->rear){ 169 linkQu->front = NULL; 170 linkQu->rear =NULL; 171 }else{ 172 linkQu->front = linkQu->front->next; 173 } 174 free(Node); 175 return 1; 176 }


运行结果如下:

-------------链队列初始化操作地址0x7fff5fbff618-------------

-------------判断链队列是否为空队列地址0x7fff5fbff5c0-------------

-------------进入链队列地址0x100202f90-------------

当前入队列的元素1

-------------判断链队列是否为空队列地址0x7fff5fbff560-------------

-------------进入链队列地址0x100202f90-------------

当前入队列的元素2

-------------判断链队列是否为空队列地址0x7fff5fbff560-------------

-------------进入链队列地址0x100202f90-------------

当前入队列的元素3

-------------判断链队列是否为空队列地址0x7fff5fbff560-------------

-------------出队列链队列地址0x100202f90-------------

-------------判断链队列是否为空队列地址0x7fff5fbff540-------------

当前出队列的元素:1

-------------出队列链队列地址0x100202f90-------------

-------------判断链队列是否为空队列地址0x7fff5fbff540-------------

当前出队列的元素:2

-------------出队列链队列地址0x100202f90-------------

-------------判断链队列是否为空队列地址0x7fff5fbff540-------------

当前出队列的元素:3

-------------出队列链队列地址0x100202f90-------------

-------------判断链队列是否为空队列地址0x7fff5fbff540-------------

链队列为空,不能出队列

-------------进入链队列地址0x100202f90-------------

当前入队列的元素4

-------------判断链队列是否为空队列地址0x7fff5fbff560-------------

-------------出队列链队列地址0x100202f90-------------

-------------判断链队列是否为空队列地址0x7fff5fbff540-------------

当前出队列的元素:4

Program ended with exit code: 0

 

如有错误 敬请指正

如需转载 请注明出处 谢谢

获取源码



 

推荐阅读