顺序队列(循环队列)基本操作实现 C语言

只写了几个主要的操作,销毁,清空等没有写

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

#define OK 1
#define ERROR 0

#define QUEUE_SIZE 100

typedef  int Status;
typedef  int ElemType;

typedef struct Qnode
{
        ElemType *base;
        int front;
        int rear;
        int count;
} Qnode;
Status Init(Qnode &qq)
{
       qq.base = (ElemType*)malloc(QUEUE_SIZE * sizeof(ElemType));
       if (qq.base == NULL) return ERROR;
       qq.front = 0;
       qq.rear = 0;
       qq.count = 0;
       return OK;
}

Status EnQueue(Qnode &qq, ElemType e)
{
       
       if ((qq.rear + 1) % QUEUE_SIZE == qq.front)
       return ERROR;
       qq.base[qq.rear] = e;
       qq.rear = (qq.rear + 1)%QUEUE_SIZE;
       qq.count++;
       return OK;
}

Status DeQueue(Qnode &qq, ElemType &e)
{
       if (qq.rear == qq.front)
       return ERROR;
       e = qq.base[qq.front];
       qq.front = (qq.front + 1) % QUEUE_SIZE;
       qq.count--;
       return OK;
}

Status GetTop(Qnode &qq, ElemType &e)
{
       if (qq.rear == qq.front)
       return ERROR;
       e = qq.base[qq.front];
       return OK;
}
Staus Destroy(Qnode qq)
{
      free(qq.base);
      qq.base = NULL;
      qq.count = 0;
      return OK;
}
int main()
{
    Qnode qq;
    Init(qq);
    int m, i;
    printf("how many nums?\n");
    scanf("%d", &m);
    for (i = 0; i < m; i++)
    {
        int e;
        scanf("%d", &e);
        EnQueue(qq, e);
    }
    int e;
    GetTop(qq, e);
    printf("%d\n" , e);
    for (i = 0; i < m; i++)
    {
        DeQueue(qq, e);
        printf("%d ", e);
    }
    printf("\n");
    system ("pause");
    return 0;
}


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值