顺序循环队列
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
| #include <stdio.h> #include <stdlib.h>
#define MAXQSIZE 100
#define TURE 1 #define FALSE 0 #define OK 1 #define ERROR 0
#define INFEASIBLE -1
#define OVEREFLOW -2
typedef int Status;
typedef char QElemType;
typedef struct { QElemType* base; int front; int rear; }SqQueue;
Status initQueue(SqQueue& Q) { Q.base = new QElemType(MAXQSIZE); if (!Q.base) exit(OVEREFLOW); Q.front = Q.rear = 0; return OK; }
int QueueLength(SqQueue& Q) { return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE; }
Status EnQueue(SqQueue& Q, QElemType e) { if ((Q.rear + 1) % MAXQSIZE == Q.front) exit(OVEREFLOW); Q.base[Q.rear] = e; Q.rear = (Q.rear + 1) % MAXQSIZE; return OK; }
Status InQueue(SqQueue& Q, QElemType& e) { if (Q.front == Q.rear) return ERROR; e = Q.base[Q.front]; Q.front = (Q.front + 1) % MAXQSIZE; return OK; }
QElemType getTop(SqQueue& Q) { if (Q.front != Q.rear) { return Q.base[Q.front]; } }
int main() { SqQueue Q; initQueue(Q); for (int i = 0; i < 2; i++) { EnQueue(Q, i); } printf("队列长度:%d\n", QueueLength(Q)); QElemType e; InQueue(Q, e); printf("出队元素:%d\n", e); printf("队头元素:%d\n", getTop(Q)); return 0; }
|
链队
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
| #include <stdio.h> #include <stdlib.h>
#define TURE 1 #define FALSE 0 #define OK 1 #define ERROR 0
#define INFEASIBLE -1
#define OVEREFLOW -2
typedef int Status;
typedef char QElemType;
typedef struct Qnode { QElemType data; struct Qnode* next; }QNode, *QueuePtr;
typedef struct { QueuePtr front; QueuePtr rear; }LinkQueue;
Status initQueue(LinkQueue& Q) { Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode)); if (!Q.front) exit(OVEREFLOW); Q.front->next = NULL; return OK; }
Status DestoryQueue(LinkQueue& Q) {
while (Q.front) { Q.rear = Q.front->next; free(Q.front); Q.front = Q.rear; } return OK; }
Status EnQueue(LinkQueue& Q, QElemType e) { QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); if (!p) exit(OVEREFLOW); p->data = e; p->next = NULL; Q.rear->next = p; Q.rear = p; return OK; }
Status InQueue(LinkQueue& Q, QElemType& e) { if (Q.rear == Q.front) return ERROR; QueuePtr p = Q.front->next; e = p->data; Q.front->next = p->next; if (Q.rear == p) Q.rear = Q.front; free(p); return OK; }
QElemType getHead(LinkQueue& Q) { if (Q.rear == Q.front) return ERROR; return Q.front->next->data; }
int main(){ LinkQueue Q; initQueue(Q); for (int i = 0; i < 2; i++) { EnQueue(Q, i); } QElemType e; InQueue(Q, e); printf("出队元素:%d\n", e); printf("队头元素:%d\n", getHead(Q)); DestoryQueue(Q); return 0; }
|
版权声明: 此文章版权归霜屿清所有,如有转载,请注明来自原作者