数据结构 实验4 循环队列的实现和运算
循环队列的基本操作

实验四循环队列的基本操作实验目的:1、熟悉将算法转换成程序代码的过程。
2、了解单循环队列的逻辑结构特性,熟练掌握循环队列顺序存储结构的C 语言描述方法。
3、熟练掌握循环队列的基本操作:入队、出队等,掌握循环队列的存取特性。
实验内容:1、分别建立包含6个数据元素的循环队列;2、从键盘输入一个数据元素x,进行入队操作;3、获取队头元素的值;4、对循环队列进行出队操作;5、打印循环队列元素和队列长度;6、给出程序及各项操作结果。
实验步骤:#include <stdio.h>#include <malloc.h>#include <stdlib.h>#define MAXSIZE 100 /*队列的最大容量*/typedef int DataType;typedef struct {DataType data[MAXSIZE]; /*队列的存储空间*/int front, rear; /*队头队尾指针*/}SeqQueue,*PSeqQueue;PSeqQueue Init_SeqQueue( ){ /*初始化一新队列,入口参数:无,返回值:新顺序队列指针,null表示失败*/ PSeqQueue Q;Q=( PSeqQueue )malloc(sizeof(SeqQueue));if (Q){Q->front=0;Q->rear=0;printf("置空队列成功!");}return Q;}void Destroy_SeqQueue(PSeqQueue *Q){ /*销毁一队列,入口参数:要销毁的顺序队列指针的地址,返回值:无*/ if (*Q)free(*Q);*Q=NULL;}int Empty_SeqQueue(PSeqQueue Q)/*判断队列是否为空,入口参数:顺序队列,返回值:1表示为空,0表示非空*/{ if (Q && Q->front==Q->rear)return (1);elsereturn (0);}int QueueLength (PSeqQueue Q){学生自己写}//返回Q的元素个数,即队列的长度int In_SeqQueue ( PSeqQueue Q , DataType x)/*入队操作,入口参数:顺序队列和待入队元素x ,返回值:1表示成功,-1表示队满溢出*/{ if ((Q->rear+1)%MAXSIZE==Q->front){ printf("队满");return -1; /*队满不能入队*/}else{ Q->rear=(Q->rear+1) % MAXSIZE;Q->data[Q->rear]=x;return 1; /*入队完成*/}}int Out_SeqQueue (PSeqQueue Q,DataType *x){ /*出队操作,入口参数:顺序队列,返回值:1表示成功,-1表示队空,出队的元素保存到*x */if (Empty_SeqQueue(Q)){printf("队空");return -1; /*队空不能出队*/}else{ Q->front=(Q->front+1) % MAXSIZE;*x=Q->data[Q->front];return 1; /*出队完成*/}}int Front_SeqQueue(PSeqQueue Q ,DataType *x){ /*取队头元素,入口参数:顺序队列和取出元素存放地址,返回值:1表示成功,-1表示队空*/if (Q->front==Q->rear){printf("队空");return -1; /*队空不能得到队头元素*/}else{ *x=Q->data[(Q->front+1)%MAXSIZE];return 1; /*取队头元素操作完成*/}}void display(PSeqQueue S){学生填写}void main(){(由学生填写)}实验用测试数据和相关结果分析:(由学生填写)实验总结:(由学生填写)。
数据结构上机4_循环队列

实验四队列(循环队列的创建、添加和删除操作)
1.实验目的:掌握循环队列的基本操作,并对其进行简单应用。
2.实验内容:
假设循环队列的最大长度为7,现在依次将以下数据入队列:{7,5,3,9,2,4};
接着进行3次出队列的操作,再将15、18这两个数据入队列,最后从对头到队尾依次输出队列中的元素。
3.实验步骤:
源代码:
运行结果;
4.总结
在C语言中不能用动态分配的一维数组来实现循环队列。
如果用户的应用程序中设有循环队列,则必须为他设定一个最大队列长度;若用户无法预估队列的最大长度,则宜采用链队列。
【数据结构】循环队列的实现

