【数据结构】栈&队列

文章详细介绍了如何使用数组和链表分别模拟实现栈和队列这两种基本数据结构,包括它们的初始化、销毁、压栈/入队、出栈/出队、判空、取栈顶/队首和队尾元素等操作。栈采用数组实现,支持FILO(先进后出)操作,而队列使用链表实现,遵循FIFO(先进先出)原则。

目录

一、Stack——栈

1.1栈

1.2栈的模拟实现

1.2.1栈的创建

1.2.2栈的初始化&销毁

1.2.3压栈&出栈

1.2.4判空&取栈顶&栈的大小

1.3完整代码

二、Queue——队列

2.1队列

2.2队列模拟实现

2.2.1队列的创建

2.2.2队列的初始化&销毁

2.2.3入队列&出队列

2.2.4大小&判空&队首&队尾

2.3完整代码


一、Stack——栈

1.1栈

栈(stack)是一种线性的数据储存结构,它支持先进后出的操作方式,即First In Last Out(FILO)。

 

栈支持的基本操作有初始化、判空、压栈(push)、出栈(pop)、读取栈顶元素以及销毁等方式。

1.2栈的模拟实现

1.2.1栈的创建

和链表一样,栈也是用结构体来实现的,但是使用链式结构还是顺序表呢?如果用链的话,每次出栈都需要找到尾结点的前一个,会造成许多不必要的麻烦,当然双向链表可以解决这个问题,但是仔细想想,似乎用数组来的更方便一些。

 数组本身就更容易在尾部进行操作,我们把数据依次压入数组中,再从数组尾部输出,这不就是FILO吗?

typedef int STDataType;
typedef struct Stack
{
	STDataType* a;  //数组模拟实现
	int top;        //top为栈顶元素指向的下一个位置
	int capcacity;  //capacity表示栈的大小
}ST;

1.2.2栈的初始化&销毁

初始化一个栈,也就是要我们先开辟一块数组的空间,同时初始化栈顶元素并按大小设置capacity。而销毁一个栈也就是要释放这块数组的空间并置为空,同时将栈顶和大小回到0。

void STInit(ST* ps)
{
	assert(ps);
	ps->a = (STDataType*)malloc(sizeof(STDataType) * 4);
	if(ps->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	ps->top = 0;
	ps->capcacity = 4;
}

void STDestory(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = 0;
	ps->capcacity = 0;
}

1.2.3压栈&出栈

由于栈是由数组模拟实现的,入栈和出栈其实也就是对数组进行插入删除,但要特别注意的是,top指向的是栈顶元素的下一个位置。

void STPush(ST* ps, STDataType x)
{
	assert(ps);
    //先判断是否需要扩容
	if (ps->capcacity == ps->top)//满栈时top与数组大小相同
	{
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * ps->capcacity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->a = tmp;
		ps->capcacity *= 2;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

void STPop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));
	ps->top--;
}

1.2.4判空&取栈顶&栈的大小

这个很简单,我们已经在结构体里设置了top元素,因此栈的大小可以由top得出,而top是否为0也决定的栈是否为空。

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

bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

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

1.3完整代码

//Stack.h

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

typedef int STDataType;
typedef struct Stack
{
	STDataType* a;  //数组模拟实现
	int top;        //top为栈顶元素指向的下一个位置
	int capcacity;
}ST;

void STInit(ST* ps);
void STDestory(ST* ps);
void STPush(ST* ps, STDataType x);
void STPop(ST* ps);
int STSize(ST* ps);
bool STEmpty(ST* ps);
STDataType STTop(ST* ps);
//Stack.c

#include "Stack.h"

void STInit(ST* ps)
{
	assert(ps);
	ps->a = (STDataType*)malloc(sizeof(STDataType) * 4);
	if(ps->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	ps->top = 0;
	ps->capcacity = 4;
}

void STDestory(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = 0;
	ps->capcacity = 0;
}

void STPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->capcacity == ps->top)
	{
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * ps->capcacity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->a = tmp;
		ps->capcacity *= 2;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

void STPop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));
	ps->top--;
}

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

bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

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

二、Queue——队列

2.1队列

