DS:顺序栈的实现

                                                    创作不易,友友们给个三连吧!!

一、栈的概念及结构

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

二、顺序栈的实现

数组实现栈:

首元素当栈低,栈顶是数组的尾元素,压栈就是尾插,出栈就是尾删

链表实现栈:

链表的最后一个结点当栈底,栈顶是链表的头结点,压栈就是头插,出栈就是头删

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

由于这些操作和顺序表的实现基本上是一样的,所以以下的介绍不做详细讲解。

建议大家看看博主关于顺序表的实现,再来看下面代码就易如反掌了!!

DS:顺序表的实现-CSDN博客

2.1 栈相关结构体

下面是定长的静态栈的结构,实际中一般不实用,因为设置得太小容易不够,设置得太大容易浪费

typedef int STDataType;
#define N 10
typedef struct Stack
{
 STDataType _a[N];
 int _top; // 栈顶
}Stack;

以我们主要实现下面的支持动态增长的栈

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

2.2 初始化栈

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

     ps->top并不指向栈顶元素,而是指向栈顶元素的下一个位置,如果想要指向栈顶元素,则需要给top赋值-1.但是给top赋值0也有好处,就是top的值就相当于是顺序表中的size,即表示栈中的有效数据个数

2.3 压栈

void StackPush(Stack* ps, STDataType x)
{
	assert(ps);
	//判断是否需要扩容
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* temp = (STDataType*)realloc(ps->a,sizeof(STDataType) * newcapacity);
		if (temp == NULL)
		{
			perror("realloc fail");
			exit(1);
		}
		ps->a = temp;
		ps->capacity = newcapacity;
	}
	//压栈
	ps->a[ps->top++] = x;
}

2.4 出栈

void StackPop(Stack* ps)
{
	assert(ps);
	//如果栈为空,则没有删除的必要
	assert(!StackEmpty(ps));
	ps->top--;
}

2.5 获取栈顶元素

STDataType StackTop(Stack* ps)
{
	assert(ps);
	//如果栈为空,不可能有栈顶元素
	assert(!StackEmpty(ps));
	return ps->a[ps->top - 1];
}

要注意:因为我们初始化的top是0,所以top指向的是栈顶元素的下一个位置! 

2.6 获取栈中有效元素个数

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

因为top初始赋值为0, 所以top其实就相当于栈中的有效数据个数,专门封装一个函数只是想提高可读性!

2.7 检测栈是否为空

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

      在顺序表中,是否为空只需要看有效容量个数是不是0即可,但是在顺序栈中有效数据个数size被替换成了 top,虽然我们知道top和size的意思差不多,但是如果在代码里直接用的话可读性就没有size这么好,所以单独设置一个检测栈是否为空的函数。

2.8 销毁栈

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

2.9 打印栈 

     栈相比较于顺序表,并不具备随机访问的特点,因为栈是后进先出的,也就是说如果我们要遍历栈去访问栈中的每个元素,那么就需要一边获取栈顶元素一边出栈,这其实就会破坏原先栈的结构了,一般只能使用一次,不具备复用性,因此没必要单独封装一个函数。如果实在想打印栈,那么就在main函数中这样测试一下

#include"Stack.h"

int main()
{
	Stack sk;
	StackInit(&sk);
	StackPush(&sk, 1);
	StackPush(&sk, 2);
	StackPush(&sk, 3);
	StackPush(&sk, 4);
	while (!StackEmpty(&sk))
	{
		printf("%d ", StackTop(&sk));//一边打印栈顶元素
		StackPop(&sk);//一边出栈
	}
}

三、顺序栈实现的所有代码 

3.1 Stack.h

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

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

void StackInit(Stack* ps);//初始化栈
void StackPush(Stack* ps, STDataType x);//入栈
void StackPop(Stack* ps);//出栈
STDataType StackTop(Stack* ps);//获取栈顶元素
int StackSize(Stack* ps);//获取栈中有效元素个数
bool StackEmpty(Stack* ps);//检测栈是否为空,为空返回true
void StackDestory(Stack* ps);//销毁栈

3.2 Stack.c

#include"Stack.h"