【数据结构】循环队列的实现//111111111111111111111111111第一部分1111111111111111111111111#include<stdio.h>#define MAXQSIZE 5#define ERROR 0#define OK 1;//原来我们定义一个int类型的ElemType,//这次我们定义一个int类型的QElemType,typedef int QElemType;typedef int Status;//定义循环队列的结构体typedef struct SqQueue{QElemType *base;int front;//头指针,说的是指针,其实就是一个整型的变量,//变量加1,相当于指针加1,不是真正的指针,深入理解int rear;//尾指针}SqQueue;//111111111111111111111111111第一部分结束1111111111111111111111111//222222222222222222222222222第二部分2222222222222222222222222222 //循环队列的操作//初始化Status InitSQueue(SqQueue *SQ){//申请MAXQSIZE个连续的空间,将空间的首地址赋给base指针SQ->base = (QElemType *)malloc(MAXQSIZE * sizeof(QElemType));//申请失败,退出if(!SQ->base)exit(ERROR);//刚开始,头指针和尾指针都等于0,//这里再次理解,front和rear其实不是真正的指针,指针式地址//这里仅仅是定义了整型变量,当指针的来使用。
//同胞们,要好好理解啊SQ->front = SQ->rear = 0;printf("初始化完毕,申请的空间是:%d个\n",MAXQSIZE);return OK;}// 入队(插入尾元素)Status EnSQueue(SqQueue *SQ,QElemType e){//先要判断,如果循环队列满了,就不能再插入了//同学们思考,这里如果满了,是否可以再申请空间//(答案是可以的,给你们空间去发挥)if((SQ->rear + 1)%MAXQSIZE == SQ->front){printf("我已经没空间了,可怜可怜我把,别进了!\n");printf("我剩余的一个空间,还要区别队空和队满的标志了!\n");return ERROR;}//看到了吧,这里的SQ->rear其实是个整型,作为数组的下标来用//类似于指针的作用SQ->base[SQ->rear] = e;//一定要注意,循环队列指针加1,一定要对MAXQSIZE求余,因为是循环的//深入理解SQ->rear = (SQ->rear + 1)%MAXQSIZE;return OK;}// 出队(删除头元素)Status DeSQueue(SqQueue *SQ,QElemType *e){//先要判断,如果循环队列是否空了,空队列没元素可出if(SQ->rear == SQ->front){printf("我已经没元素了,你还让我出啥呀!\n");return ERROR;}*e = SQ->base[SQ->front];//一定要注意,循环队列指针加1,一定要对MAXQSIZE求余,因为是循环的//深入理解SQ->front = (SQ->front + 1)%MAXQSIZE;return OK;}//遍历链队:Status visitSQueue(SqQueue *SQ){//这里遍历,就相当于输出数组int i;int length;printf("现在队列的元素值为:\n");//深入理解这个for循环for(i = SQ->front;i < SQ->rear;i++){printf("%d ",SQ->base[i]);}printf("\n");return OK;}//求队列长度:Status Queuelength(SqQueue *SQ){//这里给你们空间去发挥printf("等你来完成了,别偷懒啊,我可是全程监控了!\n");return OK;}//222222222222222222222222222第二部分结束2222222222222222222222222222//33333333333333333333333333333第三部分3333333333333333333333333333 void main(){SqQueue *SQ;QElemType e;int temp;int returnvalue;printf("1:初始化循环队列----------------------2:入队\n");printf("3:出队-------------------4:返回长度\n");printf("5:退出\n");while(1){printf("请输入要操作的编号:\n");scanf("%d",&temp);//如果是1表示头插法建立链表if(temp == 1){InitSQueue(SQ);}//如果是2入队if(temp == 2){printf("请输入要入队元素值:\n");scanf("%d",&e);//入队操作returnvalue = EnSQueue(SQ,e);if(returnvalue == 1){visitSQueue(SQ);}}//如果是3,出队if(temp == 3){//调用出栈的函数returnvalue = DeSQueue(SQ,&e);if(returnvalue == 1){printf("你出队的元素是:%d\n",e);visitSQueue(SQ);}}//返回长度if(temp == 4){Queuelength(SQ);}//操作结束if(temp == 5){exit(1);}}}。
数据结构实验4:C++实现循环队列

