解题思路:
注意事项:
参考代码:
//linkquene
#include <iostream>
#include <cstdlib>
using namespace std;
typedef struct qnode
{
int data;
qnode* next=NULL;
}qnode;
typedef struct linkquene
{
qnode* front;
qnode* rear;
}linkquene;
void enquene(linkquene* pointer,int ennum,int pp);
bool dequene(linkquene* pointer);
int quenelength(linkquene* pointer);
int pp;
int main(void)
{
int N;
cin>>N;
linkquene a;
a.front=NULL;
a.rear=NULL;
pp=1;
while(N--)
{
int flag;
cin>>flag;
if(flag==1)
{
int ennum;
cin>>ennum;
enquene(&a,ennum,pp);
pp=0;
}
if(flag==2)
{
if(!dequene(&a))
{
cout<<"no"<<endl;
break;
}
}
if(flag==3)
cout<<quenelength(&a)<<endl;
}
return 0;
}
void enquene(linkquene* pointer,int ennum,int pp)
{
qnode* tmp=(qnode*)malloc(sizeof(qnode));
tmp->data=ennum;
tmp->next=NULL;
if(pp==1)
{
pointer->rear=tmp;
pointer->front=tmp;
}
else
{
pointer->rear->next=tmp;
pointer->rear=tmp;
}
}
bool dequene(linkquene* pointer)
{
if(pointer->front==NULL)
{
return false;
}
cout<<pointer->front->data<<endl;
pointer->front=pointer->front->next;
if(pointer->front==NULL)
{
pp=1;
pointer->rear=NULL;
}
return true;
}
int quenelength(linkquene* pointer)
{
if(pp==1)
return 0;
else
{
int counter = 1;
for (qnode* tmp=pointer->front;tmp!=pointer->rear; tmp=tmp->next)
{
counter++;
}
return counter;
}
}
// 循环队列
#include<iostream>
#define maxsize 100
using namespace std;
typedef struct quene
{
int data[100];
int front;
int rear;
}quene;
bool enquene(quene* pointer,int ennum);
bool dequene(quene* pointer);
void quenelength(quene* pointer);
int main()
{
int N;
cin>>N;
quene a;
a.front=0;
a.rear=0;
getchar();
while(N--)
{
int flag;
cin>>flag;
if(flag==1)
{
int ennum;
scanf("%d",&ennum);
enquene(&a,ennum);
}
if(flag==2)
{
if(!dequene(&a))
{
cout<<"no"<<endl;
break;
}
}
if(flag==3)
quenelength(&a);
}
return 0;
}
bool enquene(quene* pointer,int ennum)
{
if(((pointer->rear+1)%maxsize)==pointer->front)
return false;
pointer->data[pointer->rear]=ennum;
pointer->rear=(pointer->rear+1)%maxsize;
}
bool dequene(quene* pointer)
{
if(pointer->front==pointer->rear)
return false;
cout<<pointer->data[pointer->front]<<endl;
pointer->front=(pointer->front+1)%maxsize;
return true;
}
void quenelength(quene* pointer)
{
cout<<(pointer->rear-pointer->front+maxsize)%maxsize<<endl;
}
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复