【初阶数据结构】- 栈和队列

1.💞栈

1.1📌栈的概念及其结构

栈是一种线性表,只允许从固定的一端进行数据的插入删除,进行数据插入删除的一端叫做栈顶,另一端则叫栈底。
🍃压栈: 栈的插入操作叫做压栈/入栈/进栈。压栈在栈顶
🍃出栈:栈的删除数据叫做出栈。出栈也在栈顶

🔖栈符合后进先出(Last In First Out) 的原则

在这里插入图片描述
🍒 将栈看作一个杯子,压栈相当于往杯子中倒水,出栈相当于将杯子中的水倒出,那么先倒入杯中的水肯定会最后倒出,后倒入杯中的水最先倒出。

1.2 📌栈的实现

栈的实现可以用数组实现 ,也可以用链表实现。数组相对于链表实现会容易一些,同样的代价也要少一点。

🎈今天我们就来通过数组来实现栈

1.2.1🐾Stack.h
#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 StackDestroy(Stack* ps);
//压栈
void StackPush(Stack* ps, STDataType x);
//出栈
void StackPop(Stack* ps);
//获取栈顶元素
STDataType StackTop(Stack* ps);
//获取栈中有效元素的个数
int StackSize(Stack* ps);
//判断栈是否为空
bool StackEmpty(Stack* ps);
1.2.2🐾Stack.c
#include"Stack.h"


//栈的初始化
void StackInit(Stack* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}
//栈的销毁
void StackDestroy(Stack* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}
//压栈
void StackPush(Stack* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top++] = x;
}
//出栈
void StackPop(Stack* ps)
{
	assert(ps);
	assert(ps->top > 0);
	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;
}

2. 💞队列

2.1 📌队列的概念及其结构

队列也是一种线性表,队列是一端进行添加数据,另一端进行删除数据的特殊线性表。
🍃入队列: 进行数据的插入,插入的一端称为队头
🍃出队列: 进行数据的删除,删除的一端称为队尾

🔖队列符合先进先出(First In First Out) 的原则

在这里插入图片描述

2.2 📌队列的实现

队列也可以用数组或者链表实现,使用链表实现会比数组更优一些,因为如果使用数组,出队列在数组的头部操作,会比较麻烦,从而使效率下降。

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

typedef int QDataType;
//链式结构
typedef struct QNode
{
	struct QNode* next;
	QDataType a;

}QNode;

typedef struct Queue
{
	QNode* head;
	QNode* tail;
	int size;  //记录队列中元素的个数
}Queue;
//队列的初始化
void QueueInit(Queue* ps);
//队列的销毁
void QueueDestroy(Queue* ps);
//队尾入数据
void QueuePush(Queue* ps,QDataType x);
//队头出数据
void QueuePop(Queue* ps);
//获取队头数据
QDataType QueueFront(Queue* ps);
//获取队尾数据
QDataType QueueBack(Queue* ps);
//获取队列中有效元素的个数
int QueueSize(Queue* ps);
//检测队列是否为空
bool QueueEmpty(Queue* ps);
2.2.2 🐾Queue.c
#include"Queue.h"
//队列的初始化
void QueueInit(Queue* ps)
{
	assert(ps);
	ps->head = ps->tail = NULL;
	ps->size = 0;
}
//队列的销毁
void QueueDestroy(Queue* ps)
{
	assert(ps);
	while (ps->head)
	{
		QNode* next = ps->head->next;
		free(ps->head);
		ps->head = next;
	}
	ps->tail = NULL;
}
//队尾入数据
void QueuePush(Queue* ps,QDataType x)
{
	assert(ps);
	//动态内存申请
	QNode* tmp = (QNode*)malloc(sizeof(QNode));
	if (tmp == NULL)
	{
		perror("malloc fail");
		return;
	}
	//一个结点
	if (ps->head==NULL)
	{
		ps->head = ps->tail = tmp;
		ps->head->a = x;
	}
	//多个结点
	else 
	{ 
		ps->tail->next = tmp;
		ps->tail = ps->tail->next;
		ps->tail->a = x;
	}
	ps->size++;
}
//队头出数据
void QueuePop(Queue* ps)
{
	assert(ps);
	assert(ps->head);
	QNode* next = ps->head->next;
	if (ps->head == ps->tail)
	{
		free(ps->head);
		ps->head=ps->tail=NULL;
	}
	else
	{
		free(ps->head);
		ps->head = next;
	}
	ps->size--;
}
//获取队头数据
QDataType QueueFront(Queue* ps)
{
	assert(ps);

	return ps->head->a;
}
//获取队尾数据
QDataType QueueBack(Queue* ps)
{
	assert(ps);

	return ps->tail->a;

}
//获取队列中有效元素的个数
int QueueSize(Queue* ps)
{
	assert(ps);
	assert(ps->size>=0);

	return ps->size;
}
//检测队列是否为空
bool QueueEmpty(Queue* ps)
{
	assert(ps);

	return ps->size == 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值