循环队列

//循环队列之队列的顺序表示和实现
#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define MASIZE 100

typedef int Elemt;
typedef struct{
 int * base;
 int front;
 int rear;
}CQueue;

//创建一个空的循环列表
Elemt creat_Queue(CQueue &Q){
 Q.base=(int*)malloc(MASIZE*sizeof(int));
 Q.front=Q.rear=0;
 printf("creat successful!!!\n");
 return OK;

}
//释放队列
Elemt free_Queue(CQueue &Q){
 if(Q.base != NULL ){
  free(Q.base);
  printf("free successful!!!!\n");
  return OK;
 }
 return ERROR;
}


// 判断该循环队列是否满了,若满,则返回1,否则返回0
Elemt determin_FULL(CQueue Q){
 if((Q.rear+1)%MASIZE == Q.front )  return OK;
 return ERROR;
}

//向循环队列中插入元素

Elemt in_Queue(CQueue &Q,int e){
 if(determin_FULL(Q)) {
  printf(" the Queue is full!!!!\n");
  return ERROR;
 }
 
 Q.base[Q.rear]=e;
 Q.rear=(Q.rear + 1 ) % MASIZE;
 printf(" in successfull!!!\n");
 return OK;//成功返回1

}

//判断该循环链表是否为空

Elemt determin_null(CQueue Q){
 if(Q.front == Q.rear) return OK;
 else return ERROR;
}

//遍历(也就是将之前插入的元素一一删除,并输出该元素)

Elemt bianli_Queue(CQueue &Q){
 if(determin_null(Q)) {
  printf("the QUEUE is null:\n");
  return ERROR;
  }
 ;

 do{
  printf("%d...........\n",Q.base[Q.front]);
 Q.front=Q.front+1;
 }while(Q.front% MASIZE != Q.rear) ;

  return OK;
}


//主函数
int main(){
CQueue Q;
int e,n=5;
creat_Queue(Q);

while(n--){
 printf("input your e:");
 scanf("%d",&e);
 in_Queue(Q,e);
}

bianli_Queue(Q);

free_Queue(Q);
return OK;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值