解题思路:使用链队列
注意事项:
参考代码:
#include<stdio.h>
#include<stdlib.h>
#define stype int
typedef struct node{
struct node *next;
stype num;}data;
typedef struct{
data *front;
data *rear;}Linkqueue;
int initqueue(Linkqueue *L)
{
L->front=L->rear=(data *)malloc(sizeof(data));
if(!L->front)
{
exit(0);
return 0;
}
else
{
L->front->next=NULL;
return 1;
}
}
int insertqueue(Linkqueue *L,stype x)
{
data *s;
s=(data *)malloc(sizeof(data));
if(!s)
{
exit(0);
return 0;
}
else
{
s->num=x;
s->next=NULL;
L->rear->next=s;
L->rear=s;
return 1;
}
}
int deletequeue(Linkqueue *L,stype *x)
{
data *s;
if(L->front==L->rear)
return 0;
else
{
s=L->front->next;
*x=s->num;
L->front->next=s->next;
free(s);
if(L->front->next==NULL)
L->rear=L->front;
return 1;
}
}
int queuelenth(Linkqueue *L)
{
data *pa;
int i=0;
for(pa=L->front->next;pa;pa=pa->next)
i++;
return i;
}
int main()
{
Linkqueue L;
int n,k,m,num,x,y,j,i;
int a[100][5];
k=0;
j=0;
initqueue(&L); //对队列进行初始化
scanf("%d",&n);
while(k<n)
{
scanf("%d",&m);
if(m==1)
{
scanf("%d",&num);
insertqueue(&L,num);
}
else if(m==2)
{
y=deletequeue(&L,&x);
if(y==0)
a[j++][0]=-2;
else
{
a[j][0]=-1;
a[j][1]=x;
j++;
}
}
else if(m==3)
{
y=queuelenth(&L);
a[j++][0]=y;
}
k++;
}
for(i=0;i<j;i++)
{
if(a[i][0]==-2)
{
printf("no\n");
break;
}
if(a[i][0]==-1)
{
printf("%d\n",a[i][1]);
}
else
printf("%d\n",a[i][0]);
}
return 0;
}
0.0分
0 人评分
【绝对值排序】 (C语言代码)浏览:714 |
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:277 |
妹子杀手的故事 (C语言代码)浏览:1219 |
简单的a+b (C语言代码)浏览:598 |
简单的for循环浏览:1409 |
C语言程序设计教程(第三版)课后习题6.10 (C语言代码)浏览:512 |
C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:707 |
【出圈】 (C++代码)简单循环浏览:631 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)浏览:389 |
P1002 (C语言代码)浏览:954 |