解题思路:使用链队列
注意事项:
参考代码:
#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语言程序设计教程(第三版)课后习题7.1 (C语言代码)浏览:724 |
数组输出 (C语言代码)--此题的题目描述有问题浏览:1814 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:377 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:526 |
用筛法求之N内的素数。 (C语言代码)浏览:646 |
1128题解(返回值为数组的情况)浏览:450 |
单词个数统计 (C语言代码)浏览:1009 |
数列有序 (C语言代码)浏览:930 |
C二级辅导-公约公倍 (C语言代码)浏览:1310 |
求圆的面积 (C语言代码)浏览:657 |