队列与栈相似,也是一种线性的数据储存结构。只不过队列支持的是先进先出操作方式,即First In First Out(FIFO)。

 队列支持的操作与栈相同,但是由于队列的结构不同,队列还支持取尾元素。

2.2队列模拟实现

2.2.1队列的创建

队列与栈的逻辑结构是相似的,那是否我们也可以像栈那样使用数组来实现队列呢?队列与栈不一样的地方在于队列支持的是FIFO操作,因此我们要不断变化队首的元素,如此一来,用数组实现的话,每次操作都会调动整个数组,严重影响效率,但是链表可以支持头插头删,这样一来,FIFO的问题便可迎刃而解。

typedef int QDataType;
//嵌套结构体
typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType data;
}QNode;

typedef struct QNode
{
	QNode* head;
	QNode* tail;
	int size;
}Queue;

2.2.2队列的初始化&销毁

由于队列是由链表实现的,而链表的操作在上一篇博客中有所讲解,因此在这便不做过多介绍

void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void QueueDestroy(Queue* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
	pq->size = 0;
}

2.2.3入队列&出队列

void QueuePush(Queue* pq, QDataType x)
{
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->data = x;
	newnode -> next = NULL;
	if (pq->head == NULL)
	{
		assert(pq->tail == NULL);//是否为空
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;//换尾
	}
	pq->size++;
}

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->head != NULL);

	if (pq->head->next == NULL)//只有一个元素
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
	pq->size--;
}

2.2.4大小&判空&队首&队尾

int QueueSize(Queue* pq)
{
	assert(pq);
	return pq->size;
}

bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->size == 0;
}

QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->head->data;
}

QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->tail->data;
}

2.3完整代码

//Queue.h

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

typedef int QDataType;
//嵌套结构体
typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType data;
}QNode;

typedef struct QNode
{
	QNode* head;
	QNode* tail;
	int size;
}Queue;

void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
int QueueSize(Queue* pq);
bool QueueEmpty(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
//Queue.c

#include "Queue.h"

void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void QueueDestroy(Queue* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void QueuePush(Queue* pq, QDataType x)
{
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->data = x;
	newnode -> next = NULL;
	if (pq->head == NULL)
	{
		assert(pq->tail == NULL);//是否为空
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;//换尾
	}
	pq->size++;
}

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->head != NULL);

	if (pq->head->next == NULL)//只有一个元素
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
	pq->size--;
}

int QueueSize(Queue* pq)
{
	assert(pq);
	return pq->size;
}

bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->size == 0;
}

QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->head->data;
}

QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->tail->data;
}

那么最后我们再来简单测试一下栈和队列的代码。

//Test.c

#include "Stack.h"
#include "Queue.h"

void TestStack()
{
	ST stack;
	STInit(&stack);
	STPush(&stack, 1);
	STPush(&stack, 2);
	STPush(&stack, 3);
	STPush(&stack, 4);
	STPush(&stack, 5);
	STPush(&stack, 6);
	STPush(&stack, 7);
	printf("Top: %d \n", STTop(&stack));
	printf("Size: %d \n", STSize(&stack));
	STPop(&stack);
	STPop(&stack);

	printf("Top: %d \n", STTop(&stack));
	printf("Size: %d \n", STSize(&stack));
}

void TestQueue()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);
	QueuePush(&q, 5);
	QueuePush(&q, 6);
	QueuePush(&q, 7);
	printf("Front: %d \n", QueueFront(&q));
	printf("Back: %d \n", QueueBack(&q));
	printf("SIZE: %d \n", QueueSize(&q));
	QueuePop(&q);
	QueuePop(&q);
	QueuePop(&q);
	printf("Front: %d \n", QueueFront(&q));
	printf("Back: %d \n", QueueBack(&q));
	printf("SIZE: %d \n", QueueSize(&q));

}

int main()
{
	printf("Stack:\n");
	TestStack();
	printf("\n");
	printf("Queue:\n");
	TestQueue();
	return 0;
}

运行结果如下:

 


栈和队列的实现到此结束,接下来会更新栈和队列的一些应用、函数栈帧的创建与销毁、堆、二叉树、八大排序。

越努力,越幸运,加油!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hehelm

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

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

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

打赏作者

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

抵扣说明:

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

余额充值