c语言实现队列

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

#define MAXQSIZE 100
typedef int QElemType;
typedef struct{
	QElemType *base;
	int front;
	int rear;
}Queue;

void Init(Queue &Q){
	Q.base = (QElemType *)malloc(MAXQSIZE * sizeof(QElemType));
	Q.front = Q.rear = 0;
	printf("init success\n");
}

void Get(Queue Q){
	for (int i=0;i<11;i++){
		printf("data is %d\n",Q.base[i]);
	}
}

int enQueue(Queue &Q,QElemType e){
	if ((Q.rear+1)%MAXQSIZE==Q.front){
		printf("pull");
		return 0;
	}
	Q.base[Q.rear] = e;
	Q.rear = (Q.rear+1)%MAXQSIZE;
	return 1;
}

int outQueue(Queue &Q){
	if (Q.front == Q.rear){
		printf("empty");
		return 0;
	}
	Q.base[Q.front] = 0;
	Q.front = (Q.front+1)%MAXQSIZE;
	return 0;
}

void Length(Queue Q){
	int i;
	i = (Q.rear-Q.front+MAXQSIZE) % MAXQSIZE;
	printf("length is %d\n",i);
}

int main(){
	Queue Q;
	Init(Q);
	for(int i=0;i<10;i++){
		enQueue(Q,i+1);
	}
	outQueue(Q);
	outQueue(Q);
	enQueue(Q,2000);
	Get(Q);
	Length(Q);
}

  

posted @ 2022-05-30 18:25  Ccdjun  阅读(37)  评论(0)    收藏  举报