循环队列的基本操作

循环队列的结构

typedef struct
{
	int front;
	int rear;
	int *base;
}Queue;

初始化

void Initqueue(Queue *q)
{
	q->base=(int*)malloc(sizeof(int)*Initsize);
	q->front=q->rear=0;
}

入队

void push(Queue *q,int x)
{
	//判断队列是否已满
	if((q->rear+1)%Initsize==q->front ) 
	{
		printf("队列已满");
		return;
	}
	q->base[q->rear]=x;
	q->rear=(q->rear+1)%Initsize;
}

出队

void pop(Queue *q,int *x) 
{
	//判断队列是否为空
	if(q->rear==q->front) 
	{
		printf("队列为空");
		return;
	}
	*x=q->base[q->front];
	q->front =(q->front +1)%Initsize;
}

取队头元素

int gettop(Queue *q)
{
	return q->base[q->front];
}

判断队列是否为空

bool empty(Queue *q)
{
	if(q->rear==q->front)
	{
		return true;
	}
	return false;
}

全部代码

#include<stdio.h>
#include<stdlib.h>
#define Initsize 20
typedef struct
{
	int front;
	int rear;
	int *base;
}Queue;

void Initqueue(Queue *q)
{
	q->base=(int*)malloc(sizeof(int)*Initsize);
	q->front=q->rear=0;
}

void push(Queue *q,int x)
{
	//判断队列是否已满
	if((q->rear+1)%Initsize==q->front ) 
	{
		printf("队列已满");
		return;
	}
	q->base[q->rear]=x;
	q->rear=(q->rear+1)%Initsize;
}

void pop(Queue *q,int *x) 
{
	//判断队列是否为空
	if(q->rear==q->front) 
	{
		printf("队列为空");
		return;
	}
	*x=q->base[q->front];
	q->front =(q->front +1)%Initsize;
}

int gettop(Queue *q)
{
	return q->base[q->front];
}

bool empty(Queue *q)
{
	if(q->rear==q->front)
	{
		return true;
	}
	return false;
}
int main()
{
	Queue q;
	Initqueue(&q);
	push(&q,1);
	push(&q,2);
	push(&q,3);
	int a;
	pop(&q,&a);
	printf("%d\n",a);
	printf("队头元素%d",gettop(&q));
	return 0;
}

链队列的结构

typedef struct Qnode
{
	int date;
	struct Qnode *next;
}Queue;
typedef struct
{
	Queue* front;//队头指针 
	Queue* rear;//队尾指针 
}Linkqueue;

初始化

void Initqueue(Linkqueue *q) 
{
	q->front=q->rear=(Queue*)malloc(sizeof(Queue));//头节点 
	q->front->next =NULL;
}

入队

void push(Linkqueue *q,int e)
{
	//就是单链表的尾插法 
	Queue* t=(Queue *)malloc(sizeof(Queue));
	t->date =e;
	t->next =NULL;
	q->rear->next =t;
	q->rear =t;
}

出队

void pop(Linkqueue *q,int *e)
{
	if(q->rear==q->front )
	{
		printf("队列为空");
		return;
	}
	//从队头删除节点 
	Queue *p=q->front->next ;
	*e=p->date ;
	q->front->next =p->next ;
	if(p==q->rear )
	{
		//特殊情况,当删除节点为最后一个的时候 
		q->rear=q->front ;
	}
	free(p);
}

取队头元素

int gettop(Linkqueue *q)
{
	if(q->front ==q->rear )
	{
		printf("队列为空");
		exit(0);
	}
	return q->front->next->date;
}

判断队列是否为空

bool empty(Linkqueue *q)
{
	if(q->front ==q->rear )
	return true;
	return false;
}

销毁链队列

void Destroy(Linkqueue *q)
{
	while(q->front)
	{
		Queue *p=q->front ;
		q->front =q->front->next ;
		free(p) ;
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
循环队列基本操作包括初始化、入队、出队、求队列长度、求队头元素、判空、清空和销毁等。以下是循环队列基本操作的实现方法: 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); } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值