队列的入队出队操作

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

你可能感兴趣的:(c语言)