当前位置:   article > 正文

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

下面程序为队列的基本操作,请将程序代码补充完整。typedef struct qnode [ qe

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

#include

#include

#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;

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/646433
推荐阅读
相关标签
  

闽ICP备14008679号