数据结构实验4:C++实现循环队列实验44.1 实验⽬的熟练掌握队列的顺序存储结构和链式存储结构。
熟练掌握队列的有关算法设计,并在循环顺序队列和链队列上实现。
根据具体给定的需求,合理设计并实现相关结构和算法。
4.2 实验要求4.2.1 循环顺序队列的实验要求循环顺序队列结构和运算定义,算法的实现以库⽂件⽅式实现,不得在测试主程序中直接实现;实验程序有较好可读性,各运算和变量的命名直观易懂,符合软件⼯程要求;程序有适当的注释。
4.3 实验任务4.3.1 循环顺序队列实验任务编写算法实现下列问题的求解。
<1>初始化⼀个队列。
<2>判断是否队空。
<3>判断是否队满。
设队列最⼤长度:MaxLen=100第⼀组数据:⼊队n个元素,判断队满第⼆组数据:⽤循环⽅式将1到99,99个元素⼊队,判队满<4>⼊队第⼀组数据:4,7,8,12,20,50第⼆组数据:a,b,c,d,f,g<5>出队<6>取队头元素<7>求当前队列中元素个数<8>编写算法实现①初始化空循环队列;②当键盘输⼊奇数时,此奇数⼊队;③当键盘输⼊偶数时,队头出队;④当键盘输⼊0时,算法退出;⑤每当键盘输⼊后,输出当前队列中的所有元素。
4.5 运⾏结果截图及说明图1 测试(1)、(2)、(3)、(5)、(6)、(7)图2 测试(4)图3 测试(4)图4 测试(8)4.6 附源代码1// stdafx.h : include file for standard system include files,2// or project specific include files that are used frequently, but3// are changed infrequently4//56#if !defined(AFX_STDAFX_H__8FA49CDF_FC99_4984_AB37_46921F7ED357__INCLUDED_)7#define AFX_STDAFX_H__8FA49CDF_FC99_4984_AB37_46921F7ED357__INCLUDED_89#if _MSC_VER > 100010#pragma once11#endif// _MSC_VER > 10001213 #include <stdc++.h>1415using namespace std;1617 typedef int elementType;18 typedef char elementType1;19const int maxn = 100;2021// TODO: reference additional headers your program requires here2223//{{AFX_INSERT_LOCATION}}24// Microsoft Visual C++ will insert additional declarations immediately before the previous line.2526#endif// !defined(AFX_STDAFX_H__8FA49CDF_FC99_4984_AB37_46921F7ED357__INCLUDED_)1// _SeqCircleQueue.h: interface for the _SeqCircleQueue class.2//3//////////////////////////////////////////////////////////////////////45#if !defined(AFX__SEQCIRCLEQUEUE_H__FCBC0603_27E1_4352_833C_6BED9B418B96__INCLUDED_) 6#define AFX__SEQCIRCLEQUEUE_H__FCBC0603_27E1_4352_833C_6BED9B418B96__INCLUDED_78#if _MSC_VER > 10009#pragma once10#endif// _MSC_VER > 10001112class _SeqCircleQueue13 {14public:15 _SeqCircleQueue();16virtual ~_SeqCircleQueue();17bool emptySeqCircleQueue();18bool fullSeqCircleQueue();19bool enQueue( elementType value );20bool deQueue( elementType &value );21bool getFront( elementType &value );22int length();23void oddOrEven( elementType value );24 friend ostream &operator<<( ostream &os, _SeqCircleQueue &scq )25 {26if( ( scq._front - 1 ) % maxn == scq._rear )27return os;28int column = 0;29for( int i = scq._front; i % maxn != scq._rear; i = ( i + 1 ) % maxn )30 {31 os << setw(3) << setiosflags(ios::left) << scq.data[i] << "";32 column ++;33if( column % 10 == 0 )34 os << endl;35 }36 os << endl;37 }39 elementType data[maxn];40int _front;41int _rear;4243 };4445#endif// !defined(AFX__SEQCIRCLEQUEUE_H__FCBC0603_27E1_4352_833C_6BED9B418B96__INCLUDED_) 1// _SeqCircleQueue.cpp: implementation of the _SeqCircleQueue class.2//3//////////////////////////////////////////////////////////////////////45 #include "stdafx.h"6 #include "_SeqCircleQueue.h"78//////////////////////////////////////////////////////////////////////9// Construction/Destruction10//////////////////////////////////////////////////////////////////////1112 _SeqCircleQueue::_SeqCircleQueue()13 {14 _front = _rear = 0;15 }1617 _SeqCircleQueue::~_SeqCircleQueue()18 {19 ios::sync_with_stdio(false);20 cout << "The _SeqCircleQueue destruction has been called!" << endl;21 }2223bool _SeqCircleQueue::emptySeqCircleQueue()24 {25return _front == _rear;26 }2728bool _SeqCircleQueue::fullSeqCircleQueue()29 {30return ( _rear + 1 ) % maxn == _front;31 }3233bool _SeqCircleQueue::enQueue( elementType value )34 {35if( fullSeqCircleQueue() )36 {37 cerr << "Seq-Circle-Queue is full!Error in _SeqCircleQueue::enQueue()!" << endl;38return false;39 }40 data[_rear] = value;41 _rear = ( _rear + 1 ) % maxn;42return true;43 }4445bool _SeqCircleQueue::deQueue( elementType &value )46 {47if( emptySeqCircleQueue() )48 {49 cerr << "Seq-Circle-Queue is empty!Error in _SeqCircleQueue::popFront()!" << endl;50return false;51 }52 value = data[_front];53 _front = ( _front + 1 ) % maxn;54return true;55 }5657bool _SeqCircleQueue::getFront( elementType &value )58 {59if( emptySeqCircleQueue() )60 {61 cerr << "Seq-Circle-Queue is empty!Error in _SeqCircleQueue::getFront()!" << endl;62return false;63 }64 value = data[_front];65return true;66 }6768int _SeqCircleQueue::length()69 {70if( emptySeqCircleQueue() )72 cerr << "Seq-Circle-Queue is empty!Error in _SeqCircleQueue::length()!" << endl;73return -1;74 }75return ( _rear - _front + maxn ) % maxn;76 }7778void _SeqCircleQueue::oddOrEven( elementType value )79 {80if( value & 1 )81 {82 enQueue(value);83 cout << value << " will be added to the queue!" << endl;84 cout << (*this);85 }86else if( !( value & 1) && value != 0 )87 {88 elementType x;89 deQueue(x);90 cout << x << " has been deleted from the queue!" << endl;91 cout << (*this);92 }93else//if( value == 0 )94 {95 cout << "The _SeqCircleQueue::oddOrEven() has been stoped!" << endl;96return;97 }98 }1// charSeqCircleQueue.h: interface for the charSeqCircleQueue class.2//3//////////////////////////////////////////////////////////////////////45#if !defined(AFX_CHARSEQCIRCLEQUEUE_H__FBB4F8DD_2EF9_43A6_8E23_FD7E4C56908E__INCLUDED_)6#define AFX_CHARSEQCIRCLEQUEUE_H__FBB4F8DD_2EF9_43A6_8E23_FD7E4C56908E__INCLUDED_78#if _MSC_VER > 10009#pragma once10#endif// _MSC_VER > 10001112class charSeqCircleQueue13 {14public:15 charSeqCircleQueue();16virtual ~charSeqCircleQueue();17bool emptyCharSeqCircleQueue();18bool fullCharSeqCircleQueue();19bool enQueue( elementType1 value );20bool deQueue( elementType1 &value );21bool getFront( elementType1 &value );22int length();23 friend ostream &operator<<( ostream &os, charSeqCircleQueue &cscq )24 {25if( ( cscq._front - 1 ) % maxn == cscq._rear )26return os;27int column = 0;28for( int i = cscq._front; i % maxn != cscq._rear; i = ( i + 1 ) % maxn )29 {30 os << setw(3) << setiosflags(ios::left) << cscq.data[i] << "";31 column ++;32if( column % 10 == 0 )33 os << endl;34 }35 os << endl;36 }37private:38 elementType1 data[maxn];39int _front;40int _rear;4142 };4344#endif// !defined(AFX_CHARSEQCIRCLEQUEUE_H__FBB4F8DD_2EF9_43A6_8E23_FD7E4C56908E__INCLUDED_) 1// charSeqCircleQueue.cpp: implementation of the charSeqCircleQueue class.3//////////////////////////////////////////////////////////////////////45 #include "stdafx.h"6 #include "charSeqCircleQueue.h"78//////////////////////////////////////////////////////////////////////9// Construction/Destruction10//////////////////////////////////////////////////////////////////////1112 charSeqCircleQueue::charSeqCircleQueue()13 {14 _front = _rear = 0;15 }1617 charSeqCircleQueue::~charSeqCircleQueue()18 {19 ios::sync_with_stdio(false);20 cout << "The charSeqCircleQueue destruction has been called!" << endl;21 }2223bool charSeqCircleQueue::emptyCharSeqCircleQueue()24 {25return _front == _rear;26 }2728bool charSeqCircleQueue::fullCharSeqCircleQueue()29 {30return ( _rear + 1 ) % maxn == _front;31 }3233bool charSeqCircleQueue::enQueue( elementType1 value )34 {35if( fullCharSeqCircleQueue() )36 {37 cerr << "Seq-Circle-Queue is full!Error in charSeqCircleQueue::::enQueue()!" << endl; 38return false;39 }40 data[_rear] = value;41 _rear = ( _rear + 1 ) % maxn;42return true;43 }4445bool charSeqCircleQueue::deQueue( elementType1 &value )46 {47if( emptyCharSeqCircleQueue() )48 {49 cerr << "Seq-Circle-Queue is empty!Error in charSeqCircleQueue::popFront()!" << endl; 50return false;51 }52 value = data[_front];53 _front = ( _front + 1 ) % maxn;54return true;55 }5657bool charSeqCircleQueue::getFront( elementType1 &value )58 {59if( emptyCharSeqCircleQueue() )60 {61 cerr << "Seq-Circle-Queue is empty!Error in charSeqCircleQueue::::getFront()!" << endl; 62return false;63 }64 value = data[_front];65return true;66 }6768int charSeqCircleQueue::length()69 {70if( emptyCharSeqCircleQueue() )71 {72 cerr << "Seq-Circle-Queue is empty!Error in charSeqCircleQueue::::length()!" << endl; 73return -1;74 }75return ( _rear - _front + maxn ) % maxn;76 }4.7 调试过程中出现的bug总结注意细节!。
Python实现数据结构-循环队列的操作方法

