数据结构——栈

简介: 介绍数据结构的——栈,及OJ题


栈的概念及结构

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

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

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

理解了栈的概念及其结构,我们可以连做一些比较常见的选择题:

1.一个栈的初始状态为空。现将元素1、2、3、4、5、A、B、C、D、E依次入栈,然后再依次出栈,则元素出

栈的顺序是( )。

A 12345ABCDE

B EDCBA54321

C ABCDE12345

D 54321EDCBA

解析:非常简单,根据后进先出,选B

2.若进栈序列为 1,2,3,4 ,进栈过程中可以出栈,则下列不可能的一个出栈序列是()

A 1,4,3,2

B 2,3,4,1

C 3,1,4,2

D 3,4,2,1

解析:学校的考试选择题最喜欢出这种了,我们可以边进边出,A、B、D是可以的。而对于C:要想出3,肯定要先入1、2、3,而要出1是不可能的,还有个2呢。


栈的实现

  • 栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。
  • 栈的作用还是挺大的:递归如果深度太深,可以利用栈来实现非递归
  • 话不多说,我们直接来进行栈的实现:数据结构这里不要直接去访问结构数据,我们最好还是通过函数接口进行访问

1.结构体的定义

栈结构体的定义与顺序表的类似,也可以分为静态栈和动态栈。不过我们需要一个指向栈顶的top。

//静态栈
//#define N 100
//typedef int STDataType;
//typedef struct Stack
//{
//  STDataType a[N];
//  int top;
//}ST;
//动态栈
typedef int STDataType;
typedef struct Stack
{
  STDataType*a;
  int top;
  int capacity;
}ST;

2.初始化

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

3.销毁

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

4.插入

对于栈的插入操作,我们需要知道top的初始位置是在哪里,是-1呢还是0呢?

很明显,这里我们在初始化的时候设置成0了。同时,插入的时候我们需要去考虑有必要扩容的问题。

void StackPush(ST* 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 (NULL == tmp)
    {
      perror("malloc fail");
      exit(-1);
    }
    ps->a = tmp;
    ps->capacity = newCapacity;
  }
  ps->a[ps->top] = x;
  ps->top++;
}

5.判断是否为空

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

6.删除

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

7.取栈顶元素

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

8.元素个数

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

完整代码

Stack.h

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
//静态栈
//#define N 100
//typedef int STDataType;
//typedef struct Stack
//{
//  STDataType a[N];
//  int top;
//}ST;
//动态栈
typedef int STDataType;
typedef struct Stack
{
  STDataType*a;
  int top;
  int capacity;
}ST;
void StackInit(ST* ps);
void StackDestory(ST* ps);
void StackPush(ST* ps,STDataType x);
void StackPop(ST* ps);
STDataType StackTop(ST* ps);
bool StackEmpty(ST*ps);
int StackSize(ST* ps);

Stack.c

#define _CRT_SECURE_NO_WARNINGS
#include "Stack.h"
void StackInit(ST* ps)
{
  assert(ps);
  ps->a = NULL;
  ps->capacity = ps->top = 0;
}
void StackDestory(ST* ps)
{
  assert(ps);
  free(ps->a);
  ps->a = NULL;
  ps->capacity = ps->top = 0;
}
void StackPush(ST* 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 (NULL == tmp)
    {
      perror("malloc fail");
      exit(-1);
    }
    ps->a = tmp;
    ps->capacity = newCapacity;
  }
  ps->a[ps->top] = x;
  ps->top++;
}
void StackPop(ST* ps)
{
  assert(ps);
  assert(!StackEmpty(ps));
  ps->top--;
}
STDataType StackTop(ST* ps)
{
  assert(ps);
  assert(!StackEmpty(ps));
  return ps->a[ps->top - 1];
}
bool StackEmpty(ST* ps)
{
  assert(ps);
  return ps->top == 0;
}
int StackSize(ST* ps)
{
  assert(ps);
  return ps->top;
}

test.c

#define _CRT_SECURE_NO_WARNINGS
#include "Stack.h"
void TestStack()
{
  ST st;
  StackInit(&st);
  StackPush(&st, 1);
  StackPush(&st, 2);
  StackPush(&st, 3);
  StackPush(&st, 4);
  StackPush(&st, 5);
  //访问栈
  while (!StackEmpty(&st))
  {
    printf("%d ", StackTop(&st));
    StackPop(&st);
  }
  //把栈顶数据拿出来
  printf("\n");
}
int main()
{
  TestStack();
  return 0;
}

