前言
数据结构中顺序循环队列和链队列的C/C++语言描述实现模板,有详细的步骤解析及使用示例。
代码仓库
sqQueue.cpp(顺序循环队列)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| #include <iostream>
using namespace std;
#define MaxSize 100
typedef int ElemType;
typedef struct SqQueue { ElemType data[MaxSize]; int front; int rear; } SqQueue;
void use_example(); void init(SqQueue &sqQueue); bool enQueue(SqQueue &sqQueue, ElemType elem); bool deQueue(SqQueue &sqQueue, ElemType &elem);
int main() { use_example();
return 0; }
void use_example() { SqQueue sqQueue;
init(sqQueue); cout << sqQueue.front << endl; cout << sqQueue.rear << endl;
ElemType elem1 = 100; ElemType elem2 = 200; ElemType elem3 = 300;
enQueue(sqQueue, elem1); enQueue(sqQueue, elem2); enQueue(sqQueue, elem3);
ElemType elem4 = 0; ElemType elem5 = 0;
deQueue(sqQueue, elem4); deQueue(sqQueue, elem5);
cout << elem4 << endl; cout << elem5 << endl;
return; }
void init(SqQueue &sqQueue) { sqQueue.front = 0; sqQueue.rear = 0; }
bool enQueue(SqQueue &sqQueue, ElemType elem) { if ((sqQueue.rear + 1) % MaxSize == sqQueue.front) { return false; }
sqQueue.data[sqQueue.rear] = elem; sqQueue.rear = (sqQueue.rear + 1) % MaxSize;
return true; }
bool deQueue(SqQueue &sqQueue, ElemType &elem) { if (sqQueue.front == sqQueue.rear) { return false; }
elem = sqQueue.data[sqQueue.front]; sqQueue.front = (sqQueue.front + 1) % MaxSize;
return true; }
|
linkQueue.cpp(链队列)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| #include <iostream>
using namespace std;
typedef int ElemType;
typedef struct LinkNode { ElemType data; struct LinkNode *next; } LinkNode;
typedef struct LinkQueue { LinkNode *front; LinkNode *rear; } LinkQueue;
void use_example(); void init(LinkQueue &linkQueue); void enQueue(LinkQueue &linkQueue, ElemType elem); bool deQueue(LinkQueue &linkQueue, ElemType &elem);
int main() { use_example();
return 0; }
void use_example() { LinkQueue linkQueue;
init(linkQueue);
ElemType elem1 = 100; ElemType elem2 = 200; ElemType elem3 = 300;
enQueue(linkQueue, elem1); enQueue(linkQueue, elem2); enQueue(linkQueue, elem3);
ElemType elem4 = 0; ElemType elem5 = 0;
deQueue(linkQueue, elem4); deQueue(linkQueue, elem5);
cout << elem4 << endl; cout << elem5 << endl;
return; }
void init(LinkQueue &linkQueue) { linkQueue.front = linkQueue.rear = (LinkNode *)malloc(sizeof(LinkNode)); linkQueue.front->next = nullptr; }
void enQueue(LinkQueue &linkQueue, ElemType elem) { LinkNode *newNode;
newNode = (LinkNode *)malloc(sizeof(LinkNode));
newNode->data = elem;
linkQueue.rear->next = newNode; linkQueue.rear = newNode; }
bool deQueue(LinkQueue &linkQueue, ElemType &elem) { if (linkQueue.front == linkQueue.rear) { return false; }
LinkNode *tempNode = nullptr;
elem = linkQueue.front->next->data;
tempNode = linkQueue.front->next; linkQueue.front->next = tempNode->next;
if (linkQueue.rear == tempNode) { linkQueue.rear = linkQueue.front; }
free(tempNode);
return true; }
|
总结
不同参考资料中,顺序循环队列和链队列的描述实现各不相同,但基本思想是一致的。作者使用规范的变量命名、提供详细的步骤解析及使用示例,应用C/C++语言将其整合成模板,以帮助理解记忆。
参考资料
- 《2023版数据结构高分笔记》主编:率辉
- 《2023年计算机组成原理考研复习指导》组编:王道论坛
- 《大话数据结构》作者:程杰
作者的话
- 感谢参考资料的作者/博主
- 作者:夜悊
- 版权所有,转载请注明出处,谢谢~
- 如果文章对你有帮助,请点个赞或加个粉丝吧,你的支持就是作者的动力~
- 文章在描述时有疑惑的地方,请留言,定会一一耐心讨论、解答
- 文章在认识上有错误的地方, 敬请批评指正
- 望读者们都能有所收获