C语言数据结构算法队列之循环队列的基本操作(创建队列,入队,出队,求长度,取队头,判断队列满空,打印队列)

完整代码:

#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define M  100
//循环队列结构体定义 
typedef int ElemType;
typedef struct node {
	ElemType *elem;
	int front,rear;
	int QueueSize;
} SeqQueue;

//创建算法 
int InitQueue(SeqQueue *Q) {
	Q->elem=(ElemType*)malloc(sizeof(ElemType)*M);
	if(!(Q->elem)) {
		return ERROR;
	}
	Q->QueueSize=M;
	Q->front=Q->rear=0;
	return OK;
}

//入队算法 
int EnQueue(SeqQueue *Q,ElemType e) {
	if(Q->front==(Q->rear+1)%Q->QueueSize) {
		return ERROR;
	}
	Q->elem[Q->rear]=e;
	Q->rear=(Q->rear+1)%Q->QueueSize;
	return OK;
}
//出队算法 
int DeQueue(SeqQueue *Q,ElemType *e) {
	if(Q->front==Q->rear) {
		return ERROR;
	}
	*e=Q->elem[Q->front];
	Q->front=(Q->front+1)%Q->QueueSize;
	return OK;
}
//判断队空 
int QueueEmpty(SeqQueue *Q) {
	if(Q->front==Q->rear) {
		return OK;
	} else {
		return ERROR;
	}
}

//判断队满 
int IsFull(SeqQueue *Q) {
	if(Q->front==(Q->rear+1)%Q->QueueSize) {
		return OK;
	} else {
		return ERROR;
	}
}
//求队列长度 
int QueueLength(SeqQueue *Q) {
	return (Q->rear-Q->front+Q->QueueSize)%Q->QueueSize;
}

//打印队列 
void PrintfQueue(SeqQueue *Q) {
	if(Q->front==Q->rear) {
		printf("\nThis is null!");
	}else{
	for(int i=Q->front; i<Q->rear; i++) {
		printf("%d",Q->elem[i]);
	}
}
}
int main(void) {
	SeqQueue Q;
	ElemType e,e1;
	scanf("%d",&e);
	InitQueue(&Q);
	EnQueue(&Q,e);
	DeQueue(&Q,&e1);
	printf("%d\n",e1);
	PrintfQueue(&Q);
	return 0;
}

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
C语言数据结构算法中,实现顺序队列基本操作包括入队出队判断队列是否为空判断队列是否、获取队元素等操作。其中,顺序队列是一种基于数组实现队列,它的特点是元素在队列中的位置是固定的,队列和尾都是可以移动的指针。以下是实现顺序队列的各种基本操作的方法: 1. 入队操作:将元素插入队列尾部,同时移动队列尾指针。 2. 出队操作:将队列部元素删除,同时移动队列指针。 3. 判断队列是否为空:当队列指针和队列尾指针相同时,队列为空。 4. 判断队列是否:当队列尾指针指向队列的最后一个元素时,队列。 5. 获取队元素:返回队列部元素的值,但不删除该元素。 6. 获取队长度:通过队列指针和队列尾指针的位置关系计算得出队长度。 以下是实现顺序队列基本操作的代码实现: ``` #define MAXSIZE 100 // 定义队列的最大长度 typedef int DataType; // 定义队列元素的数据类型 typedef struct { DataType data[MAXSIZE]; // 队列的元素数组 int front; // 队列指针 int rear; // 队列尾指针 } SeqQueue; // 初始化队列 void InitQueue(SeqQueue *Q) { Q->front = Q->rear = 0; } // 判断队列是否为空 int IsEmpty(SeqQueue Q) { return Q.front == Q.rear; } // 判断队列是否 int IsFull(SeqQueue Q) { return (Q.rear + 1) % MAXSIZE == Q.front; } // 入队操作 int EnQueue(SeqQueue *Q, DataType x) { if (IsFull(*Q)) { return 0; // 队列入队失败 } Q->data[Q->rear] = x; Q->rear = (Q->rear + 1) % MAXSIZE; return 1; // 入队成功 } // 出队操作 int DeQueue(SeqQueue *Q, DataType *x) { if (IsEmpty(*Q)) { return 0; // 队列为空,出队失败 } *x = Q->data[Q->front]; Q->front = (Q->front + 1) % MAXSIZE; return 1; // 出队成功 } // 获取队元素 int GetHead(SeqQueue Q, DataType *x) { if (IsEmpty(Q)) { return 0; // 队列为空,获取队元素失败 } *x = Q.data[Q.front]; return 1; // 获取队元素成功 } // 获取队长度 int GetLength(SeqQueue Q) { return (Q.rear - Q.front + MAXSIZE) % MAXSIZE; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扎心小指针0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值