栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构——栈

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构——栈

目录
  • 栈
  • 栈的实现
    • 栈的创建
    • 栈的初始化
    • 栈的销毁
    • 栈的插入
    • 栈的删除
    • 栈顶数据的取出
    • 栈中数据个数
    • 栈的打印(bushi)
    • 完整代码
  • 出栈的顺序
  • 练习

栈

栈:一种特殊的线性表,其只允许固定的一端进行插入和删除元素的操作。进行数据的插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的元素遵循后进先出(LIFO(last in first out))的原则
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶
出栈:栈的删除操作叫做出栈。出数据也在栈顶

栈的实现

栈的实现一般可以使用数组或者链表实现。相较于链表,数组的结构实现更优一些。数组尾插尾删的效率非常高,同时缓存利用率也很高。

栈的创建

静态的栈:

#define N 10
typedef int STDataType;
typedef struct Stack
{
	STDataType a[N];
	int top;//标识栈顶的位置
}Stack;

动态的栈:

typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}Stack;

动态的栈的优势相较于静态的栈是不言而喻的,这里自然是选用动态的栈

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

上述的top所标识的是最后一个数据的下一个位置。当然top也可以标识最后一个数据的位置,由于插入后top要++,所以初始化时得赋-1;

这里top的两种标识都可以,我选用的是前者

栈的销毁 栈的插入

栈的性质已经规定死了,只能在栈顶插入

void StackPush(Stack* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		//如果栈中没有数据,那么给它四个数据的空间;如果有,那么在原有的基础上扩大二倍
		int  newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		//用temp指向扩容后的空间,回头再把它赋给ps->a
		STDataType* temp = realloc(ps->a, sizeof(STDataType) * newCapacity);
		if (temp == NULL)//realloc可能会失败,失败会返回空指针
		{
			printf("realloc failn");
			exit(-1); 
		}
		ps->a = temp;
		ps->capacity = newCapacity;
	}
	
	ps->a[ps->top] = x;
	ps->top++;
}
栈的删除

栈的性质已经规定了,只能在栈顶删除

top–后,栈顶的元素删不删都可以。显然的,如果栈为空还进行删除很不合理,鉴于后续还可能要判断栈为不为空,所以将其封装成一个函数(StackEmpty)然后调用。

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

判空:

bool StackEmpty(Stack* ps)
{
	assert(ps);
	if (ps->top > 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

也可以直接用一个表达式来判断:

bool StackEmpty(Stack* ps)
{
	assert(ps);
	return ps->top == 0;
}
栈顶数据的取出

同样的,如果栈为空,依旧进行栈顶数据的取出是不合理的,所以调用之前封装的StackEmpty函数进行判断

STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	//前文有提到:top所标识的是栈顶后一位,所以要-1
	return ps->a[ps->top - 1];
}
栈中数据个数
int StackSize(Stack* ps)
{
	assert(ps);
	return ps->top;
}
栈的打印(bushi)

栈是不能够直接打印的,如果能遍历就不符合栈的性质
比如栈中有1、2、3、4、5这五个数据,如果依次打印便于“先进后出”的性质相悖

虽然不能够遍历,但是可以取出栈顶数据,然后打印、删除,再取出栈顶数据,循环往复直至栈为空。

void InspectStack()
{
	Stack s;
	StackInit(&s);
	StackPush(&s, 1);
	StackPush(&s, 2);
	StackPush(&s, 3);
	StackPush(&s, 4);
	StackPush(&s, 5);
	while (!StackEmpty(&s))
	{
		printf("%d ", StackTop(&s));
		StackPop(&s);
	}
	printf("Stack is NULLn");
	StackDestroy(&s);
}
完整代码

Stack.h:

#pragma once
#include
#include
#include
#include

typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;//top所标识的是最后一个数据的下一个位置
	int capacity;
}Stack;

void StackInit(Stack* ps);
void StackDestroy(Stack* ps);
void StackPush(Stack* ps, STDataType x);
void StackPop(Stack* ps);
void Stackp(Stack* ps);
STDataType StackTop(Stack* ps);
bool StackEmpty(Stack* ps);
int StackSize(Stack* ps);

Stack.c:

#include"Stack.h"

void StackInit(Stack* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = 0;
	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* temp = realloc(ps->a, sizeof(STDataType) * newCapacity);//用temp指向扩容后的空间,回头再把它赋给ps->a
		if (temp == NULL)//realloc可能会失败,失败会返回空指针
		{
			printf("realloc failn");
			exit(-1); 
		}
		ps->a = temp;
		ps->capacity = newCapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

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

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

bool StackEmpty(Stack* ps)
{
	assert(ps);
	//if (ps->top > 0)
	//{
	//	return false;
	//}
	//else
	//{
	//	return true;
	//}
	return ps->top == 0;
}

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

test.c:

#include"Stack.h"

void InspectStack()
{
	Stack s;
	StackInit(&s);
	StackPush(&s, 1);
	StackPush(&s, 2);
	StackPush(&s, 3);
	StackPush(&s, 4);
	StackPush(&s, 5);
	while (!StackEmpty(&s))
	{
		printf("%d ", StackTop(&s));
		StackPop(&s);
	}
	printf("Stack is NULLn");
	StackDestroy(&s);
}

int main()
{
	InspectStack();
	return 0;
}
出栈的顺序

栈的“先进后出”是相对于同时在栈里面的数据,一个入栈顺序可能面对多个出栈顺序

比如将元素1、2、3、4、5、6这六个元素放入栈中,那么出栈的顺序可以有654321、365421、356421

练习

有效的括号: 链接

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/947977.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号