数据结构队列的各种操作
2012-03-26 21:45
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/817866
相关文章推荐
- 数据结构_队列_用链表动态建立释放节点实现队列各种操作_C++实现
- 数据结构队列的各种操作
- 数据结构队列的各种操作
- 数据结构队列的各种操作
- 数据结构队列的各种操作
- 数据结构复习之用两个栈模拟队列操作
- c++实现静态循环队列的各种操作
- 进击的数据结构之六队列的基本操作-顺序结构
- 跟着郝斌学数据结构(07)——队列(操作及应用)
- 数据结构 队列的基本操作
- 【数据结构】用C++实现双链表的各种操作(包括头删,尾删,插入,逆序,摧毁,清空等等)
- 数据结构-循环队列的基本操作
- 【数据结构】队列的出队和入队操作
- 数据结构 循环队列的基本操作
- Java 数据结构之数组的操作三:实现各种排序方法
- 【数据结构】 链式队列的一些操作
- 数据结构-单链队列操作算法
- 队列的基本操作概念及各种实现方式的代码
- 数据结构_链队列相关操作
- 数据结构--链队列基本操作