栈的OJ题

有效的括号

给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串 s ,判断字符串是否有效。

有效字符串需满足:

左括号必须用相同类型的右括号闭合。

左括号必须以正确的顺序闭合。

思路:数个数是不行的,我们还需要去考虑括号的顺序,我们可以利用栈来解决这道题目,利用后进先出的特性,左括号就进栈,右括号就出栈栈顶元素进行匹配比较(当然还有一些细节的东西我们需要去注意到):

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
//静态栈
//#define N 100
//typedef int STDataType;
//typedef struct Stack
//{
//  STDataType a[N];
//  int top;
//}ST;
//动态栈
typedef char STDataType;
typedef struct Stack
{
  STDataType*a;
  int top;
  int capacity;
}ST;
void StackInit(ST* ps);
void StackDestory(ST* ps);
void StackPush(ST* ps,STDataType x);
void StackPop(ST* ps);
STDataType StackTop(ST* ps);
bool StackEmpty(ST*ps);
int StackSize(ST* ps);
void StackInit(ST* ps)
{
  assert(ps);
  ps->a = NULL;
  ps->capacity = ps->top = 0;
}
void StackDestory(ST* ps)
{
  assert(ps);
  free(ps->a);
  ps->a = NULL;
  ps->capacity = ps->top = 0;
}
void StackPush(ST* 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 (NULL == tmp)
    {
      perror("malloc fail");
      exit(-1);
    }
    ps->a = tmp;
    ps->capacity = newCapacity;
  }
  ps->a[ps->top] = x;
  ps->top++;
}
void StackPop(ST* ps)
{
  assert(ps);
  assert(!StackEmpty(ps));
  ps->top--;
}
STDataType StackTop(ST* ps)
{
  assert(ps);
  assert(!StackEmpty(ps));
  return ps->a[ps->top - 1];
}
bool StackEmpty(ST* ps)
{
  assert(ps);
  return ps->top == 0;
}
int StackSize(ST* ps)
{
  assert(ps);
  return ps->top;
}
bool isValid(char * s){
    ST st;
    StackInit(&st);
    while(*s)
    {
        if(*s == '{' || *s=='[' ||*s=='(')
        {
            StackPush(&st,*s);
        }
        else
        {
            //可能只有右括号,而栈为空,数量不匹配
            if(StackEmpty(&st))
              return false;
            char top = StackTop(&st);
            StackPop(&st);
            if((*s == '}'&&top != '{')
            ||(*s == ']'&&top != '[')
            ||(*s == ')'&&top != '('))
            {
                return false;
            }
        }
        ++s;
    }
    //栈不为空,数量不匹配
    bool flag = StackEmpty(&st);
    StackDestory(&st);
    return flag;
}



相关文章
|
4天前
|
机器学习/深度学习 算法 测试技术
【单调栈】3113. 边界元素是最大值的子数组数目
【单调栈】3113. 边界元素是最大值的子数组数目
|
3天前
|
前端开发 JavaScript 算法
JavaScript 中实现常见数据结构:栈、队列与树
JavaScript 中实现常见数据结构:栈、队列与树
|
4天前
|
存储 NoSQL C语言
数据结构——顺序栈与链式栈的实现-2
数据结构——顺序栈与链式栈的实现
数据结构——顺序栈与链式栈的实现-2
|
4天前
|
存储 C语言
数据结构——顺序栈与链式栈的实现-1
数据结构——顺序栈与链式栈的实现
数据结构——顺序栈与链式栈的实现-1
|
4天前
栈的基本应用
栈的基本应用
13 3
|
4天前
栈与队列理解
栈与队列理解
13 1
|
4天前
|
存储 算法
数据结构与算法 栈与队列
数据结构与算法 栈与队列
12 0
数据结构与算法 栈与队列
|
4天前
|
C++
数据结构(共享栈
数据结构(共享栈
9 0
|
4天前
|
C++
数据结构(顺序栈
数据结构(顺序栈
13 2
|
4天前
|
容器
【栈与队列】栈与队列的相互转换OJ题
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
11 0