数据结构c语言之顺序循环队列

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
当入队与出队时,如果内存未满,但是在队头删除,在队尾入队,对于但循序循环队列会发生“假溢出”现象,为了更好的解决问题吗,引入循序循环队列,,定义两个变量,一个指向队头,一个指向队尾,记录队头队尾的下标


status.h


#ifndef STATUS_H
#define STATUS_H
#define YES 1
#define NO 0
typedef int Status;
typedef int ElemType;
#endif

list.h


#ifndef LIST_H
#define LIST_H
#include <stdio.h>
#include "status.h"
#define MAX_SIZE 5
typedef struct Queue{
  ElemType ****base;
  int front;//指向队头元素的下标 
  int real;//指向队尾元素的下标 
}Queue;
/**
(1)初始化队列  InitQueue(Q)    
(2)入队  EnQueue(Q,item)     
(3)出队  DeQueue(Q,item)       
(4)获取队头元素内容  GetHead(Q,item)  
(5)判断队列是否为空 QueueEmpty(Q)*/     
Status InitQueue(Queue *p);
Status QueueEmpty(Queue *p);
Status QueueFull(Queue *p);
Status EnQueue(Queue *p,ElemType value);
void PutQueue(Queue *p);
Status DeQueue (Queue *p,ElemType *value);
Status GetHead(Queue *p,ElemType *value);
#endif 

list.c


#include <stdio.h>
#include "list.h"
//初始化队列
Status InitQueue(Queue *p)
   {
    p->base=(ElemType *)malloc(sizeof(ElemType)*MAX_SIZE);
    if(!p->base)
    return NO;
    p->front=p->real=0;
    return YES;
   }
//判断队列是否为空
Status QueueEmpty(Queue *p)
   {
    if((p->real-p->front)==0)
    return YES;//空
    else
    return NO;//非空 
   } 
//判断队列是否满了
Status QueueFull(Queue *p) 
   {
    if((p->real+1)%MAX_SIZE==p->front)
    return YES;//满
    else
    return NO;//未满 
   }
//入队
Status EnQueue(Queue *p,ElemType value)    
   {
    p->base[p->real]=value;
    if(QueueFull(p))
    return NO;
    if(p->real+1==MAX_SIZE)
    p->real=0;
    else
    p->real++; 
    return YES;
   }
//输出 
void PutQueue(Queue *p)
   {
    int i;
    printf("此时队列中的元素为;");
    i = p->front;
    for(;i!=p->real;i++)
    {
     printf("%d  ",p->base[i]);
     if(i==MAX_SIZE-1)
     i=-1;
    }
    printf("%d  ",p->base[i]); 
    printf("\n"); 
   }
//出队
Status DeQueue (Queue *p,ElemType *value)
   {
    if(QueueEmpty(p))
    return NO;
    *value=p->base[p->front];
    p->front=p->front+1;
    return YES;
   } 
//获取队头元素
Status GetHead(Queue *p,ElemType *value) 
   {
    if(QueueEmpty(p))
    return NO;
       *value=p->base[p->front];
       return YES; 
   }  

main.c


#include <stdio.h>
#include "list.h"
int main()
{
  Queue qu;
  Queue *p=&qu;
  ElemType value;
  int i,t,n; 

  printf("================初始化队列:=================\n");
  t=InitQueue(p);
  if(t==NO)
  printf("初始化失败");
  else
  printf("初始化成功") ;


  printf("\n===============判断队列是否为空=============\n");
  t=QueueEmpty(p);
  if(t==YES)
  printf("队列为空");
  else
  printf("队列不为空"); 


  printf("\n===============判断队列是否满=============\n");
  t=QueueFull(p);
  if(t==YES)
  printf("队列满");
  else
  printf("队列未满"); 


  printf("\n================入队====================\n"); 
  printf("请依次输入%d个元素:",MAX_SIZE);
  for(i=0;i<MAX_SIZE;i++)
  {  
   scanf("%d",&value);
   EnQueue(p,value) ;
  }
  PutQueue(p); 



  printf("================出队====================\n"); 
  DeQueue (p,&value);
  printf("出队的元素为:%d\n",value);
  PutQueue(p);



  printf("================获取队头元素====================\n"); 
  GetHead(p,&value);
  printf("队头元素为:%d\n",value);
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱睡觉的小馨

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

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

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

打赏作者

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

抵扣说明:

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

余额充值