赞
踩
队列的顺序存储结构:
#include<stdio.h> #define ElemType int #define MAXSIZE 5 typedef struct Node { ElemType* base; int front; int rear; }QueueLink; QueueLink* Init(QueueLink* q) { q->base = new ElemType[MAXSIZE]; q->front = q->rear = 0; return q; } QueueLink* enQueue(QueueLink* q, ElemType data) { if (((q->rear + 1) % MAXSIZE) == q->front) return q; q->base[q->rear] = data; q->rear = (q->rear + 1) % MAXSIZE; return q; } QueueLink* deQueue(QueueLink* q, ElemType& data) { if (q->front == q->rear) return q; data = q->base[q->front]; q->front = (q->front + 1) % MAXSIZE; return q; } int getQueueLength(QueueLink* q) { return (q->rear - q->front + MAXSIZE) % MAXSIZE; } void displayQueue(QueueLink* q) { int temp = q->front; while (temp != q->rear) { printf("%d ", q->base[temp]); temp = (temp+1) % MAXSIZE; } }
队列的链式存储结构
#include<stdio.h> #define ElemType int typedef struct QNode { ElemType data; struct QNode* next; }QLinkNode; typedef struct { QLinkNode* front; QLinkNode* rear; }LinkQueue; LinkQueue* Init(LinkQueue* lq) { lq->front = lq->rear = new QLinkNode(); lq->front->next = NULL; return lq; } LinkQueue* enQueue(LinkQueue* lq, ElemType data) { QLinkNode* node = new QLinkNode(); node->data = data; node->next = NULL; lq->rear->next = node; lq->rear = node; return lq; } LinkQueue* deQueue(LinkQueue* lq, ElemType& data) { if (lq->rear == lq->front) return lq; QLinkNode* node = lq->front->next; data = node->data; lq->front->next = node->next; if (lq->rear == node) lq->rear = lq->front; delete node; return lq; } void display(LinkQueue* lq) { QLinkNode* p = lq->front->next; while (p!=NULL) { printf("%d ", p->data); p = p->next; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。