栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言队列基本操作

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言队列基本操作

#define MaxSize 100
#include 
#include
typedef struct {
	int data[MaxSize];
	int front;//队头元素
	int rear;//队尾元素指向队尾元素下一个位置
}SqQueue;//顺序存储队列
void InitQueue(SqQueue Q) {
	Q.front = Q.rear = 0;//初始化空队列
}
bool isEmpty(SqQueue Q) {
	if (Q.front = Q.rear = 0)
		return true;
	else
		return false;
}//判空
bool INQueue(SqQueue Q, int x) {
	if ((Q.rear + 1) % MaxSize == Q.front){
		return false;//队列已满
}
	else {
		Q.data[Q.rear] = x;
		Q.rear = (Q.rear + 1) % MaxSize;
	}
	return true;
}//入队
bool OUTQueue(SqQueue Q, int x) {
	if (Q.rear = Q.front = 0) {
		return false;//队列为空
	}
	else {
		Q.data[Q.front] = x;
		Q.front = (Q.front + 1) % MaxSize;
	}
	return true;
}//出队

typedef struct LNode {
	int data;
	struct LNode* next;
}LNode;//定义结点
typedef struct LinkQueue {
	LNode* front;//队头指针
	LNode* rear;//队尾指针
}LinkQueue;//定义链式存储队列
void InitQueue(LinkQueue Q) {
	Q.front = Q.rear = (LNode*)malloc(sizeof(LNode));
	Q.front->next = Q.rear->next = NULL;//带头结点
}//初始化
bool isEmpty(LinkQueue Q) {
	if (Q.front != Q.rear)
		return false;
	else
		return true;
}//判空
void INQueue(LinkQueue Q,int x) {
	LNode* s = (LNode*)malloc(sizeof(LNode));
	s->data = x;
	Q.rear->next = s;
	Q.rear = s;
}//入队
bool OUTQueue(LinkQueue Q, int x) {
	if (Q.front == Q.rear)
		return false;
	else {
		LNode* p = Q.front->next;
		p->data = x;
		Q.front->next = p->next;
		if (p->next == Q.rear) {
			Q.front == Q.rear;
		}
		free(p);
	}
	return true;
}//入队

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847520.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号