【数据结构】——栈和队列的实现(赋源码)

在前面我们已经学过顺序表以及单链表、双向表链的实现都是一种线性表,这里可以我们介绍栈和队列——是具有特殊化的线性表

栈的概念以及结构

栈:⼀种特殊的线性表,其只允许在固定的⼀端进⾏插入和删除元素操作。进⾏数据插⼊和删除操作的⼀端称为栈顶,另⼀端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。

压栈:栈的插⼊操作叫做进栈/压栈/入栈,入数据在栈顶。

出栈:栈的删除操作叫做出栈。出数据也在栈顶。 

下面我们来用一幅图来表示栈的进栈以及出栈的详细情况

 看到这幅图,很多人会想到用数组来实现,因为数组就是在尾部进行插入

将这幅图旋转其实就有一些类似于栈这一种数据结构 

本质上,栈的实现可以通过底层数组来实现 ,不过其也可以通过单链表来实现

不过,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。  

栈的底层定义(代码)

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int STDataType;
typedef struct Stack
{
	STDataType* arr;
	int capacity;
	int top;
}ST;

// 初始化栈
void STInit(ST* ps);

// ⼊栈
void STPush(ST* ps, STDataType x);

//出栈
void STPop(ST* ps);

//栈是否为空
bool STEmpty(ST* ps);

//取栈顶元素
STDataType STTop(ST* ps);

//获取栈中有效元素个数
int STSize(ST* ps);

// 销毁栈
void STDestroy(ST* ps);

栈的相关底层函数和顺序表的实现基本上是一样滴!!! 

栈的初始化 

void STInit(ST* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}

入栈 

void STPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->capacity == ps->top)
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType *tmp = (STDataType* )realloc(ps->arr, newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
	//空间足够
	ps->arr[ps->top++] = x;
}

出栈

void STPop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));

	--ps->top;
}

判断栈是否为空

//栈是否为空
bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

取栈顶元素

STDataType STTop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));

	return ps->arr[ps->top - 1];
}

获取栈的有效个数

int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

销毁栈

void STDestroy(ST* ps)
{
	assert(ps);
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}

队列

队列的概念以及结构

队列:只允许在⼀端进⾏插⼊数据操作,在另⼀端进⾏删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out)

入队列:进行插入操作的⼀端称为队尾

出队列:进行删除操作的⼀端称为队头

队列也可以数组和链表的结构实现,使⽤链表的结构实现更优⼀些,因为如果使⽤数组的结构,出队列在数组头上出数据,效率会⽐较低。 

队列的底层定义(代码)

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int QDataType;
typedef struct QueueNode
{
	QDataType data;
	struct QueueNode* next;
}QueueNode;

 typedef struct Queue
{
	struct QueueNode* phead;
	struct QueueNode* ptail;
	int size;//保持队列有效数据个数
}Queue;

 //初始化队列
 void QueueInit(Queue* pq);

 // ⼊队列,队尾
 void QueuePush(Queue* pq, QDataType x);

 //队列判空
 bool QueueEmpty(Queue* pq);

 // 出队列,队头
 void QueuePop(Queue* pq);

 //取队头数据
 QDataType QueueFront(Queue* pq);

 //取队尾数据
 QDataType QueueBack(Queue* pq);

 //队列有效元素个数
 int QueueSize(Queue* pq);

 //销毁队列
 void QueueDestroy(Queue* pq);

队列的初始化 

void QueueInit(Queue* pq)
{
	assert(pq);

	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

判断队列是否为空 

bool QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->phead == NULL && pq->ptail == NULL;
}

入队列——即队尾 

void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	//申请新节点
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
	{
		perror("malloc fail!");
		exit(1);
	}
	newnode->data = x;
	newnode->next = NULL;

	//ptail newnode
	if (pq->phead == NULL)
	{//队列为空
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		//队列不为空
		pq->ptail->next = newnode;
		pq->ptail = pq->ptail->next;//newnode
	}
	pq->size++;
}

出队列——即对头 

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	//只有一个结点的情况,避免ptail变成野指针
	if (pq->ptail == pq->phead)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else
	{
		//删除队头元素
		QueueNode* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}
	--pq->size;
}

取对头数据 

QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->phead->data;
}

取队尾数据 

QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->ptail->data;
}

队列有效元素个数

int QueueSize(Queue* pq)
{
	assert(pq);
	/*int size = 0;
	QueueNode* pcur = pq->phead;

	while (pcur)
	{
		size++;
		pcur = pcur->next;
	}

	return size;*/

	return pq->size;
}

销毁队列 

void QueueDestroy(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	QueueNode* pcur = pq->phead;
	while (pcur)
	{
		QueueNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

以上就是栈和队列的实现——感谢各位的喜欢!

  • 36
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值