队列的基本运算

2023-12-25 18:00:35

有了栈那么相对应的就有了队列(这两个有点相反的意思)?

队列的图片如下大家可以参考参考


队列的基本运算:

队列的链式结构

#define  _CRT_SECURE_NO_WARNINGS 1
//队列的链式存储结构
#include <stdio.h>
#include <stdbool.h>
typedef int ElemType;
typedef struct qnode
{
	ElemType data;
	struct qnode* next;
}DataNode;
typedef struct LinkNode
{
	DataNode* front, * rear;
}LinkQuNode;
LinkQuNode* InitQueue()
{
	LinkQuNode* q = (LinkQuNode*)malloc(sizeof(LinkQuNode));
	q->front = q->rear = NULL;
	return q;
}
void DestroyQueue(LinkQuNode* q)
{
	DataNode* pre = q->front, * p;//删除结点要一前一后
	if (pre != NULL)
	{
		p = pre->next;
		while (p != NULL)
		{
			free(pre);
			pre = p;
			p = p->next;
		}
		free(pre);
	}
	free(q);
}
bool QueueEmpty(LinkQuNode* q)
{
	return (q->rear == NULL);
}
bool enQueue(LinkQuNode* q, ElemType e)
{
	DataNode* p;
	p = (DataNode*)malloc(sizeof(DataNode));
	p->data = e;
	p->next = NULL;
	if (q->rear == NULL)
	{
		q->front = q->rear = p;
	}
	else
	{
		q->rear->next = p;
		q->rear = p;		
	}
	return true;
}
bool deQueue(LinkQuNode* q, ElemType* e)
{
	DataNode* t;
	if (q->rear == NULL) return false;
	t = q->front;
	if (q->rear == q->front)
		q->rear = q->front = NULL;
	else
	{
		q->front = q->front->next;
	}
	e = t->data;
	free(t);
	return true;
}
int main()
{

}

顺序队列

#define  _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//顺序队列的基本运算
typedef int ElemType;
#define MaxSize 100
typedef struct
{
	ElemType data[MaxSize];
	int front, rear;
}SqQueue;
SqQueue* InitQueue()
{
	SqQueue* q = (SqQueue*)malloc(sizeof(SqQueue));
	q->front = q->rear = -1;
	return q;
}
void DestroyQueue(SqQueue* q)
{
	free(q);
	return;
}
bool QueueEmpty(SqQueue* q)
{
	return (q->front == q->rear);
}
bool enQueue(SqQueue* q,ElemType e)
{
	if (q->rear == MaxSize - 1) return false;
	q->rear++;
	q->data[q->rear] = e;
	return true;
}
bool deQueue(SqQueue* q, ElemType* e)
{
	if (q->front == q->rear) return false;
	q->front++;
	*e = q->data[q->front];
	return true;
}
int main()
{
	InitQueue();
	SqQueue* q = InitQueue();
	DestroyQueue(q);
	QueueEmpty(q);

}

环形队列

#define  _CRT_SECURE_NO_WARNINGS 1
//环形队列的基本运算
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MaxSize 100
typedef int ElemType;
typedef struct
{
	ElemType data[MaxSize];
	int front,rear
}SqQueue;
SqQueue* InitQueue()
{
	SqQueue* q = (SqQueue*)malloc(sizeof(SqQueue));
	q->front = q->rear = 0;
	return q;
}
void DestroyQueue(SqQueue* q)
{
	free(q);
}
bool QueueEmpty(SqQueue* q)
{
	return (q->rear == q->front);
}
bool enQueue(SqQueue* q, ElemType e)
{
	if ((q->rear + 1) % MaxSize == q->front) return false;
	q->rear = (q->rear + 1) % MaxSize;
	q->data[q->rear] = e;
	return true;
}
bool deQueue(SqQueue* q, ElemType* e)
{
	if (q->front == q->rear) return false;
	q->front = (q->front + 1) % MaxSize;
	*e = q->data[q->front];
	return true;
}


总结:队列是先进先出,栈是先进后出,要好好利用这个特点,解决一些问题。

如果我的文章对你有帮助的话给我点个赞👍 耶耶耶。

文章来源:https://blog.csdn.net/2301_80035594/article/details/135174265
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。