解题思路:使用链队列
注意事项:
参考代码:
#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语言训练-阶乘和数* (C++代码)(直接输出样例hhhh)浏览:1152 |
C语言程序设计教程(第三版)课后习题8.7 (C语言代码)浏览:642 |
点我有惊喜!你懂得!浏览:2065 |
C二级辅导-同因查找 (C语言代码)浏览:553 |
数列排序 (C语言代码)浏览:827 |
C语言程序设计教程(第三版)课后习题6.8 (C语言代码)浏览:758 |
C语言程序设计教程(第三版)课后习题7.5 (C语言代码)浏览:517 |
C语言程序设计教程(第三版)课后习题6.5 (C语言代码)浏览:586 |
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:370 |
Tom数 (C语言代码)浏览:721 |