Python实现数据结构-循环队列的操作⽅法今天我们来到了循环队列这⼀节,之前的⽂章中,我介绍过了,这是最简单的实现⽅法。
但是,我们都知道,在列表中删除第⼀个元素和删除最后⼀个元素花费的时间代价是不⼀样的,删除列表的第⼀个元素,那么在它之后的所有元素都要进⾏移动。
所以当列表特别长的时候,这个代价就⽐较明显了。
我们本⽂介绍的循环队列可以避免这个问题,同样我们上篇⽂章提到的⽤链表实现的⽅法也可以避免。
下⾯,我们来介绍循环队列。
循坏队列循环队列,就是将普通的队列⾸尾连接起来,形成⼀个环状,并分别设置⾸尾指针,⽤来指明队列的头和尾。
每当我们插⼊⼀个元素,尾指针就向后移动⼀位,当然,在这⾥我们队列的最⼤长度是提前定义好的,当我们弹出⼀个元素,头指针就向后移动⼀位。
这样,列表中就不存在删除操作,只有修改操作,从⽽避免了删除前⾯节点造成的代价⼤的问题。
好,话不多说,我们⽤代码来实现⼀下class Loopqueue:def __init__(self, length):self.head = 0self.tail = 0self.maxSize = lengtht = 0self.__list = [None]*length这⾥同样,我们定义⼀个队列类,在实例化循环队列的时候,要求指定队列的⼤⼩,除了⾸尾指针以及队列最⼤长度之外,我们还声明⼀个表⽰队列当前长度的属性cnt。
接下来我们给队列增加⼀些操作:判空def isEmpty(self):return t == 0判满def isFull(self):return t == self.maxSize添加元素def push(self, data):if self.isFull():return Falseif self.isEmpty():self.__list[0] = dataself.head = 0self.tail = 0t = 1return Trueself.tail = (self.tail+1)%self.maxSizet += 1self.__list[self.tail] = datareturn True弹出元素def pop(self):if self.isEmpty():return Falsedata = self.__list[self.head]self.head = (self.head+1)%self.maxSizet -= 1return data清空队列def clear(self):self.head = 0self.tail = 0t = 0return True定义len和print函数def __len__(self):return tdef __str__(self):s = ''for i in range(t):index = (i + self.head) % self.maxSizes += str(self.__list[index])+' 'return sOK,我们的循环队列类就定义好了,如果你看过介绍队列的⽂章,就会发现循环队列和普通队列的操作在逻辑上还是有⼀些相似的。
循环队列的基本操作

