标签:队列
#pragma once #include<stdio.h> #include<stdlib.h> #define ElemType int #define SizeFull 20 typedef struct Queue { ElemType *data; int sizefull; int front; int rear; }Queue; void Show(); void InitQueue(Queue *Q); void DestoryQueue(Queue *Q); void ClearQueue(Queue *Q); void QueueEmpty(Queue *Q); int QueueLength(Queue *Q); void GetQueue(Queue *Q,ElemType *x); void EnQueue(Queue *Q,ElemType x); void DeQueue(Queue *Q); void ShowQueue(Queue *Q);
#include"Queue.h" void InitQueue(Queue *Q) { Q->data=(ElemType*)malloc(sizeof(ElemType)*SizeFull); if(Q->data==NULL) { printf("开辟内存失败\n"); return; } Q->sizefull=SizeFull; Q->front=Q->rear=0; } void Show() { printf("************************************************\n"); printf("*[1]: 入栈 [2]: 出栈 *\n"); printf("*[3]: 栈长度 [4]: 摧毁栈 *\n"); printf("*[5]: 清理栈 [6]: 显示栈 *\n"); printf("*[0]: 退栈 *\n"); printf("************************************************\n"); printf("请选择:"); } void DestoryQueue(Queue *Q) { free(Q->data); Q->front=Q->rear=Q->sizefull=0; } void ClearQueue(Queue *Q) { Q->front=0; Q->rear=0; } bool IsFull(Queue *Q) { return Q->rear>=Q->sizefull; } bool IsEmpty(Queue *Q) { return Q->rear==0; } int QueueLength(Queue *Q) { return Q->rear; } void EnQueue(Queue *Q,ElemType x) { if(IsFull(Q)) { printf("空间已满\n"); return; } Q->data[Q->rear++]=x; } void DeQueue(Queue *Q) { if(IsEmpty(Q)) { printf("队列为空,无法删除\n"); return; } Q->front++; } void ShowQueue(Queue *Q) { for(int i=Q->front;i<Q->rear;i++) { printf("%d ",Q->data[i]); } printf("\n"); }
#include"Queue.h" void main() { Queue Q; InitQueue(&Q); ElemType x; int Length; int pos=1; while(pos) { Show(); scanf("%d",&pos); switch(pos) { case 1: printf("请输入数据,以-1结束:"); while(scanf("%d",&x),x!=-1) { EnQueue(&Q,x); } break; case 2: DeQueue(&Q); break; case 3: Length=QueueLength(&Q); printf("Length=%d\n",Length); break; case 4: DestoryQueue(&Q); break; case 5: ClearQueue(&Q); break; case 6: ShowQueue(&Q); break; default : break; } } }
<img src="http://img.blog.csdn.net/20150511231648250?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhvdTc1MzA5OTk0Mw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
标签:队列
原文地址:http://blog.csdn.net/zhou753099943/article/details/45652033