队列——课上练

​​​​​​​

 

#include <stdio.h>
#include<stdlib.h>

/*此处是顺序队列数据结构定义*/
typedef int DataType;
struct seqQueue//请完成数据结构定义
{
   int MAXNUM;
   int front,rear;
   DataType *element;
};

typedef struct seqQueue *PseqQueue;
//第一关
PseqQueue createNullQueue_seq(int m)
{//此处填写代码,创建一个空的顺序队列,能存放的最大元素个数为 m
 //若m=0,则返回NULL
  if(m==0)return 0;
  PseqQueue q = (PseqQueue)malloc(sizeof(struct seqQueue));
  if(q==NULL)return NULL;
  q->MAXNUM = m;
  q->front = 0;
  q->rear = 0;
  q->element = (int*)malloc(sizeof(int)*m);
  return q;
}

//第二关
int isNullQueue_seq(PseqQueue Q)
{
  //判断顺序(环形)队列是否为空,若为空,返回值为1,否则返回值为0,若队列不存在,则返回-1
  if(Q==NULL)return -1;
  else{
    if(Q->rear==Q->front)return 1;
    else return 0;
  }
}


//第三关
int isFullQueue_seq(PseqQueue Q)
{
  //判断环形队列是否已满,若已满,返回值为1,否则返回值为0
  if((Q->rear+1)%Q->MAXNUM==Q->front)return 1;
  else return 0;
}


//第四关
int enQueue_seq(PseqQueue Q ,DataType x)
{//在环形队列中插入数据元素x,若插入不成功,返回0;插入成功返回值为1
    
    if(isFullQueue_seq(Q))return 0;
    else{
      Q->element[Q->rear] = x;
      Q->rear++;
      return 1;
    }
}
 


//第五关
DataType delQueue_seq(PseqQueue Q)
{//出队并返回删除元素,若队列为空,则返回-1
      if(isNullQueue_seq(Q))return -1;
      else{
        int temp = Q->element[Q->front];
        Q->front++;
        return temp;
      }
}

//第六关
DataType front_seq(PseqQueue Q)
{// 取队首元素返回,若队列为空,则返回-1
    if(isNullQueue_seq(Q))return -1;
    else{
      return Q->element[Q->front];
    }
}

//销毁顺序队列,释放队列所占存储空间
int destroyQueue_seq(PseqQueue Q)
{
    //返回值为销毁的栈中现有数据元素的个数,若待销毁的线性表不存在,则返回0
    if(Q->element==NULL)return 0;
    else return Q->rear-Q->front;
}


//第七关
void queueApp(int c[],int n)
{
//参数用于传递顾客编号和顾客人数,输出顾客接受服务后离开顺序
  PseqQueue a = createNullQueue_seq(n+1);
  PseqQueue b = createNullQueue_seq(n+1);

  for(int i=0;i<n;i++){
    if(c[i]%2==0){
      enQueue_seq(b,c[i]);
    }else{
      enQueue_seq(a,c[i]);
    }
  }
  int i = 0;
  while(!isNullQueue_seq(a)&&!isNullQueue_seq(b)){
   
    while(++i){
      printf("%d ",delQueue_seq(a));
        if(i%2==0)break;;
    }
    printf("%d ",delQueue_seq(b));
  }
  while(!isNullQueue_seq(a))printf("%d ",delQueue_seq(a));
  while(!isNullQueue_seq(b))printf("%d ",delQueue_seq(b));
  
}

int main(void)
{
     int m ,a[8];
     scanf("%d",&m);
     for(int i=0;i<m;i++)
      scanf("%d",&a[i]);
     queueApp(a,m);
    
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蒋的学习笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值