实验四循环队列的基本操作实验目的:1、熟悉将算法转换成程序代码的过程。
2、了解单循环队列的逻辑结构特性,熟练掌握循环队列顺序存储结构的C 语言描述方法。
3、熟练掌握循环队列的基本操作:入队、出队等,掌握循环队列的存取特性。
实验内容:1、分别建立包含6个数据元素的循环队列;2、从键盘输入一个数据元素x,进行入队操作;3、获取队头元素的值;4、对循环队列进行出队操作;5、打印循环队列元素和队列长度;6、给出程序及各项操作结果。
实验步骤:#include <stdio.h>#include <malloc.h>#include <stdlib.h>#define MAXSIZE 100 /*队列的最大容量*/typedef int DataType;typedef struct {DataType data[MAXSIZE]; /*队列的存储空间*/int front, rear; /*队头队尾指针*/}SeqQueue,*PSeqQueue;PSeqQueue Init_SeqQueue( ){ /*初始化一新队列,入口参数:无,返回值:新顺序队列指针,null表示失败*/ PSeqQueue Q;Q=( PSeqQueue )malloc(sizeof(SeqQueue));if (Q){Q->front=0;Q->rear=0;printf("置空队列成功!");}return Q;}void Destroy_SeqQueue(PSeqQueue *Q){ /*销毁一队列,入口参数:要销毁的顺序队列指针的地址,返回值:无*/ if (*Q)free(*Q);*Q=NULL;}int Empty_SeqQueue(PSeqQueue Q)/*判断队列是否为空,入口参数:顺序队列,返回值:1表示为空,0表示非空*/{ if (Q && Q->front==Q->rear)return (1);elsereturn (0);}int QueueLength (PSeqQueue Q){学生自己写}//返回Q的元素个数,即队列的长度int In_SeqQueue ( PSeqQueue Q , DataType x)/*入队操作,入口参数:顺序队列和待入队元素x ,返回值:1表示成功,-1表示队满溢出*/{ if ((Q->rear+1)%MAXSIZE==Q->front){ printf("队满");return -1; /*队满不能入队*/}else{ Q->rear=(Q->rear+1) % MAXSIZE;Q->data[Q->rear]=x;return 1; /*入队完成*/}}int Out_SeqQueue (PSeqQueue Q,DataType *x){ /*出队操作,入口参数:顺序队列,返回值:1表示成功,-1表示队空,出队的元素保存到*x */if (Empty_SeqQueue(Q)){printf("队空");return -1; /*队空不能出队*/}else{ Q->front=(Q->front+1) % MAXSIZE;*x=Q->data[Q->front];return 1; /*出队完成*/}}int Front_SeqQueue(PSeqQueue Q ,DataType *x){ /*取队头元素,入口参数:顺序队列和取出元素存放地址,返回值:1表示成功,-1表示队空*/if (Q->front==Q->rear){printf("队空");return -1; /*队空不能得到队头元素*/}else{ *x=Q->data[(Q->front+1)%MAXSIZE];return 1; /*取队头元素操作完成*/}}void display(PSeqQueue S){学生填写}void main(){(由学生填写)}实验用测试数据和相关结果分析:(由学生填写)实验总结:(由学生填写)。
数据结构实验4循环队列的实现和运算

