//队列数据类型定义
typedef int ElementType;
#ifndef _Queue_h
struct QueueRecord;
typedef struct QueueRecord *Queue;
int IsEmpty(Queue Q);
int IsFull(Queue Q);
Queue CreateQueue(int MaxElements);
void DisposeQueue(Queue Q);
void MakeEmpty(Queue Q);
void Enqueue(ElementType X,Queue Q);
ElementType Front(Queue Q);
void Dequeue(Queue Q);
ElementType FrontAndDequeue(Queue Q);
#endif
//queue.c
#include "queue.h"
#include "fatal.h"
#define MinQueueSize (5)
struct QueueRecord
{
int Capacity;
int Front;
int Rear;
int Size;
ElementType *Array;
};
int IsEmpty(Queue Q)
{
return Q->Size==0;
}
int IsFull(Queue Q)
{
return Q->Size==Q->Capacity;
}
//创建队列
Queue CreateQueue(int MaxElements)
{
Queue Q;
if(MaxElements<MinQueueSize)
Error("Queue size is too small");
Q=malloc(sizeof(struct QueueRecord));
if(Q==NULL)
FatalError("Out of space");
Q->Array=malloc(sizeof(ElementType)*MaxElements);
if(Q->Array==NULL)
FatalError("Out of space");
Q->Capacity=MaxElements;
MakeEmpty(Q);
return Q;
}
void MakeEmpty(Queue Q)
{
Q->Size=0;
Q->Front=1;
Q->Rear=0;
}
void DisposeQueue(Queue Q)
{
if(Q!=NULL)
{
free(Q->Array);
free(Q);
}
}
//队头和队尾的设置
static int Succ(int Value,Queue Q)
{
if(++Value==Q->Capacity)
Value=0;
return Value;
}
void Enqueue(ElementType X,Queue Q)
{
if(IsFull(Q))
Error("Full Queue");
else
{
Q->Size++;
Q->Rear=Succ(Q->Rear,Q);
Q->Array[Q->Rear]=X;
}
}
ElementType Front(Queue Q)
{
if(!IsEmpty(Q))
return Q->Array[Q->Front];
Error("Empty queue");
return 0;
}
void Dequeue(Queue Q)
{
if(IsEmpty(Q))
Error("Empty queue");
else
{
Q->Size--;
Q->Front=Succ(Q->Front,Q);
}
}
ElementType FrontAndDequeue(Queue Q)
{
ElementType X=0;
if(IsEmpty(Q))
Error("Empty queue");
else
{
Q->Size--;
X=Q->Array[Q->Front];
Q->Front=Succ(Q->Front,Q);
}
return X;
}
分享到:
相关推荐
c语言数据结构-栈队列数组完美版资料.ppt
队列是一种先入先出的数据结构(FIFO),只允许在前端(front)删除,在后端(rear)插入。容量为capacity大小的内存,只能存capacity-1的元素,其中rear的位置始终为空。 本文实现的队列,功能如下: 1 获取元素内容 ...
用队列+数组完成的停车管理系统,也许有点low,但是可以参考一下,多多交流。
完整代码 正确产生结果 三个类分开写 class linklist { protected: struct node { int data; node *next; }; node *head; int length; public:
由数组实现队列,包括队列的创建、入队和出队。通过打印显示出队的结果。正在学习数据结构的童鞋可以参考。
学习数据结构过程中,亲自在VC++上编译通过的使用数组实现队列的源代码,与大家共享。
数据结构-03-123栈队列数组-691.ppt
数据结构的定义 数据结构是计算机存储、组织数据的方式,用于高效地访问和修改数据。...Java提供了丰富的数据结构库,包括数组、链表、栈、队列等,这些数据结构为程序员提供了处理各种问题的工具和方法。
go语言通过数组和链表的方式实现队列 数组和链表.pdf
数组、链表、队列、栈数据结构特点,各自优点和缺点 数组和链表.pdf
数据结构-基本算法-循环队列(学生时代源码,调试可运行)
常见的数据结构(栈、队列、数组、链表和红黑树) 数组和链表.pdf
数据结构与算法 数据结构与C语言 第3章 栈和队列(共150页).ppt 数据结构与算法 数据结构与C语言 第4章 串、数组和广义表(共66页).ppt 数据结构与算法 数据结构与C语言 第5章 树和二叉树(共95页).ppt 数据结构...
在队列的代码中,引用了链表的代码
主要介绍了JavaScript数组实现数据结构中的队列与堆栈的相关资料,需要的朋友可以参考下
数据结构知识,分别介绍了数组,栈,队列。
数据结构双向队列实验 用数组和链表实现的。
利用C++实现以下经典数据结构与算法:线性表(顺序表、链表、静态链表、三元组)、栈(双栈、共享栈)、队列(任务调度、循环队列、双向队列、链队列)、数组(特殊矩阵、稀疏矩阵压缩)、串(朴素模式匹配、KMP算法...
9.定义并实现循环队列的数据结构。 10.应用队列完成Johnson问题(n个人围成一圈,每个人都有一个号码,从1..n,从1号报数,报到m号的出列,输出出列的号码顺序)。测试:10个人,报到3的出列。
数据结构,队列的实现,包括链式和数组式 别的不多说,自己看吧,上传也挺麻烦