顺序循环队列基本操作(入队,出队,清空,销毁,历遍)

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

#define MAXSIZE   5
#define OVERFLOW -1
#define OK        1
#define TRUE      1
#define FALSE     0
#define ERROR     1

typedef int QElemType;
typedef int Status;


struct SqQueue
{
	QElemType *base;
	int front;
	int rear;
};

Status InitQueue(SqQueue &Q)
{
	Q.base = (QElemType *)malloc(MAXSIZE * sizeof(QElemType));
	if(!Q.base)
		exit(OVERFLOW);
	Q.front = Q.rear = 0;
	return OK;
}

Status DestoryQueue(SqQueue &Q)
{
	if(Q.base)
		free(Q.base);
	Q.base = NULL;
	Q.front = Q.rear = 0;
	return OK;
}

Status ClearQueue(SqQueue &Q)
{
	Q.front = Q.rear = 0;	
	return OK;
}

Status QueueEmpty(SqQueue Q)
{
	if(Q.front == Q.rear)
		return TRUE;
	else
		return FALSE;
}

int QueueLength(SqQueue Q)
{
	return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
}

Status GetHead(SqQueue Q, QElemType &d)
{
	if(Q.front == Q.rear) return ERROR;
	d = *(Q.base + Q.front);
	return OK;
}

Status EnQueue(SqQueue &Q,QElemType e)
{
	if((Q.rear + 1)%MAXSIZE == Q.front)
		return TRUE;
	Q.base[Q.rear] = e;
	Q.rear = (Q.rear + 1)%MAXSIZE;
	return OK;
}

Status DeQueue(SqQueue &Q,QElemType &e)
{
	if(Q.front == Q.rear)
		return ERROR;
	e = Q.base[Q.front];
	Q.front = (Q.front + 1)%MAXSIZE;
	return OK;
}

Status QueueTraverse(SqQueue Q, void(*visit)(QElemType))
{
	int i;
	i = Q.front;
	while(i != Q.rear)
	{
		visit(Q.base[i]);
		i = (i+1)%MAXSIZE;
	}
	printf("\n");	
	return OK;
}

void visit(QElemType e)
{
	printf("%d ",e);
}

int main()
{
	int i = 0,l;
	Status j;
	QElemType d;
	SqQueue Q;
	InitQueue(Q);
	printf("After init queue,is it empty?%d(1:YES 0:NO)\n",QueueEmpty(Q));
	printf("Please input int type queue (length is less than %d), -1 is end character:\n",MAXSIZE - 1);

	do
	{
		scanf("%d",&d);
		++i;
		if(d == -1)
			break;
		EnQueue(Q,d);
	}while(i < MAXSIZE -1);
	printf("The length of queue is:%d\n",QueueLength(Q));
	printf("Is it empty?%d(1:YES 0:NO)\n",QueueEmpty(Q));
	printf("Delete element % times from the head of queue, insert element at the end of queue:\n",MAXSIZE);
	
	for(i = 0; i < MAXSIZE; ++i)
	{
		DeQueue(Q,d);
		printf("The deleted element is %d,please input the new element:\n",d);
		scanf("%d",&d);
		EnQueue(Q,d);
	}
	l = QueueLength(Q);
	printf("The elements of queue are:\n");
	QueueTraverse(Q,visit);
	printf("the number of inserted data at end of queue is:%d\n", i+MAXSIZE);
	if(l - 2 > 0)
		printf("delete %d elements:\n",l-2);
	while(QueueLength(Q) > 2)
	{
		DeQueue(Q,d);
		printf("the deleted element is:%d\n",d);
	}
	j = GetHead(Q,d);
	if(j)
		printf("the head element is:%d\n",d);
	ClearQueue(Q);
	printf("After clear queue,is it empty?(1:YES 0:NO)\n",QueueEmpty(Q));
	DestoryQueue(Q);
	return 0;
}

  • 10
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
循环队列基本操作包括初始化、入队、求队列长度、求头元素、判销毁等。以下是循环队列基本操作的实现方法: 1. 初始化:创建一个循环队列,需要指定队列的最大长度。 2. 入队:将元素插入到队列尾,如果队列已满则无法插入。 3. :将队列头元素删除并返回,如果队列则无法删除。 4. 求队列长度:返回队列中元素的个数。 5. 求头元素:返回队列头元素,但不删除。 6. 判:判断队列是否为。 7. 队列中的所有元素。 8. 销毁销毁队列,释放内存间。 以下是一个基于顺序表实现的循环队列的代码示例: ``` #define MAXSIZE 100 // 队列的最大长度 typedef struct { int data[MAXSIZE]; // 存储队列元素的数组 int front; // 头指针 int rear; // 尾指针 } CircularQueue; // 初始化循环队列 void InitQueue(CircularQueue *q) { q->front = q->rear = 0; } // 判断队列是否为 int IsEmpty(CircularQueue *q) { return q->front == q->rear; } // 判断队列是否已满 int IsFull(CircularQueue *q) { return (q->rear + 1) % MAXSIZE == q->front; } // 入队 int EnQueue(CircularQueue *q, int x) { if (IsFull(q)) { return 0; // 队列已满,无法插入 } q->data[q->rear] = x; q->rear = (q->rear + 1) % MAXSIZE; return 1; } // int DeQueue(CircularQueue *q, int *x) { if (IsEmpty(q)) { return 0; // 队列,无法删除 } *x = q->data[q->front]; q->front = (q->front + 1) % MAXSIZE; return 1; } // 求队列长度 int QueueLength(CircularQueue *q) { return (q->rear - q->front + MAXSIZE) % MAXSIZE; } // 求头元素 int GetHead(CircularQueue *q, int *x) { if (IsEmpty(q)) { return 0; // 队列,无法获取头元素 } *x = q->data[q->front]; return 1; } // 队列 void ClearQueue(CircularQueue *q) { q->front = q->rear = 0; } // 销毁队列 void DestroyQueue(CircularQueue *q) { free(q); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值