数据结构实验4循环队列的实现和运算循环队列是一种特殊的队列,它的特点是可以循环利用已经出队的元
素所占用的空间。
循环队列采用一维数组来实现,通常需要两个指针(称
为队头指针front和队尾指针rear)。
循环队列的基本操作包括初始化、判空、判满、入队、出队、获取队
头元素等。
1. 初始化:循环队列的初始化很简单,只需将队头指针front和队
尾指针rear都置为0即可。
2. 判空:当队头指针front和队尾指针rear相等时,队列为空。
3. 判满:当队尾指针rear加1后等于队头指针front时,队列为满。
4. 入队操作:在插入元素之前,需要判断队列是否已满。
如果队列
为满,则无法插入新的元素;否则,在rear指针的位置插入新的元素,
并将rear指针往后移动一位。
5. 出队操作:在删除元素之前,需要判断队列是否为空。
如果队列
为空,则无法删除元素;否则,在front指针的位置删除元素,并将
front指针往后移动一位。
6. 获取队头元素:获取队列的队头元素很简单,只需返回front指
针位置的元素即可。
循环队列的实现比较简洁高效,主要是通过取模运算来实现队列指针
的循环移动。
当rear指针到达数组的最后一个位置时,再插入新的元素时,需要将rear指针指向数组的第一个位置;当front指针在数组的最
后一个位置上时,再删除元素时,需要将front指针指向数组的第一个位置。
通过循环队列的实现和运算,可以高效地进行队列的操作,从而提高算法的执行效率。
在实际应用中,循环队列常被用于实现缓冲区、进程调度等场景。
数据结构:循环队列及其基本操作的实现

