本文共 3966 字,大约阅读时间需要 13 分钟。
队列可以用两种方法实现,数组或者链表。
用数组实现的称作循环队列,
用链表实现的称作链式队列。
理论上,队列的一个特征就是没有特定的容量。不管已经存在多少元素,新的元素总能入列,也可以清空。
固定长度的数组限制了这个能力,并且低效,因为元素需要朝队首方向复制。现在的语言支持对象和指针的可以通过动态列表实现队列。往一个满的队列中插入元素会导致队列溢出,往队列删除元素会导致队列下溢。
以下代码引用自wikipedia
Example using CThis C example uses a singly and . 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->data = value; if (q->first == NULL) q->first = q->last = node; else { q->last->next = node; q->last = node; } node->next = NULL; return 0; } int dequeue(struct queue *q, int *value) { if (!q->first) { *value = 0; return 1; } *value = q->first->data; struct queue_node *tmp = q->first; if (q->first == q->last) q->first = q->last = NULL; else q->first = q->first->next; free(tmp); return 0; } void init_queue(struct queue *q) { q->first = q->last = NULL; } int queue_empty_p(const struct queue *q) { return q->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; } [ ] Example C++ language code using arrayThis 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 <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 < head+ queuelength; index ++) { cout << Q[(index+QMAX) % QMAX]->value << "-"; } cout << endl; } void PrintArray() { for(int i=0; i< 10; i++) { if(Q[i]!=NULL) { cout<< Q[i]->value << "-"; } else { cout << "00" << "-"; } } cout << 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<=20) { int rand = generateRandNum(); if(rand<=60) { Node* n = (Node*)malloc(sizeof(Node)); n->value=generateRandNum(); n->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,如需转载请自行联系原作者