栈和队列的实现

1.栈

1.1栈的概念及结构

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

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

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

1.2栈的实现

栈的实现一般可以用数组或是链表实现,相对而言数组的结构实现更优一些,因为数组在尾上插入数据的代价比较小。

Stack.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int STDateType;

typedef struct stack
{
	STDateType* a;
	int top;
	int capacity;
}stack;

void STInit(stack* pst);//初始化

void STDestory(stack* pst);//销毁

void STPush(stack* pst, STDateType x);//入栈

void STPop(stack* pst);//出栈

STDateType STTop(stack* pst);//取栈顶元素

bool STEmpty(stack* pst);//判空

int STSize(stack* pst);//获取元素个数

stack.c

#include"stack.h"

void STInit(stack* pst)//初始化
{
	assert(pst);

	pst->a = NULL;
	pst->capacity = 0;
	pst->top = 0;
}

void STDestory(stack* pst)//销毁
{
	assert(pst);

	free(pst->a);
	pst->a = NULL;
	pst->capacity = pst->top = 0;
}

void STPush(stack* pst, STDateType x)//入栈
{
	assert(pst);
	if (pst->top == pst->capacity)
	{
		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDateType* tmp = (STDateType*)realloc(pst->a,(newcapacity *sizeof(STDateType)));
		
		pst->a = tmp;
		pst->capacity = newcapacity;
	}
	pst->a[pst->top++] = x;

}

void STPop(stack* pst)//出栈
{
	assert(pst);
	assert(pst->top > 0);

	pst->top--;
}

STDateType STTop(stack* pst)//取栈顶元素
{
	assert(pst);
	assert(pst->top > 0);

	return pst->a[pst->top - 1];
}

bool STEmpty(stack* pst)//判空
{
	assert(pst);
	return pst->top == 0;
}

int STSize(stack* pst)//获取元素个数
{
	assert(pst);
	return pst->top;
}

test.c

#include"stack.h"

int main()
{
	stack st;
	STInit(&st);
	STPush(&st, 1);
	STPush(&st, 2);
	STPush(&st, 3);
	STPush(&st, 4);
	while (!STEmpty(&st))
	{
		printf("%d\n", STTop(&st));
		STPop(&st);
	}
	STDestory(&st);
}

2.队列

2.1队列的概念和结构

队列:只允许在一端进行插入数据操作,在另一端进行删除数据的操作的特殊线性表,队列具有先进先出的特性FIFO(Frist In First Out)入队列,进行插入操作的一端称为队尾。出队列,进行删除的一端称为队头。

2.2队列的实现

queue.h

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

typedef int QDataType;

typedef struct QueueNode
{
	QDataType val;
	struct QueueNode* next;
}QueueNode;

typedef struct Queue
{
	QueueNode* qhead;
	QueueNode* qtail;
	int size;
}Queue;

void QInit(Queue* pq);//初始化

void QDestory(Queue* pq);//销毁

void QPush(Queue* pq, QDataType x);//入队列

void QPop(Queue* pq);//出队列

bool QEmpty(Queue* pq);//判空

int QSize(Queue* pq);//队列元素个数

QDataType QueueFront(Queue* pq);//队列头元素

QDataType QueueBack(Queue* pq);//队列尾元素

queue.c

#include"queue.h"

void QInit(Queue* pq)//初始化
{
	assert(pq);
	
	pq->qhead = pq->qtail = NULL;
	pq->size = 0;
}

void QDestory(Queue* pq)//销毁
{
	QueueNode* cur = pq->qhead;
	while (cur)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->qhead = pq->qtail = NULL;
	pq->size = 0;
}

void QPush(Queue* pq, QDataType x)//入队列
{
	assert(pq);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->next = NULL;
	newnode->val = x;
	if (pq->qtail == NULL)
	{
		pq->qhead = pq->qtail = newnode;
	}
	else
	{
		pq->qtail->next = newnode;
		pq->qtail = newnode;
	}
	pq->size++;
}

void QPop(Queue* pq)//出队列
{
	assert(pq);
	assert(pq->qhead);

	if (pq->qhead == pq->qtail)
	{
		free(pq->qhead);
		pq->qhead = pq->qtail = NULL;
		pq->size--;
	}
	else
	{
		QueueNode* next = pq->qhead->next;
		free(pq->qhead);
		pq->qhead = next;
		pq->size--;
	}
}

bool QEmpty(Queue* pq)//判空
{
	assert(pq);
	return pq->size == 0;
}

int QSize(Queue* pq)//队列元素个数
{
	assert(pq);

	return pq->size;
}

QDataType QueueFront(Queue* pq)//队列头元素
{
	assert(pq);
	assert(pq->qhead);

	return pq->qhead->val;
}

QDataType QueueBack(Queue* pq)//队列尾元素
{
	assert(pq);
	assert(pq->qtail);

	return pq->qtail->val;
}

test.c

int main()
{
	Queue pq;
	QInit(&pq);
	QPush(&pq, 1);
	QPush(&pq, 2);
	QPush(&pq, 3);
	QPush(&pq, 4);

	printf("%d ", QueueFront(&pq));
	QPop(&pq);
	printf("%d ", QueueFront(&pq));
	QPop(&pq);
	QPop(&pq);
	printf("%d ", QueueBack(&pq));
	QPop(&pq);
	return 0;
}

3.栈和队列的面试题

20. 有效的括号 - 力扣(LeetCode)

225. 用队列实现栈 - 力扣(LeetCode)

232. 用栈实现队列 - 力扣(LeetCode)

622. 设计循环队列 - 力扣(LeetCode)

相关推荐
猪猪虾的业余生活1 分钟前
Qt 驾校考试系统项目实现
开发语言·qt
香菇滑稽之谈3 分钟前
责任链模式的C++实现示例
开发语言·c++·设计模式·责任链模式
LiDAR点云14 分钟前
Matlab中快速查找元素索引号
数据结构·算法·matlab
风莫寻24 分钟前
【Troubleshot】Qt 长按按键 keyPressEvent keyReleaseEvent 自动重复问题
开发语言·qt
ZC·Shou25 分钟前
Rust 之一 基本环境搭建、各组件工具的文档、源码、配置
开发语言·rust·cargo·rustc·rustup·clippy·rustfmt
Hello.Reader26 分钟前
深入理解 Rust 中的模式匹配语法
开发语言·rust
最胖的小仙女31 分钟前
通过动态获取后端数据判断输入的值打小
开发语言·前端·javascript
阿波拉36 分钟前
AttributeError: module ‘backend_interagg‘ has no attribute ‘FigureCanvas’问题解决
开发语言·python
JKHaaa40 分钟前
数据结构之线性表
数据结构
臣妾写不来啊1 小时前
使用dify的api连接外部知识库,dify连接ragflow的知识库(附java代码)
java·开发语言·spring boot