数据结构:循环队列及其基本操作的实现/*** 循环队列* 队列设置first指针直接指向队列头部元素,tail尾指针指向队列最后⼀个元素的后⼀个,即队列中总是预留⼀个空位*/class CircleQueue implements Queue<Integer>{private Integer[] queueArray = null;private int first;private int tail;private int maxSize;public CircleQueue(int max){this.maxSize=max+1;this.queueArray = new Integer[maxSize];}@Overridepublic int size() {return (tail-first+maxSize)%maxSize;}@Overridepublic boolean isEmpty() {return tail==first;}@Overridepublic boolean contains(Object o) {Integer integer = (Integer)o;for (int i = first; i!=tail; i++) {if (i==queueArray.length){i=0;}if (integer.equals(queueArray[i])){return true;}}return false;}@Overridepublic Iterator<Integer> iterator() {return new Iterator<Integer>() {int temFirst = first;@Overridepublic boolean hasNext() {return tail!=temFirst;}@Overridepublic Integer next() {int num = queueArray[temFirst];temFirst = (temFirst+1)%maxSize;return num;}};}@Overridepublic boolean add(Integer integer) {if ((tail+1)%maxSize==first){System.out.println("队列已满,⽆法添加");return false;}queueArray[tail]=integer;tail = (tail+1)%maxSize;return true;}@Overridepublic Integer poll() {if(isEmpty()){throw new RuntimeException("队列为空");}int result = queueArray[first];first = (first+1)%maxSize;return first;}@Overridepublic Integer peek() {return queueArray[first];}}public static void main(String[] args) {CircleQueue circleQueue = new CircleQueue(5);Scanner scanner = new Scanner(System.in);while (true){System.out.println("1:添加元素");System.out.println("2:取出元素");System.out.println("3:查看头元素");System.out.println("4:遍历队列");System.out.println("5:查看元素数量");System.out.println("6:是否为空");int commend = scanner.nextInt();switch (commend){case 1:{System.out.println("请输出⼀个元素");int num = scanner.nextInt();circleQueue.add(num);break;}case 2:{System.out.println(circleQueue.poll());break;}case 3:{System.out.println(circleQueue.peek());break; }case 4:{for (Integer integer : circleQueue) {System.out.printf("%d\t",integer);}System.out.println();break;}case 5:{System.out.println(circleQueue.size());break; }case 6:{System.out.println(circleQueue.isEmpty());break;}}}}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
5、测试数据与实验结果(可以抓图粘贴)(1)菜单显示:
(2)入队:
(3)队满(已入队9个元素):
(4)出队:
(5)队空(已出队9个元素):
(6)显示队中的元素:
(7)求队长:
6、结果分析与实验体会
本次实验是参考了范例程序,经过自己的改写,从而实现要求。
先做简单的输出,一步步的再做其它格式的设置。
在实验的过程中,我加深了对队列各种操作的理解,因为队列是“先进先出”的操作受限制的线性表,一般队列只允许在队尾进行插入操作,在队头进行删除操作,元素之间存在一对一的关系,本程序的设计过程也是对前面线性表知识的巩固。
再者,用链式存储结构实现这个程序,实际上是设计一个带有头指针(front)和尾指针(rear)的单向链表,此单向链表没有头结点,头尾指针指的是同一个单向链表中的首尾结点,所以,从链队列的整体结构考虑,一般讲这两个指针封装在一个结构体中。
队列是一种应用广泛的数据结构,凡具有其特点的需要排队处理的问题,都可以使用它来处理。