队列 - 链式存储结构
队列的链式存储结构,其实就是线性表的单链表,只不过它只能尾进头出而已,我们把它简称为链队列。为了操作上的方便,我们将队头指针指向链队列的头节点,而队尾指针指向终端节点。空队列时,front和rear都指向头节点。
示例程序:(改变自《大话数据结构》)
C++ Code
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
#include<iostream>
using namespace std; typedef int ElemType; typedef struct Node { ElemType data; struct Node *next; } Node, *NodePtr; typedef struct { NodePtr front;/* 队头、队尾指针 */ NodePtr rear; } LinkQueue; /* 构造一个空队列 */ bool InitQueue(LinkQueue *Lp) { cout << "Init Queue ..." << endl; NodePtr p = (NodePtr)malloc(sizeof(Node)); p->next = NULL; Lp->front = Lp->rear = p; return true; } /* 销毁队列,包括头节点 */ bool DestroyQueue(LinkQueue *Lp) { cout << "Destroy Queue ..." << endl; while (Lp->front) { Lp->rear = Lp->front->next; free(Lp->front); Lp->front = Lp->rear; } return true; } /* 清为空队列,保留头节点 */ bool ClearQueue(LinkQueue *Lp) { cout << "Clear Queue ..." << endl; NodePtr p = Lp->front->next; Lp->front->next = NULL; Lp->rear = Lp->front; NodePtr q; while (p) { q = p->next; free(p); p = q; } return true; } bool QueueEmpty(LinkQueue LQ) { return LQ.front == LQ.rear; } int QueueLength(LinkQueue LQ) { int i = 0; if (LQ.front == NULL) return 0; NodePtr p = LQ.front->next; while (p) { ++i; p = p->next; } return i; } bool GetHead(LinkQueue LQ, ElemType *pe) { NodePtr p; if (LQ.front == LQ.rear) return false; p = LQ.front->next; *pe = p->data; cout << "Get Head Item : " << *pe << endl; return true; } /* 插入元素Elem为队列的新的队尾元素 */ bool EnQueue(LinkQueue *Lp, ElemType Elem) { cout << "EnQueue Item " << Elem << endl; NodePtr s = (NodePtr)malloc(sizeof(Node)); s->data = Elem; s->next = NULL; Lp->rear->next = s; Lp->rear = s; return true; } /*删除队列的队头元素,用*pe返回其值 */ bool DeQueue(LinkQueue *Lp, ElemType *pe) { if (Lp->front == Lp->rear) return false; NodePtr p = Lp->front->next; *pe = p->data; cout << "DeQueue Item " << *pe << endl; Lp->front->next = p->next; if (Lp->rear == p)/* 若队头就是队尾,则删除后将rear指向头结点*/ Lp->rear = Lp->front; free(p); return true; } /* 从队头到队尾依次对队列中每个元素输出 */ bool QueueTraverse(LinkQueue LQ) { cout << "Queue Traverse ..." << endl; NodePtr p = LQ.front->next; while (p) { cout << p->data << ' '; p = p->next; } cout << endl; return true; } int main(void) { LinkQueue LQ; InitQueue(&LQ); for (int i = 0; i < 5; i++) EnQueue(&LQ, i); QueueTraverse(LQ); int result; GetHead(LQ, &result); DeQueue(&LQ, &result); QueueTraverse(LQ); if (!QueueEmpty(LQ)) cout << "Queue Length : " << QueueLength(LQ) << endl; /*ClearQueue(&LQ);*/ DestroyQueue(&LQ); cout << "Queue Length : " << QueueLength(LQ) << endl; return 0; } |
总的来说,如果可以确定队列的最大值,建议用循环队列,如果不能预估队列的长度,则用链队列。