void StackInit(Stack* ps)
{
	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 : 2 * ps->capacity;
		STDataType* temp = (STDataType*)realloc(ps->a,sizeof(STDataType) * newcapacity);
		if (temp == NULL)
		{
			perror("realloc fail");
			exit(1);
		}
		ps->a = temp;
		ps->capacity = newcapacity;
	}
	//压栈
	ps->a[ps->top++] = x;
}


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];
}

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

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

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

3.3 test.c(测试)

#include"Stack.h"

int main()
{
	Stack sk;
	StackInit(&sk);
	StackPush(&sk, 1);
	StackPush(&sk, 2);
	StackPush(&sk, 3);
	StackPush(&sk, 4);
	while (!StackEmpty(&sk))
	{
		printf("%d ", StackTop(&sk));//一边打印栈顶元素
		StackPop(&sk);//一边出栈
	}
}

 四、栈的相关oj题

4.1 选择题

1、一个栈的初始状态为空。现将元素1、2、3、4、5、A、B、C、D、E依次入栈,然后再依次出栈,则元素出
栈的顺序是(B)。
A  12345ABCDE
B  EDCBA54321
C  ABCDE12345
D  54321EDCBA
解析:后进先出的特点,进栈过程中如果没有出栈,入栈和出栈的顺序是相反的。

2.若进栈序列为 1,2,3,4 ,进栈过程中可以出栈,则下列不可能的一个出栈序列是(C)
A 1,4,3,2
B 2,3,4,1
C 3,1,4,2
D 3,4,2,1
解析:虽然是后进先出,但是入栈和出栈顺序相反是相对的,重点就是要判断进栈过程中是否有出栈,题目有明确提出这一点,所以这题最好同过画图去排除可能性,比如C,3出栈说明1和2都在栈内,下一个要出栈的话只能是2不能是1,1不可能在2的前面出栈。

4.2 有效的括号(OJ题) 

有效的括号(力扣)

条件的意思是:括号可以相互包含,放不能参差摆放

思路:利用栈结构后进先出的特点,让左括号入栈,右括号出栈匹配,当左右括号相等的前提下,如果最后栈为空,则符合题目要求,左括号大于右括号和右括号大于左括号要分开讨论。

typedef char STDataType;
//支持动态增长的栈
typedef struct Stack
{
	STDataType* a;
	int top;//栈顶
	int capacity;//栈容量
}Stack;
void StackInit(Stack* ps);//初始化栈
void StackPush(Stack* ps, STDataType x);//入栈
void StackPop(Stack* ps);//出栈
STDataType StackTop(Stack* ps);//获取栈顶元素
int StackSize(Stack* ps);//获取栈中有效元素个数
bool StackEmpty(Stack* ps);//检测栈是否为空,为空返回true
void StackDestory(Stack* ps);//销毁栈
void StackInit(Stack* ps)
{
	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 : 2 * ps->capacity;
		STDataType* temp = (STDataType*)realloc(ps->a,sizeof(STDataType) * newcapacity);
		if (temp == NULL)
		{
			perror("realloc fail");
			exit(1);
		}
		ps->a = temp;
		ps->capacity = newcapacity;
	}
	//压栈
	ps->a[ps->top++] = x;
}


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];
}

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

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

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

bool isValid(char* s) 
{
    Stack st;
    StackInit(&st);
    while(*s)
    {
        if(*s=='('||*s=='{'||*s=='[')
        StackPush(&st,*s);
        else
        {
            //如果右比左多,栈为空
            if(StackEmpty(&st))
            {
                StackDestory(&st);
                return false;
            }
            char top=StackTop(&st);
            StackPop(&st);
            //不匹配
            if((*s==')'&&top!='(')||(*s=='}'&&top!='{')||(*s==']'&&top!='['))
            {
                StackDestory(&st);
                return false;
            }
        }
        ++s;
    }
    //如果左括号比右括号多,栈也有元素
    bool ret=StackEmpty(&st);
    StackDestory(&st);
    return ret;
}

要注意中间的条件判断!! 

  • 81
    点赞
  • 82
    收藏
    觉得还不错? 一键收藏
  • 65
    评论
评论 65
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值