2021-09-12 数据结构与算法 3.2 队列

目录

1、循环队列

2、链式队列


1、循环队列

头文件

#pragma once
#include <stdio.h>
#include <stdlib.h>
#define maxsize 1024

typedef int datatype;
typedef struct
{
	datatype data[maxsize];
	int front;
	int rear;
}SeQueue;
SeQueue *sq= (SeQueue*)malloc(sizeof(SeQueue));

//生成循环队列
void InitQueue(SeQueue*&sq);
//置空循环队列
void SetNull(SeQueue*&sq);
//求队列长度
int Length(SeQueue*sq);
//循环队列入队
int Enqueue(SeQueue*sq, datatype x);
//循环队列出队
int Dequeue(SeQueue*sq, datatype &x);
//取循环队列队头元素
int GetFront(SeQueue*sq, datatype&x);//通过参数带回队头元素的值,取队头元素成功返回1,不成功返回0



int main()
{
	SeQueue * q;
	int show;
	InitQueue(q);
	for (int i = 0; i < 10; i++)
	{
		Enqueue(q, i);
	}
	
	for (int i = 0; i < 10; i++)
	{
		Dequeue(q, show);
		printf("%d  ", show);
	}

	return 0;
}

实现

#include "3.2.1队列.h"

//生成循环队列
void InitQueue(SeQueue*&sq)
{
	sq = (SeQueue*)malloc(sizeof(SeQueue));
	sq->front = sq->rear = 0;
}
//置空循环队列
void SetNull(SeQueue*&sq)
{
	sq->front = sq->rear = 0;//求队列长度
	int Length(SeQueue*sq);
}
//求队列长度
int Length(SeQueue*sq)
{
	int x = (sq->rear - sq->front + 1) % maxsize;
	return x;
}
//循环队列入队
int Enqueue(SeQueue*sq, datatype x)
{
	if (sq->front == (sq->rear + 1) % maxsize)//条件要牢记
	{
		printf("队列上溢");
		return 0;
	}
	else
	{
		sq->rear = (sq->rear + 1) % maxsize;//还有要记得除以总数
		sq->data[sq->rear] = x;
		return 1;
	}
}
//循环队列出队
int Dequeue(SeQueue*sq, datatype &x)
{
	if (sq->front == sq->rear)
	{
		printf("队列下溢");
		return 0;
	}
	else {
		sq->front = (sq->front + 1) % maxsize;
		x = sq->data[sq->front];
		return 1;
	}
}
//取循环队列队头元素
int GetFront(SeQueue*sq, datatype&x)
{
	if (sq->front == sq->rear)
	{
		printf("队列下溢");
		return 0;
	}
	else
	{
		x = sq->data[(sq->front + 1) % maxsize];
		return 1;
	}
}

2、链式队列

头文件

#pragma once
#include <stdio.h>
#include <stdlib.h>
#define maxsize 1024

typedef int datatype;
typedef struct node
{
	datatype data;
	struct node*next;
}QueueNode;
typedef struct
{
	QueueNode *front, *rear;
}LinkQueue;
//队列初始化
void InitQueue(LinkQueue *&q);
//置空队列
void SetNull(LinkQueue*q);
//判断队列是否为空
int Empty(LinkQueue*q);
//入队
void EnQueue(LinkQueue*q, datatype x);
//出队
int DeQueue(LinkQueue*q, datatype &x);
//取队头元素
int GetFront(LinkQueue*q, datatype&x);

//案例3.3 入队算法
void Eg_EnQueue(QueueNode*&rear, datatype x);
//出对算法
int Eg_DeQueue(QueueNode*&rear, datatype &x);

int main()
{
	LinkQueue *q;
	InitQueue(q);
	Empty(q);

	for (int i = 0; i < 20; i++)
	{
		EnQueue(q, i);
	}
	int show;
	for (int i = 0; i < 20; i++)
	{
		DeQueue(q, show);
		printf("%d  ", show);
	}
	return 0;
}

实现

#include "3.2.2链队列.h"

//队列初始化
void InitQueue(LinkQueue *&q)
{
	q = (LinkQueue*)malloc(sizeof(LinkQueue));
	q->front = q->rear = (QueueNode*)malloc(sizeof(QueueNode));
	q->front->next = NULL;
}
//置空队列
void SetNull(LinkQueue*q)
{
	q->rear = q->front;
	q->front->next = NULL;
	
}
//判断队列是否为空
int Empty(LinkQueue*q)
{
	if (q->front == q->rear)
	{
		//printf("队列为空");
		return 1;
	}
	else
	{
		//printf("队列不为空");
		return 0;
	}
}
//入队
void EnQueue(LinkQueue*q, datatype x)
{
	q->rear->next = (QueueNode*)malloc(sizeof(QueueNode));
	q->rear = q->rear->next;
	q->rear->data = x;
	q->rear->next = NULL;
}
//出队
int DeQueue(LinkQueue*q, datatype &x)
{
	QueueNode*s;
	if (Empty(q))
	{
		printf("队列下溢");
		return 0;
	}
	else
	{	
		s = q->front->next;
		if (s->next == NULL)//多想一步,要增加判断是否表长只为1,若表长为1,则置空表
		{
			q->front->next = NULL;
		}
		else {
			q->front->next = s->next;
		}
		x = s->data;
		free(s);
		return 1;
	}
}
//取队头元素
int GetFront(LinkQueue*q, datatype&x)
{
	if (Empty(q))
	{
		printf("队列下溢");
		return 0;
	}
	else
	{
		x = q->front->next->data;
		return 1;
	}
}

//案例3.3 入队算法
void Eg_EnQueue(QueueNode*&rear, datatype x)
{
		QueueNode*s = (QueueNode*)malloc(sizeof(QueueNode));
		s->data = x;
		s->next = rear->next;
		rear->next = s;
		rear = s;
}
//出对算法
int Eg_DeQueue(QueueNode*&rear, datatype &x)
{
	if (rear == NULL)
	{
		printf("链队列下溢");
		return 0;
	}
	else
	{
		QueueNode*s;
		s = rear->next->next;
		x = s->data;
		if (rear == s)
		{//置空表
			rear = rear->next;
			rear->next = rear;
		}
		else
		{
			rear->next->next = s->next;
		}
		free(s);
	}
	return 1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值