循环队列的简单代码实现

队列是先进先出的一种结构,有链式的链队列也有基于数组实现的那啥循环队列。注意这不同于那个顺序表,可以直接空间不够了,赋空间,因为这个是队列的先进先出结构,噫想成一个环,不好增大空间。

#include <iostream>
#include <stdlib.h>
#define maxsize 4
#define ERROR 0
#define OK 1 
typedef int Elemtype;
using namespace std;

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

int InitQueue(queue &q)
{
  q.base=(Elemtype *)malloc(maxsize*sizeof(Elemtype));
  if(!q.base)
  {
  	return ERROR;
  }
  q.front=q.rear=0;
}

int Enqueue(queue &q,Elemtype e)
{
  if((q.rear+1)%maxsize==q.front)	
  {
  	return ERROR;
  }
  q.base[q.rear]=e;
  q.rear=(q.rear+1)%maxsize;
}

int Dequeue(queue &q,Elemtype &e)
{
  if(q.rear==q.front)
  {
  	return ERROR;
  }
  e=q.base[q.front];
  q.front=(q.front+1)%maxsize;
}

void InsertQueue(queue q)
{ 
  while(q.base[q.front])
  {
  	cout<<q.base[q.front]<<endl;
  	++q.front;
  }
}

int main()
{ 
  queue a;
  InitQueue(a);
  for(int i=0;i<=2;i++)
  {
  	int x;
  	cin>>x;
  	Enqueue(a,x);
  }
  for(int i=0;i<=1;i++)
  {
    int x;
  	Dequeue(a,x);
  }
  InsertQueue(a);
  
}

int这个可以有return 可以没有 return 但是函数内要有return 语句,必须得有int 函数类型。

删除不是真正的删除
因为q. base[0]还可以输出数来。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
循环队列是一种常见的数据结构,它可以在固定大小的数组上实现队列的功能。下面是一个简单的C语言代码实现循环队列的示例: ```c #include <stdio.h> #define MAX_SIZE 5 typedef struct { int data[MAX_SIZE]; int front; int rear; } CircularQueue; void initQueue(CircularQueue *queue) { queue->front = -1; queue->rear = -1; } int isFull(CircularQueue *queue) { if ((queue->rear + 1) % MAX_SIZE == queue->front) { return 1; } return 0; } int isEmpty(CircularQueue *queue) { if (queue->front == -1 && queue->rear == -1) { return 1; } return 0; } void enqueue(CircularQueue *queue, int item) { if (isFull(queue)) { printf("Queue is full.\n"); return; } if (isEmpty(queue)) { queue->front = 0; queue->rear = 0; } else { queue->rear = (queue->rear + 1) % MAX_SIZE; } queue->data[queue->rear] = item; } int dequeue(CircularQueue *queue) { if (isEmpty(queue)) { printf("Queue is empty.\n"); return -1; } int item = queue->data[queue->front]; if (queue->front == queue->rear) { queue->front = -1; queue->rear = -1; } else { queue->front = (queue->front + 1) % MAX_SIZE; } return item; } void displayQueue(CircularQueue *queue) { if (isEmpty(queue)) { printf("Queue is empty.\n"); return; } int i = queue->front; printf("Queue: "); while (i != queue->rear) { printf("%d ", queue->data[i]); i = (i + 1) % MAX_SIZE; } printf("%d\n", queue->data[i]); } int main() { CircularQueue queue; initQueue(&queue); enqueue(&queue, 1); enqueue(&queue, 2); enqueue(&queue, 3); enqueue(&queue, 4); enqueue(&queue, 5); displayQueue(&queue); dequeue(&queue); dequeue(&queue); displayQueue(&queue); enqueue(&queue, 6); enqueue(&queue, 7); displayQueue(&queue); return 0; } ``` 这段代码实现了一个循环队列,使用了一个固定大小为5的数组来存储队列元素。其中,`initQueue`函数用于初始化队列,`isFull`函数用于判断队列是否已满,`isEmpty`函数用于判断队列是否为空,`enqueue`函数用于入队操作,`dequeue`函数用于出队操作,`displayQueue`函数用于打印队列中的元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值