当前位置:   article > 正文

队列的入队出队操作_队列入队出队

队列入队出队
#include <stdio.h>
#define MAXSIZE 30
#define OK 1
#define ERROR 0
typedef int Status;
typedef int ElemType;


typedef struct SqQueue
{
ElemType data[MAXSIZE];
int front ,rear;
}Queue;   //把SqQueue取别名为Queue


void IintQueue(Queue *q)    //初始化队列
{
q->front = q->rear = 0;
printf("队列初始化成功!\n");
}


int GetLength(const Queue *q)
{
return(q->rear - q->front + MAXSIZE)%MAXSIZE;
}


Status EnQueue(Queue *q,ElemType e)   //入队
{
if (GetLength(q) == MAXSIZE-1)
{
printf("队列已满!\n");
return ERROR;
}
q->data[q->rear] = e;
q->rear = (q->rear+1)%MAXSIZE;
return OK;
}


Status DeQueue(Queue *q,ElemType *e)   //出队
{
if (q->front == q->rear)
{
printf("队列为空!\n");
return ERROR;
}
*e = q->data[q->front];
q->front = (q->front+1)%MAXSIZE;
return OK;
}


void print( const Queue *q)
{
if (q->front == q->rear)
{
printf("队列为空!\n");
//return ERROR;
}
int i;
for (i = q->front; i <q->front + GetLength(q) ; ++i)
{
i%=MAXSIZE;
printf("%d ", q->data[i]);


}
printf("\n");
}


int main()
{
Queue q;
ElemType e;
int i;
IintQueue(&q);
for ( i =1 ; i <=9 ; ++i)
{
EnQueue(&q,i);
}
print(&q);
DeQueue(&q,&e);
print(&q);
return 0;
}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/975076
推荐阅读
相关标签
  

闽ICP备14008679号