顺序队列和链队列

顺序队列和链队列

队列的定义:队列是一种特殊的线性表,是一种先进先出(FIFO)的数据结构。它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。

顺序队列的结构定义

#define MAXQSIZE 10

typedef struct queue
{
	int* base;//指向动态内存
	int front;//队头指针,队头元素的下标
	int rear;//队尾指针,当前可以插入数据的下标(队尾后一个元素的下标)
	//int queuesize;//队列的总容量,要做到自动扩容就必须要增加这个成员
}SqQueue, * PSqQueue;

//初始化  
void InitQueue(PSqQueue ps);

//往队列中入数据(入队操作)
bool Push(PSqQueue ps, int val);

//获取队头元素的值,但不删除
bool GetTop(PSqQueue ps, int* rtval);

//获取队头元素的值,且删除
bool  Pop(PSqQueue ps, int* rtval);

//判断队是否为空
bool IsEmpty(PSqQueue ps);

//获取队有效数据的个数
int GetLength(PSqQueue ps);

//清空所有的数据
void Clear(PSqQueue ps);

//销毁
void Destroy(PSqQueue ps);

bool IsFull(PSqQueue ps);

顺序队列的具体实现

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "queue.h"
//初始化  
void InitQueue(PSqQueue ps)
{
	assert(ps != NULL);
	if (ps == NULL)
		return;
	ps->base = (int*)malloc(MAXQSIZE * sizeof(int));
	assert(ps->base != NULL);

	ps->front = 0;
	ps->rear = 0;
}

//往队列中入数据(入队操作)
bool Push(PSqQueue ps, int val)
{
	assert(ps != NULL);
	if (ps == NULL)
		return false;
	if (IsFull(ps))
	{
		return false;
	}

	ps->base[ps->rear] = val;
	ps->rear = (ps->rear + 1) % MAXQSIZE;
	return true;

}
bool IsFull(PSqQueue ps)
{
	return (ps->rear + 1) % MAXQSIZE == ps->front;
}
//获取队头元素的值,但不删除
bool GetTop(PSqQueue ps, int* rtval)
{
	assert(ps != NULL&&rtval!=NULL);
	if (ps == NULL|| rtval == NULL)
		return false;
	if (IsEmpty(ps))
	{
		return false;
	}

	*rtval = ps->base[ps->front];

	return true;
}

//获取队头元素的值,且删除
bool  Pop(PSqQueue ps, int* rtval)
{
	assert(ps != NULL && rtval != NULL);
	if (ps == NULL || rtval == NULL)
		return false;
	if (IsEmpty(ps))
	{
		return false;
	}

	*rtval = ps->base[ps->front];
	ps->front = (ps->front + 1) % MAXQSIZE;
	return true;
}

//判断队是否为空
bool IsEmpty(PSqQueue ps)
{
	return ps->front == ps->rear;
}

//获取队有效数据的个数
int GetLength(PSqQueue ps)//考试的重点
{
	assert(ps != NULL );
	if (ps == NULL )
		return -1;
	return (ps->rear - ps->front + MAXQSIZE) % MAXQSIZE;
}

//清空所有的数据
void Clear(PSqQueue ps)
{
	assert(ps != NULL );
	if (ps == NULL )
		return ;
	ps->front = 0;
	ps->rear = 0;
}

//销毁
void Destroy(PSqQueue ps)
{
	assert(ps != NULL);
	if (ps == NULL)
		return;
	free(ps->base);
	ps->base = NULL;
	ps->front = 0;
	ps->rear = 0;
}

链队列的结构的定义

typedef struct Node
{
	int      data;  //  元素
	struct Node* next;  //  下一个结点的地址
}Node, *PNode;

typedef struct LQueue
{
	PNode prior;
	PNode tail;
}LQueue,*PLQueue;

void InitQueue(PLQueue ps);

//往队列中入数据(入队操作)
bool Push(PLQueue ps, int val);

//获取队头元素的值,但不删除
bool GetTop(PLQueue ps, int* rtval);

//获取队头元素的值,且删除
bool  Pop(PLQueue ps, int* rtval);

//判断队是否为空
bool IsEmpty(PLQueue ps);

//获取队有效数据的个数
int GetLength(PLQueue ps);

//清空所有的数据
void Clear(PLQueue ps);

//销毁
void Destroy(PLQueue ps);

链队列的具体实现

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"lqueue.h"

void InitQueue(PLQueue ps)
{
	assert(ps != nullptr);
	if (ps == nullptr) return;

	ps->prior = nullptr;
	ps->tail = nullptr;
}

//往队列中入数据(入队操作)
bool Push(PLQueue ps, int val)
{
	assert(ps != NULL);
	if (ps == NULL)
		return false;
	
		PNode new_node = (Node*)malloc(sizeof(Node));
		assert(new_node != nullptr);
		new_node->data = val;
		new_node->next = nullptr;
		if (IsEmpty(ps))
		{
			ps->prior = new_node;
			ps->tail = new_node;
		}
		else
		{
			new_node->next = ps->tail->next;
			ps->tail->next = new_node;
			ps->tail = new_node;
		}
		return true;
}

//获取队头元素的值,但不删除
bool GetTop(PLQueue ps, int* rtval)
{
	assert(ps != NULL&&rtval!=nullptr&&IsEmpty(ps));
	if (ps == NULL||rtval==nullptr||IsEmpty(ps))
		return false;
	*rtval = ps->prior->data;
	return true;
}

//获取队头元素的值,且删除
bool  Pop(PLQueue ps, int* rtval)
{
	assert(ps != NULL && rtval != nullptr && IsEmpty(ps));
	if (ps == NULL || rtval == nullptr || IsEmpty(ps))
		return false;
	*rtval = ps->prior->data;
	if (ps->prior == ps->tail) 
	{
		free(ps->prior);
		ps->prior = ps->tail = nullptr;
	}
	else
	{
		PNode p = ps->prior->next;
		free(ps->prior);
		ps->prior = p;
	}
	return true;
}

//判断队是否为空
bool IsEmpty(PLQueue ps)
{
	assert(ps != NULL);
	if (ps == NULL)
		return false;
	return ps->tail == nullptr;
}

//获取队有效数据的个数
int GetLength(PLQueue ps)
{
	assert(ps != NULL);
	if (ps == NULL)
		return -1;
	if (IsEmpty(ps))
		return 0;
	int length = 1;
	while (ps->prior != ps->tail)
	{
		ps->prior = ps->prior->next;
		length++;
	}
	return length;
}

//清空所有的数据
void Clear(PLQueue ps)
{
	Destroy(ps);
}

//销毁
void Destroy(PLQueue ps)
{
	assert(ps != nullptr);
	if (nullptr == ps)
		return;
	Node* p = NULL;

		while (!IsEmpty(ps))
		{
			p = ps->prior;
			ps->prior = p->next;
			free(p);
		}
		ps->prior = ps->tail = nullptr;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值