队列的实现

简介:

队列可以用两种方法实现,数组或者链表。

用数组实现的称作循环队列,

用链表实现的称作链式队列。

理论上,队列的一个特征就是没有特定的容量。不管已经存在多少元素,新的元素总能入列,也可以清空。

固定长度的数组限制了这个能力,并且低效,因为元素需要朝队首方向复制。现在的语言支持对象和指针的可以通过动态列表实现队列。往一个满的队列中插入元素会导致队列溢出,往队列删除元素会导致队列下溢。

以下代码引用自wikipedia

http://en.wikipedia.org/wiki/Queue_%28data_structure%29

Example using C

This C example uses a singly linked list and dynamic memory. It's an efficient way to implement linear access containers.

#include <stdio.h> #include <errno.h> #include <stdlib.h> struct queue_node { struct queue_node *next; int data; }; struct queue { struct queue_node *first; struct queue_node *last; }; int enqueue(struct queue *q, const int value) { struct queue_node *node = malloc(sizeof(struct queue_node)); if (node == NULL) { errno = ENOMEM; return 1; } node-&gt;data = value; if (q-&gt;first == NULL) q-&gt;first = q-&gt;last = node; else { q-&gt;last-&gt;next = node; q-&gt;last = node; } node-&gt;next = NULL; return 0; } int dequeue(struct queue *q, int *value) { if (!q-&gt;first) { *value = 0; return 1; } *value = q-&gt;first-&gt;data; struct queue_node *tmp = q-&gt;first; if (q-&gt;first == q-&gt;last) q-&gt;first = q-&gt;last = NULL; else q-&gt;first = q-&gt;first-&gt;next; free(tmp); return 0; } void init_queue(struct queue *q) { q-&gt;first = q-&gt;last = NULL; } int queue_empty_p(const struct queue *q) { return q-&gt;first == NULL; } int main(void) { struct queue Q; init_queue(&Q); srand(time(NULL)); unsigned int k = 0; while (k < 100) { enqueue(&Q, (rand() % 100) + 1); /* Fill with random numbers from 1 to 100 */ ++k; } k = 1; while (!queue_empty_p(&Q)) { int data; dequeue(&Q, &data); printf("(%d) %d\n", k, data); ++k; } putchar('\n'); return 0; } [ edit ] Example C++ language code using array

This C++ language example uses a fixed length array and global variables for conceptual simplicity.

// global data structure for simplicity // CppInterview.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include &lt;iostream> #include "math.h" #include <time.h> using namespace std; #define QMAX 10 struct Node { int value; Node* next; }; Node* Q[QMAX]; int head=0; int tail=0; // return 1 if successful, otherwise 0 int enqueue(Node* p) { if(tail<head+QMAX) { Q[tail]=p; tail= (tail+1)%QMAX; return 1; } else { return 0; } } // return 1 if successful, otherwise 0 int dequeue(Node** p) { if(tail == head) { return 0; } *p = Q[head]; Q[head] =NULL; head++; head %=QMAX; return 1; } void printqueue() { int index = head; int queuelength = (tail+QMAX-head) % QMAX; for(int index=head; index &lt; head+ queuelength; index ++) { cout &lt;&lt; Q[(index+QMAX) % QMAX]->value <&lt; "-"; } cout &lt;&lt; endl; } void PrintArray() { for(int i=0; i&lt; 10; i++) { if(Q[i]!=NULL) { cout&lt;&lt; Q[i]->value <&lt; "-"; } else { cout &lt;&lt; "00" &lt;&lt; "-"; } } cout &lt;&lt; endl; } // generate random number from 1-100 int generateRandNum() { ///* initialize random seed: */ //srand ( time(NULL) ); /* generate number of nodes: */ return rand() % 100 + 1; } int main(int argc, char* argv[]) { int i=1; while(i&lt;=20) { int rand = generateRandNum(); if(rand&lt;=60) { Node* n = (Node*)malloc(sizeof(Node)); n->value=generateRandNum(); n-&gt;next=NULL; enqueue(n); } else { Node* p; if(dequeue(&p)!=0) { free(p); } } //printqueue(); PrintArray(); i++; } return 0; }

The sample output looks like:

68-00-00-00-00-00-00-00-00-00- 68-1-00-00-00-00-00-00-00-00- 00-1-00-00-00-00-00-00-00-00- 00-1-79-00-00-00-00-00-00-00- 00-1-79-63-00-00-00-00-00-00- 00-00-79-63-00-00-00-00-00-00- 00-00-79-63-46-00-00-00-00-00- 00-00-00-63-46-00-00-00-00-00- 00-00-00-63-46-62-00-00-00-00- 00-00-00-00-46-62-00-00-00-00- 00-00-00-00-00-62-00-00-00-00- 00-00-00-00-00-62-28-00-00-00- 00-00-00-00-00-62-28-92-00-00- 00-00-00-00-00-62-28-92-3-00- 00-00-00-00-00-62-28-92-3-93- 00-00-00-00-00-00-28-92-3-93- 17-00-00-00-00-00-28-92-3-93- 17-96-00-00-00-00-28-92-3-93- 17-96-27-00-00-00-28-92-3-93- 17-96-27-00-00-00-00-92-3-93- Press any key to continue . . .















本文转自cnn23711151CTO博客,原文链接: http://blog.51cto.com/cnn237111/619565,如需转载请自行联系原作者


相关文章
|
9月前
|
缓存
指令缓存队列
指令缓存队列
37 0
|
4月前
队列的实现
队列的实现
|
4月前
|
C++
c++ 队列
队列的数据结构
21 0
|
5月前
12 队列
12 队列
17 0
|
6月前
|
算法
|
9月前
|
存储
队列的实现(下)
队列的实现(下)
|
9月前
|
机器学习/深度学习 存储 C语言
队列的实现(上)
队列的实现(上)
|
存储
队列的使用
队列的使用
62 0
|
前端开发 数据安全/隐私保护
队列
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。
|
存储
队列?是你了解的这样吗?
队列?是你了解的这样吗?
84 0
队列?是你了解的这样吗?