【数据结构】栈与队列的实现

栈与队列是数据结构中重要的结构,

可以用于解决一些题目

模拟实现时可以增加对于这些结构的理解,也可以巩固我们的语言水平,解决某些题目也会有很好的效果

话不多说

目录

栈的实现

先来看一下栈的定义:

栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端

称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。

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

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

栈的实现 一般可以使用数组或者链表实现,

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

结构体的定义:

c 复制代码
// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;		// 栈顶
	int capacity;  // 容量 
}Stack;

初始化栈:

栈在初始化时,

我们可以定义top为栈顶元素的下一个,故这样我们就可以将top定义为0,若将top定义为栈顶元素,则需要将top定义为-1

这里我在仔细的讲解一下:

top0时,我们不能将top视作栈顶元素,因为如果压栈,压入的元素就会在top == 0的位置压入,压入的top仍为0,我们将判断不了top == 0时是否有元素,

则若top = 0,我们应当定义栈顶元素的下一个,这样我们赋值时:ps->a[top] = data;top++;

同理,若是初始化top == -1,则赋值:top++;ps->a[top] = data;

c 复制代码
// 初始化栈 
void StackInit(Stack* ps)
{
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

压栈:

因为只有压栈时会增加元素个数,故我们不需要封装一个函数用来创造新节点

c 复制代码
void StackPush(Stack* ps, STDataType data)
{
	assert(ps);
	//检查容量
	if (ps->capacity == ps->top)
	{
		int newcapacity = (ps->capacity == 0 ? 4 : 2 * ps->capacity);
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = data;
	ps->top++;
}

出栈:

c 复制代码
void StackPop(Stack* ps)
{
	assert(ps);
	assert(ps->top);
	ps->top--;
}

获取栈顶元素:

c 复制代码
STDataType StackTop(Stack* ps)
{
	assert(ps);
	return ps->a[ps->top - 1];
}

获取栈中有效元素个数 :

c 复制代码
int StackSize(Stack* ps)
{
	assert(ps);
	return ps->top;
}

检测栈是否为空:

检测栈是否为空,如果为空返回非零结果,如果不为空返回0

c 复制代码
bool StackEmpty(Stack* ps)
{
	assert(ps);
	return ps->top == 0;
}

销毁栈 :

c 复制代码
void StackDestroy(Stack* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

栈模拟源代码:

stack.c

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1

#include "stack.h"

// 初始化栈 
void StackInit(Stack* ps)
{
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

void StackPush(Stack* ps, STDataType data)
{
	assert(ps);
	//检查容量
	if (ps->capacity == ps->top)
	{
		int newcapacity = (ps->capacity == 0 ? 4 : 2 * ps->capacity);
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] =data;
	ps->top++;
}

void StackPop(Stack* ps)
{
	assert(ps);
	assert(ps->top);
	ps->top--;
}

STDataType StackTop(Stack* ps)
{
	assert(ps);
	return ps->a[ps->top - 1];
}

int StackSize(Stack* ps)
{
	assert(ps);
	return ps->top;
}

bool StackEmpty(Stack* ps)
{
	assert(ps);
	return ps->top == 0;
}

void StackDestroy(Stack* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

stack.h

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;		// 栈顶
	int capacity;  // 容量 
}Stack;

// 初始化栈 
void StackInit(Stack* ps);
// 入栈 
void StackPush(Stack* ps, STDataType data);
// 出栈 
void StackPop(Stack* ps);
// 获取栈顶元素 
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数 
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
bool StackEmpty(Stack* ps);
// 销毁栈 
void StackDestroy(Stack* ps);

队列的实现:

队列的定义:

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出

FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾 出队列:进行删除操作的一端称为队头

队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数

组头上出数据,效率会比较低。

结构体的定义:

我们使用链表进行尾插时,需要遍历一遍,效率并不高,因此我们可以使用结构体再次定义一个包含头指针与尾指针的结构体,帮助我们尾插,即push,因此,我们传参时只传入Queue的地址即可,

也取消了对二级指针的使用,因为我们使用不带头的单链表时需要使用二级指针

c 复制代码
typedef int QDataType;
// 链式结构:表示队列 
typedef struct QListNode
{
	struct QListNode* next;
	QDataType data;
}QNode;

// 队列的结构 
typedef struct Queue
{
	QNode* front;
	QNode* rear;
}Queue;

初始化队列:

c 复制代码
void QueueInit(Queue* q)
{
	assert(q);
	q->front = NULL;
	q->rear = NULL;
}

队尾入队列:

同理,队列在push时,同样不需要封装创建新节点的函数

c 复制代码
void QueuePush(Queue* q, QDataType data)
{
	assert(q);

	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->data = data;
	newnode->next = NULL;
	if (q->front == NULL)
	{
		q->front = q->rear = newnode;
	}
	else
	{
		q->rear->next = newnode;
		q->rear = newnode;
	}
}

队头出队列

在没有元素和只有一个元素时需要单独检查:

因为如果只有一个元素时,frontrear指向同一个地方,释放掉front,缺保留了rear这个野指针时十分危险的,因此我们也要将q->rear = NULL

c 复制代码
void QueuePop(Queue* q)
{
	assert(q);
	assert(q->front);

	QNode* next = q->front->next;
	if (q->front == NULL)
	{
		q->rear = NULL;
	}
	free(q->front);
	q->front = next;
}

获取队列头部元素

c 复制代码
QDataType QueueFront(Queue* q)
{
	assert(q);
	assert(q->front);
	return q->front->data;
}

获取队列队尾元素

c 复制代码
QDataType QueueBack(Queue* q)
{
	assert(q);
	assert(q->rear);
	return q->rear->data;
}

获取队列中有效元素个数

c 复制代码
int QueueSize(Queue* q)
{
	assert(q);
	int count = 0;
	QNode* cur = q->front;
	while (cur != NULL)
	{
		cur = cur->next;
		count++;
	}
	return count;
}

检测队列是否为空

检测队列是否为空,如果为空返回非零结果,如果非空返回0

c 复制代码
bool QueueEmpty(Queue* q)
{
	assert(q);
	return q->front == NULL;
}

销毁队列

c 复制代码
void QueueDestroy(Queue* q)
{
	assert(q);
	while (q->front)
	{
		QNode* next = q->front->next;
		free(q->front);
		q->front = next;
	}
}

队列源代码:

queue.h

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int QDataType;
// 链式结构:表示队列 
typedef struct QListNode
{
	struct QListNode* next;
	QDataType data;
}QNode;

// 队列的结构 
typedef struct Queue
{
	QNode* front;
	QNode* rear;
}Queue;

// 初始化队列 
void QueueInit(Queue* q);
// 队尾入队列 
void QueuePush(Queue* q, QDataType data);
// 队头出队列 
void QueuePop(Queue* q);
// 获取队列头部元素 
QDataType QueueFront(Queue* q);
// 获取队列队尾元素 
QDataType QueueBack(Queue* q);
// 获取队列中有效元素个数 
int QueueSize(Queue* q);
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0 
bool QueueEmpty(Queue* q);
// 销毁队列 
void QueueDestroy(Queue* q);

queue.c

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1

#include"queue.h"

void QueueInit(Queue* q)
{
	assert(q);
	q->front = NULL;
	q->rear = NULL;
}

void QueuePush(Queue* q, QDataType data)
{
	assert(q);

	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->data = data;
	newnode->next = NULL;
	if (q->front == NULL)
	{
		q->front = q->rear = newnode;
	}
	else
	{
		q->rear->next = newnode;
		q->rear = newnode;
	}
}

void QueuePop(Queue* q)
{
	assert(q);
	assert(q->front);

	QNode* next = q->front->next;
	if (q->front == NULL)
	{
		q->rear = NULL;
	}
	free(q->front);
	q->front = next;
}

QDataType QueueFront(Queue* q)
{
	assert(q);
	assert(q->front);
	return q->front->data;
}

bool QueueEmpty(Queue* q)
{
	assert(q);
	return q->front == NULL;
}

QDataType QueueBack(Queue* q)
{
	assert(q);
	assert(q->rear);
	return q->rear->data;
}

int QueueSize(Queue* q)
{
	assert(q);
	int count = 0;
	QNode* cur = q->front;
	while (cur != NULL)
	{
		cur = cur->next;
		count++;
	}
	return count;
}

void QueueDestroy(Queue* q)
{
	assert(q);
	while (q->front)
	{
		QNode* next = q->front->next;
		free(q->front);
		q->front = next;
	}
}

有问题可以及时找博主沟通,欢迎讨论

相关推荐
虚拟搬运工11 分钟前
Python类及元类的创建流程
开发语言·chrome·python
Aurora_th30 分钟前
树与图的深度优先遍历(dfs的图论中的应用)
c++·算法·深度优先·图论·dfs·树的直径
消失的旧时光-194331 分钟前
kotlin的密封类
android·开发语言·kotlin
A_cot35 分钟前
Redis 的三个并发问题及解决方案(面试题)
java·开发语言·数据库·redis·mybatis
学步_技术35 分钟前
Python编码系列—Python原型模式:深克隆与高效复制的艺术
开发语言·python·原型模式
alden_ygq41 分钟前
GCP容器镜像仓库使用
java·开发语言
苹果酱05671 小时前
一文读懂SpringCLoud
java·开发语言·spring boot·后端·中间件
Eoneanyna1 小时前
QT设置git仓库
开发语言·git·qt
aqymnkstkw1 小时前
2024年【电气试验】考试题库及电气试验模拟试题
大数据·c语言·人工智能·嵌入式硬件·安全
小鹿( ﹡ˆoˆ﹡ )1 小时前
Python中的树与图:构建复杂数据结构的艺术
开发语言·python