队列的顺序实现
#include #include #include #define MaxSize 10typedef struct{int front,rear;int data[MaxSize];}SqQueue;void InitQueue(SqQueue &Q){Q.front=Q.rear=0;} bool QueueEmpty(SqQueue Q){if(Q.front==Q.rear) return true;else return false; }bool EnQueue(SqQueue &Q,int x){if((Q.rear+1)%MaxSize==Q.front){return false; }Q.data[Q.rear]=x;Q.rear = (Q.rear+1)%MaxSize;return true;} int DeQueue(SqQueue &Q){if(Q.front==Q.rear) return false;int x;x = Q.data[Q.front];Q.front=(Q.front+1)%MaxSize;return x;}int GetHead(SqQueue &Q){if(Q.front==Q.rear) return false;int x;x = Q.data[Q.front];return x;} int CountQueue(SqQueue &Q){if(Q.front==Q.rear) return false;int count;count = (Q.rear+MaxSize-Q.front)%MaxSize;return count;} bool PrintSqQueue(SqQueue &Q){if(Q.front==Q.rear) return false;for(int i=Q.front;i<Q.rear;i++)printf("%d ",Q.data[i]);printf("\n");return true;}int main(){SqQueue Q;InitQueue(Q);printf("-----入队-----\n");EnQueue(Q,1);EnQueue(Q,2);EnQueue(Q,3);EnQueue(Q,4);EnQueue(Q,5);int count;count=CountQueue(Q);printf("队内元素个数为:%d\n",count);PrintSqQueue(Q);printf("-----出队-----\n");int x;x=DeQueue(Q);printf("%d出队\n",x);x=DeQueue(Q);printf("%d出队\n",x);x=DeQueue(Q);printf("%d出队\n",x);count=CountQueue(Q);printf("队内元素个数为:%d\n",count);PrintSqQueue(Q);return 0;}
队列的链式实现
#include #include #include typedef struct LinkNode{int data;struct LinkNode *next;}LinkNode; typedef struct{LinkNode *front,*rear;}LinkQueue;void InitQueue1(LinkQueue &Q){Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode));Q.front->next=NULL;}void InitQueue2(LinkQueue &Q){Q.front=NULL;Q.rear=NULL;} bool IsEmpty1(LinkQueue Q){if(Q.front==Q.rear) return true;else return false; } bool IsEmpty2(LinkQueue Q){if(Q.front==NULL) return true;else false;} void EnQueue1(LinkQueue &Q,int x){LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));s->data=x;s->next=NULL;Q.rear->next=s;Q.rear=s;}void EnQueue2(LinkQueue &Q,int x){LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));s->data=x;s->next=NULL;if(Q.front==NULL){Q.front=s;Q.rear=s;}else{Q.rear->next=s;Q.rear=s;}}int DeQueue1(LinkQueue &Q){if(Q.front==Q.rear){return false;}LinkNode *p=Q.front->next;int x;x=p->data;Q.front->next = p->next;if(Q.rear==p) Q.rear=Q.front;free(p);return x ;} int DeQueue2(LinkQueue &Q){if(Q.front==NULL) return false;LinkNode *p=Q.front;int x;x=p->data; Q.front=p->next;if(Q.rear==p){Q.front=NULL;Q.rear=NULL;}free(p);return x; } void PrintQueue1(LinkQueue &Q){LinkNode *temp=Q.front->next;printf("打印队列:");while(temp){printf("%d ",temp->data);temp = temp->next;}printf("\n");}void PrintQueue2(LinkQueue &Q){LinkNode *temp=Q.front;printf("打印队列:");while(temp){printf("%d ",temp->data);temp = temp->next;}printf("\n");}int main(){printf("--------带头结点-------\n");LinkQueue Q;InitQueue1(Q);printf("开始进队列\n");EnQueue1(Q,1);EnQueue1(Q,2);EnQueue1(Q,3);EnQueue1(Q,4);EnQueue1(Q,5);PrintQueue1(Q);if (!IsEmpty1(Q)) printf("非空队列\n");else printf("空队列\n");int x;x=DeQueue1(Q);printf("%d出队列\n",x);x=DeQueue1(Q);printf("%d出队列\n",x);x=DeQueue1(Q);printf("%d出队列\n",x);PrintQueue1(Q);printf("--------不带头结点-------\n");LinkQueue P;InitQueue2(P);printf("开始进队列\n");EnQueue2(P,1);EnQueue2(P,2);EnQueue2(P,3);EnQueue2(P,4);EnQueue2(P,5);PrintQueue2(P);if (!IsEmpty2(P)) printf("非空队列\n");else printf("空队列\n");int y;y=DeQueue2(P);printf("%d出队列\n",y);y=DeQueue2(P);printf("%d出队列\n",y);y=DeQueue2(P);printf("%d出队列\n",y);PrintQueue2(P);return 0;}