顺序循环队列的基本操作

#include

#include

#define OK 1

#define ERROR 0

typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等

typedef int QElemType;

#define MAXQSIZE 100 // 最大队列长度(对于循环队列,最大队列长度要减1)

typedef struct

{

QElemType *base; // 初始化的动态分配存储空间

int front; // 头指针,若队列不空,指向队列头元素

int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置

} SqQueue;

//创建队列

Status InitQueue(SqQueue &Q)

{

// 构造一个空队列Q,该队列预定义大小为MAXQSIZE

Q.base=(QElemType *)malloc(sizeof(SqQueue)*MAXQSIZE);

if(Q.base==NULL)return ERROR;

Q.front=Q.rear=0;

return OK;

}

//入队

Status EnQueue(SqQueue &Q,QElemType e)

{

// 插入元素e为Q的新的队尾元素

if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR;    //队满则不插入

Q.base[Q.rear]=e;

Q.rear=(Q.rear+1)%MAXQSIZE;

return OK;

}

//出队

Status DeQueue(SqQueue &Q, QElemType &e)

{

// 若队列不空, 则删除Q的队头元素, 用e返回其值, 并返回OK; 否则返回ERROR

if(Q.front==Q.rear)return ERROR;

e=Q.base[Q.front];

Q.front=(Q.front+1)%MAXQSIZE;

return OK;

}

//取对头元素

Status GetHead(SqQueue Q, QElemType &e)

{

// 若队列不空,则用e返回队头元素,并返回OK,否则返回ERROR

if(Q.front==Q.rear)return OK;

e=Q.base[Q.front];

return OK;

}

//计算队列的元素个数

int QueueLength(SqQueue Q)

{

return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;

}

//判断队列是否为空

Status QueueTraverse(SqQueue Q)

{

// 若队列不空,则从队头到队尾依次输出各个队列元素,并返回OK;否则返回ERROR.

int i;

i=Q.front;

if(i==Q.rear)printf("The Queue is Empty!");  //请填空

else

{

printf("The Queue is: ");

while(i!=Q.rear)     //请填空

{

printf("%d ",Q.base[i]);   //请填空

i = (i+1)%MAXQSIZE;   //请填空

}

}

printf("\n");

return OK;

}

//主函数

int main()

{

int a;

SqQueue S;

QElemType x, e;

if(InitQueue(S))    // 判断顺序表是否创建成功,请填空

{

printf("A Queue Has Created.\n");

}

while(1)

{

printf("1:Enter \n2:Delete \n3:Get the Front \n4:Return the Length of the Queue\n5:Load the Queue\n0:Exit\nPlease choose:\n");

scanf("%d",&a);

switch(a)

{

case 1:

scanf("%d", &x);

if(!EnQueue(S,x)) printf("Enter Error!\n"); // 判断入队是否合法,请填空

else printf("The Element %d is Successfully Entered!\n", x);

break;

case 2:

if(!DeQueue(S,e)) printf("Delete Error!\n"); // 判断出队是否合法,请填空

else printf("The Element %d is Successfully Deleted!\n", e);

break;

case 3:

if(!GetHead(S,e))printf("Get Head Error!\n"); // 判断Get Head是否合法,请填空

else printf("The Head of the Queue is %d!\n", e);

break;

case 4:

printf("The Length of the Queue is %d!\n",QueueLength(S));  //请填空

break;

case 5:

QueueTraverse(S); //请填空

break;

case 0:

return 1;

}

}

}

时间: 2024-08-28 13:01:47

顺序循环队列的基本操作的相关文章

数据结构Java实现07----队列:顺序队列&顺序循环队列、链式队列、顺序优先队列

数据结构Java实现07----队列:顺序队列&顺序循环队列.链式队列.顺序优先队列 一.队列的概念: 队列(简称作队,Queue)也是一种特殊的线性表,队列的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置插入和删除,而队列只允许在其一端进行插入操作在其另一端进行删除操作. 队列中允许进行插入操作的一端称为队尾,允许进行删除操作的一端称为队头.队列的插入操作通常称作入队列,队列的删除操作通常称作出队列. 下图是一个依次向队列中插入数据元素a0,a1,...,an-

数据结构--顺序循环队列和链式队列

第一部分:顺序循环队列的实现 1 //循环队列的实现 2 #define OK 1 3 #define MAXSIZE_Q 10 4 //#define OVERFLOW -2 5 #define ERROR 0 6 7 typedef int Status; 8 typedef int QElemtype; 9 10 typedef struct{ 11 QElemtype *base; //初始化的动态分配存储空间 12 int front; //队头指针 13 int rear; //队尾

链式队列 + 顺序队列 + 顺序循环队列

#include <stdio.h> #include <stdlib.h> #define OK 1 #define FALSE 0 #define ERROR -1 typedef int Status; typedef int QElemType; typedef struct QNode { QElemType data; QNode *next; }*QueuePtr; struct LinkQueue { QueuePtr front,rear;//队头,队尾指针 };

_DataStructure_C_Impl:顺序循环队列

//_DataStructure_C_Impl:顺序循环队列 #include<stdio.h> #include<stdlib.h> #define QueueSize 10 //定义顺序循环队列的最大容量 typedef char DataType; typedef struct Squeue{ //顺序循环队列的类型定义 DataType queue[QueueSize]; int front,rear; //队头指针和队尾指针 int tag; //队列空.满的标志 }SC

顺序循环队列的c语言实现

1. 循环队列的顺序存储结构 typedef struct { QElemType data[MAXSIZE]; int front; /* 头指针 */ int rear; /* 尾指针,若队列不空,指向队列尾元素的下一个位置 */ }SqQueue; 2. 初始化一个空队列Q Status InitQueue(SqQueue *Q) { Q->front=0; Q->rear=0; return OK; } 3.将Q清为空队列 Status ClearQueue(SqQueue *Q) {

顺序循环队列

一  顺序表循环队列 1.1 顺序循环队列定义 队列是一种运算受限的先进先出线性表,仅允许在队尾插入(入队),在队首删除(出队).新元素入队后成为新的队尾元素,元素出队后其后继元素就成为队首元素. 队列的顺序存储结构使用一个数组和两个整型变量实现,其结构如下: 1 struct Queue{ 2 ElemType elem[MaxSize]; 3 int head, tail; 4 }; 即利用数组elem顺序存储队列中的所有元素,利用整型变量head和tail分别存储队首元素和队尾(或队尾下一

C语言实现循环队列(基本操作及图示)

-------------------------------------------- 如果使用顺序表作为队列的话,当处于右图状态则不能继续插入新的队尾元素,否则会因为数组越界而导致程序代码被破坏. 由此产生了由链表实现的循环队列,只有队列未满时才可以插入新的队尾元素. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

循环队列的基本操作

主要的功能:1)循环队列的初始化 2)求循环队列的长度 3)循环队列的插入删除操作 4) 判断循环队列是否为空,是否已经满了 5)遍历循环队列 主要的原理: 1)初始化 front = rear = 0; 2)为空  front = rear 3)已满 front=(rear+1)%size 4)  插入 rear = (rear+1)%size 5)删除 front=(front+1)%size 代码如下: /******************************************

顺序循环队列和链式存储队列(带头结点和不带头结点)

1.顺序存储的循环队列 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <stdbool.h> 4 5 typedef int ElementType; 6 typedef int Position; 7 typedef struct QNode* PtrToNode; 8 struct QNode { 9 ElementType *Data; 10 Position Front, Rear; 11 int M