链式队列 + 顺序队列 + 顺序循环队列
#include <stdio.h> #include <stdlib.h> #define OK 1 #define FALSE 0 #define ERROR -1 typedef int Status; typedef int QElemType; typedef struct QNode { QElemType data; QNode *next; }*QueuePtr; struct LinkQueue { QueuePtr front,rear;//队头,队尾指针 }; Status InitQueue(LinkQueue &Q) { Q.front->next = NULL; return OK; } Status DestroyQueue(LinkQueue &Q) { while(Q.front){ Q.rear = Q.front->next; free(Q.front); Q.front = Q.rear; } return OK; } Status ClearQueue(LinkQueue &Q) { QueuePtr p,q; Q.rear = Q.front; p = Q.front->next; Q.front->next = NULL; while(p){ q = p; p = p->next; free(q); } return OK; } Status QueueEmpty(LinkQueue Q){ if(Q.front == Q.rear) return true; else return false; } int QueueLength(LinkQueue Q){ int i = 0; QueuePtr p; p = Q.front; while(Q.rear != Q.front){ i++; p = p->next; } return i; } Status GetHead(LinkQueue Q,QElemType &e){ QueuePtr p; if(Q.front == Q.rear) return ERROR; p = Q.front->next; e = p->data; return OK; } Status EnQueue(LinkQueue &Q,QElemType e){ QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); p->data = e; p->next = NULL; Q.rear->next = p; Q.rear = p; return OK; } Status DeQueue(LinkQueue &Q,QElemType &e){ QueuePtr p; if(Q.front == Q.rear) return ERROR; p = Q.front->next; e = p->data; Q.front->next = p->next; if(Q.rear == p) Q.rear = Q.front; free(p); return OK; } Status QueueTraverse(LinkQueue Q,void(*vi)(QElemType)) { QueuePtr p; p = Q.front->next; while(p){ vi(p->data); p = p->next; } printf("\n"); return OK; } int main() { return 0; }
顺序队列:
//顺序队列 #include <stdio.h> #include <stdlib.h> #define OK 1 #define FALSE 0 #define ERROR -1 #define OVERFLOW -1 #define MAXQSIZE 20 typedef int Status; typedef int QElemType; struct SqQueue { QElemType *base; int front; int rear; }; Status InitQueue(SqQueue &Q){ Q.base = (QElemType *)malloc(MAXQSIZE * sizeof(QElemType)); // Q.front = Q.rear = 0; return OK; } Status DestroyQueue(SqQueue &Q){ if(Q.base) free(Q.base); Q.base = NULL; Q.front = Q.rear = 0; return 0; } Status ClearQueue(SqQueue &Q){ Q.front = Q.rear = 0; return OK; } Status QueueEmpty(SqQueue Q){ if(Q.front == Q.rear) return true; else return false; } int QueueLength(SqQueue Q){ return (Q.rear - Q.front); } Status GetHead(SqQueue Q,QElemType &e){ if(Q.front == Q.rear) return ERROR; e = *(Q.base + Q.front); return OK; } Status EnQueue(SqQueue &Q,QElemType e){ if(Q.rear >= MAXQSIZE) { Q.base = (QElemType *)realloc(Q.base,(Q.rear + 1) * sizeof(QElemType)); // } *(Q.base + Q.rear) = e; Q.rear++; return OK; } Status DeQueue(SqQueue &Q,QElemType e){ if(Q.front == Q.rear) return ERROR; e = Q.base[Q.front]; Q.front = (Q.front + 1); return OK; } Status QueueTraverse(SqQueue Q,void(*vi)(QElemType)){ int i ; i = Q.front; while(i != Q.rear){ vi(*(Q.base + i)); i++; } printf("\n"); return OK; }
顺序循环队列:
//顺序循环队列 #include <stdio.h> #include <stdlib.h> #define OK 1 #define FALSE 0 #define ERROR -1 #define OVERFLOW -1 #define MAXQSIZE 5 typedef int Status; typedef int QElemType; struct SqQueue { QElemType *base; int front; int rear; }; Status InitQueue(SqQueue &Q){ Q.base = (QElemType *)malloc(MAXQSIZE * sizeof(QElemType)); // Q.front = Q.rear = 0; return OK; } Status DestroyQueue(SqQueue &Q){ if(Q.base) free(Q.base); Q.base = NULL; Q.front = Q.rear = 0; return 0; } Status ClearQueue(SqQueue &Q){ Q.front = Q.rear = 0; return OK; } Status QueueEmpty(SqQueue Q){ if(Q.front == Q.rear) return true; else return false; } int QueueLength(SqQueue Q){ return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE; } Status GetHead(SqQueue Q,QElemType &e){ if(Q.front == Q.rear) return ERROR; e = *(Q.base + Q.front); return OK; } Status EnQueue(SqQueue &Q,QElemType e){ if((Q.rear + 1) % MAXQSIZE == Q.front) //队满 return ERROR; Q.base[Q.rear] = e; Q.rear = (Q.rear + 1) % MAXQSIZE; return OK; } Status DeQueue(SqQueue &Q,QElemType e){ if(Q.front == Q.rear) return ERROR; e = Q.base[Q.front]; Q.front = (Q.front + 1) % MAXQSIZE; return OK; } Status QueueTraverse(SqQueue Q,void(*vi)(QElemType)){ int i ; i = Q.front; while(i != Q.rear){ vi(*(Q.base + i)); i = (i + 1) % MAXQSIZE; } printf("\n"); return OK; }