小荆


私信TA

用户名:JHN01

访问量:5624

签 名:

等  级
排  名 2195
经  验 2295
参赛次数 0
文章发表 23
年  龄 0
在职情况 学生
学  校 邯郸学院
专  业

  自我简介:

解题思路:使用链队列

注意事项:

参考代码:

#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 人评分

  评论区