队列ADT+实现代码

目录

队列

ADT 

代码实现

链接:https://blog.youkuaiyun.com/qq_29350001/article/details/55261466


队列

队列是一种先进先出的线性表,他只允许在表的一端进行插入,另一端进行删除

队头(front):允许删除的一端

队尾(rear):允许插入的一端

ADT 

ADT Queue{
    数据对象:D = {ai|ai∈ElemSet,i = 1,2,...,n, n>= 0}
    数据关系:R1 = {<ai-1,ai>|ai-1,ai∈D,i = 2,...,n}
        约定其中a1端为队列头,an端为队列尾
    基本操作:
    InitQueue(*Q):初始化操作,建立一个空队列Q。
	DestroyQueue(*Q):若队列Q存在,則销毀它。
	ClearQueue(*Q):将队列 Q 清空。
	QueueEmpty(Q):若队列Q为空,送回true,否則退回false。
	GetHead(Q, *e):若队列Q存在且非空,用e返因队列Q的队头元素。
	EnQueue(*Q,e):若队列Q存在,插入新元素e到队列Q中并成为队尾元素。 
	DeQueue(*Q, *e):刪除队列Q中队头元素,并用e返回其值。	
	QueueLength(Q):送回队列Q的元素个教。
}ADT Queue;

代码实现

链式存储结构 

// 队列.cpp : 定义控制台应用程序的入口点。
//队列的链式存储结构

#include <stdio.h>
# include<stdlib.h>

# define Status int
# define QElemType int 
# define OVERFLOW -1
# define OK 1
# define ERROR 0
# define TRUE 1
# define FALSE 0

typedef struct QNode
{
	QElemType data;
	struct QNode *next;
}QNode,*QueuePtr;

typedef struct
{
	QueuePtr front, rear;
}LinkQueue;

//初始化队列
Status InitQueue(LinkQueue *q)
{
	if (!q)
	{
		printf("队列不存在!\n");
		return ERROR;
	}

	q->front = q->rear = (QueuePtr)malloc(1 * sizeof(QNode));

	if (!q->front)
	{
		printf("内存分配失败!\n");
		exit(OVERFLOW);
	}

	q->front->next = NULL;

	return OK;
}

//清空队列
Status ClearQueue(LinkQueue *q)
{
	if (!q)
	{
		printf("队列不存在!\n");
		return ERROR;
	}

	QueuePtr p = q->front->next, tmp;

	while (p)
	{
		tmp = p->next;
		free(p);
		p = tmp;
	}
	q->front = q->rear;

	return OK;
}

//销毁队列
Status DestoryQueue(LinkQueue *queue)
{
	if (!queue)
	{
		printf("队列不存在!\n");
		return ERROR;
	}

	while (queue->front != NULL) {
		queue->rear = queue->front->next;
		free(queue->front);
		queue->front = queue->rear;
	}

	return OK;
}

//空队列
Status QueueEmpty(LinkQueue q)
{
	if (!&q)
	{
		printf("空队列!\n");
		return ERROR;
	}

	if (q.front == q.rear)
		return TRUE;
	else
		return FALSE;
}

//插入元素e为q的新队尾元素
Status EnQueue(LinkQueue *q, QElemType e)
{
	QueuePtr p = (QueuePtr)malloc(1 * sizeof(QNode));
	if (!q)
	{
		printf("内存分配失败!\n");
		return ERROR;
	}

	p->data = e;
	p->next = NULL;
	q->rear->next = p;
	q->rear = p;

	return OK;
}

//出队
Status DeQueue(LinkQueue *q, QElemType *e)
{
	if (!q)
	{
		printf("队列不存在!\n");
		return ERROR;
	}

	if (QueueEmpty(*q))
	{
		printf("空队列!\n");
		return ERROR;
	}

	QueuePtr p = (*q).front->next;
	*e = p->data;
	(*q).front->next = p->next;
	if ((*q).front == (*q).rear)
	{
		(*q).rear = (*q).front;
	}
	free(p);

	return OK;
}

//队列长度
Status QueueLength(LinkQueue q)
{
	if (!&q)
	{
		printf("不存在!\n");
		return ERROR;
	}

	int len = 0;
	QueuePtr p = q.front->next;

	while (p)
	{
		len++;
		p = p->next;
	}

	return len;
}

//获取队首元素
Status GetHead(LinkQueue q, QElemType *e)
{
	if (!(&q))
	{
		printf("队列不存在!\n");
		return ERROR;
	}

	if (QueueEmpty(q))
	{
		printf("空队列!\n");
		return ERROR;
	}

	*e = q.front->next->data;

	return OK;
}

