数据结构队列的各种操作
2014-08-10 10:06
295 查看
#include"stdio.h" #include"stdlib.h" typedef int ElementType; typedef struct Queue { int rear,front; ElementType *elements; int MaxSize; }Queue; void InitQueue(Queue *Q,int sz)//初始化 { Q->MaxSize=sz; Q->elements=(ElementType *)malloc(sizeof(ElementType)*Q->MaxSize); Q->front=Q->rear=0; } void freeQueue(Queue *Q,int sz)//释放空间 { free(Q->elements); } void MakeEmpty(Queue *Q)//置空 { Q->front=Q->rear=0; } int Length(Queue *Q)//返回长度 { return (Q->rear-Q->front+Q->MaxSize)%(Q->MaxSize); } int IsFull(Queue *Q)//判断是否为满 { if (Q->rear!=0&&(Q->front==(Q->rear)%(Q->MaxSize))) return 1; else return 0; } int IsEmpty(Queue *Q)//判断是否为空 { if(Q->front==Q->rear) return 1; else return 0; } void EnQueue(Queue *Q)//进队 { ElementType item; scanf("%d",&item); while((!IsFull(Q))&&(item!=-1)) { Q->elements[Q->rear]=item; Q->rear=(Q->rear+1)%(Q->MaxSize); scanf("%d",&item); } } ElementType DeQueue(Queue *Q) { ElementType item; if(!IsEmpty(Q)) { item=Q->elements[Q->front]; Q->front=(Q->front+1)%(Q->MaxSize); return item; } else { printf("对空!\n"); exit(1); } } ElementType GetFront(Queue *Q) { if(!IsEmpty(Q)) return Q->elements[Q->front]; else { printf("队空!\n"); exit(1); } } void main() { int i; Queue Q; InitQueue(&Q,10); EnQueue(&Q); for(i=0;i<10;i++) if(!IsEmpty(&Q)) printf("%-5d",DeQueue(&Q)); }
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/817865
相关文章推荐
- 数据结构队列的各种操作
- 数据结构队列的各种操作
- 数据结构_队列_用链表动态建立释放节点实现队列各种操作_C++实现
- 数据结构队列的各种操作
- 数据结构队列的各种操作
- 【数据结构】 链式队列的一些操作
- 数据结构类型定义及基本操作汇总(一)--线性表,单链表,栈和队列
- 数据结构面试之四——队列的常见操作
- 【数据结构】第2周 栈与队列 2:栈的基本操作
- 顺序队列和链队列的各种操作
- 进击的数据结构之六队列的基本操作-顺序结构
- c++实现静态循环队列的各种操作
- 数据结构C语言实现—队列操作
- 数据结构面试之四——队列的常见操作
- 数据结构 链队列基本操作
- 数据结构面试之四——队列的常见操作
- C数据结构(文件操作,随机数,排序,栈和队列,图和遍历,最小生成树,最短路径)程序例子
- 顺序队列和链队列的各种操作
- 数据结构 队列的相关操作
- 数据结构-队列操作