//访问函数
Status visit(QElemType e)
{
	printf("%d ",e);
	return OK;
}

//遍历队列
Status QueueTraverse(LinkQueue q, Status(*visit)(QElemType e))
{
	if (!(&q))
	{
		printf("队列不存在!\n");
		return ERROR;
	}
	if (QueueEmpty(q))
	{
		printf("队列为空!\n");
		return ERROR;
	}

	QueuePtr p = q.front->next;
	while (p)
	{
		visit(p->data);
		p = p->next;
	}

	return OK;
}

int main()
{
	LinkQueue que;
	QElemType data;
	int n;

	InitQueue(&que);
	
	printf("输入队列元素数量:\n");
	scanf_s("%d", &n);
	printf("输入队列中元素:\n");
	while (n--)
	{
		scanf_s("%d",&data);
		EnQueue(&que, data);
	}

	printf("遍历队列:\n");
	QueueTraverse(que, visit);
	printf("\n");

	
	/*
	DestoryQueue(&que);

	printf("遍历队列:\n");
	QueueTraverse(que, visit);
	printf("\n");

	DeQueue(&que, &data);
	printf("队首部元素是:%d\n", data);
	printf("遍历队列:\n");
	QueueTraverse(que, visit);
	
	GetHead(que, &data);
	printf("队列首元素是:%d\n",data);
	printf("长度:%d\n", QueueLength(que));
	DeQueue(&que, &data);
	printf("%d\n", que.front->next->next->data);
	*/
    return 0;
}

顺序存储结构(循环队列)

// 循环队列.cpp : 定义控制台应用程序的入口点。
// 牺牲一个元素的空间,队列头指针在队列尾指针的下一位置上 最为队列满的标志

#include <stdio.h>
# include<stdlib.h>

# define Status int
# define ElemType int
# define OK 1
# define ERROR 0
# define OVERFLOW -1
# define TRUE 1
# define FALSE 0
# define MAXSIZE 10
# define MAXRC 10

typedef struct
{
	QElemType *base;
	int front;
	int rear;
}SqQueue;

//初始化队列
Status InitQueue(SqQueue *q)
{
	q->base = (QElemType *)malloc(MAXQSIZE * sizeof(QElemType));
	if (!q->base)
	{
		printf("内存分配失败!\n");
		exit(OVERFLOW);
	}

	q->front = q->rear = 0;

	return OK;
}

//空队列
Status EmptyQueue(SqQueue q)
{
	if (q.front == q.rear)
		return TRUE;
	else
		return FALSE;
}

//入队
Status EnQueue(SqQueue *q, QElemType e)
{
	if ((q->rear + 1) % MAXQSIZE == q->front)
	{
		printf("队列满!\n");
		return ERROR;
	}

	q->base[q->rear] = e;
	q->rear = (q->rear + 1) % MAXQSIZE;

	return OK;
}

//出队
Status DeQueue(SqQueue *q, QElemType *e)
{
	if (q->front == q->rear)
		return ERROR;

	*e = q->base[q->front];
	q->front = (q->front + 1) % MAXQSIZE;

	return OK;
}

//访问函数
Status visit(QElemType e)
{
	printf("%d ",e);

	return OK;
}

//遍历
Status Traverse(SqQueue q, Status(*visit)(QElemType e))
{
	if (!&q)
	{
		printf("队列不存在!\n");
		return ERROR;
	}

	QElemType *t = &q.base[q.front];

	while (t != &q.base[q.rear])
	{
		visit(*t);
		t++;
	}

	return OK;
}

//求队列长度
Status QueueLength(SqQueue q)
{
	return (q.rear - q.front + MAXQSIZE) % MAXQSIZE;
}

int main()
{
	int n;
	QElemType data;
	SqQueue q;

	InitQueue(&q);
	if (EmptyQueue(q))
	{
		printf("空队列\n");
	}

	printf("输入队列中元素个数:\n");
	scanf_s("%d",&n);
	printf("输入队列元素\n");
	while (n--)
	{
		scanf_s("%d",&data);
		EnQueue(&q, data);
	}
	printf("遍历队列!\n");
	Traverse(q, visit);
	printf("\n");
	printf("队列长度为:%d\n",QueueLength(q));

/*
	DeQueue(&q, &data);
	printf("删除的队首元素是:%d\n",data);
	printf("遍历队列!\n");
	Traverse(q, visit);
	printf("\n");
	*/
    return 0;
}

链接:数据结构与算法 -- 队列 ADT_队列adt_聚优致成的博客-优快云博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AmosTian

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

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

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

打赏作者

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

抵扣说明:

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

余额充值