探索顺序结构:栈的实现方式

简介: 探索顺序结构:栈的实现方式

一、栈的定义

栈(Stack)是一种常见的数据结构,它是一种“后进先出”(Last In First Out,LIFO)的数据结构。栈可以看做是一种特殊的线性表,只能在栈顶进行插入和删除操作。栈顶是允许操作的,而栈底是固定的。

二、栈的分类

当我们了解栈的定义之后,我们就能大概知晓其实现方式无非就是顺序表或者单链表。根据其实现方式,我们又能将栈分为顺序栈链式栈。



因为单链表头插的效率O(1)明显比尾差O(N)更高,所以我们用单链表实现栈时最好以链表的头为栈顶。如果一定要以尾节点作为栈顶的话,最好以双向链表来实现。本章实现链表栈时以头节点作为栈顶。


三、栈的声明

3.1 顺序栈

顺序栈的声明需要一个指向一块空间的指针a,指向栈顶下一个元素的top,以及标志栈大小的capacity。

typedef int STDataType;
 
typedef struct Stack
{
  STDataType* a;//动态数组
  int top;//栈顶的后一个
  int capacity;//数组大小
}ST;


当然也有实现top指向当前栈顶元素的,只不过这时top初始化要为-1,这样才能在填入元素时刚好指向栈顶元素。

3.2 链式栈

链式栈的声明只需要一个top指针,以及栈的容量capacity。

typedef struct SListNode
{
  STDataType data;
  struct SListNode* next;
}SListNode;
 
typedef struct Stack
{
  SListNode* top;
  int size;
}Stack;


四、栈的操作实现

顺序栈与链式栈的初始化分别与顺序表,链表的初始化大致相同。顺序栈先预先分配一块空间,而链式栈可以先初始为NULL,我们本章节就以顺序栈为例来进行讲解

4.1 栈的初始化

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


4.2 销毁栈

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

4.3 入栈

void StackPush(ST* ps, STDataType x)
{
  assert(ps);
 
  if (ps->top = ps->capacity)
  {
    STDataType newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
    STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType));
    if (tmp == NULL)
    {
      perror("realloc fail");
    }
    ps->a = tmp;
    ps->capacity = newcapacity;
  }
 
  ps->a[ps->top] = x;
  ps->top++;
}


4.4 出栈

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

4.5 获取栈顶元素

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

4.6 获取栈中有效元素个数

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

4.7 检测栈是否为空

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



注意:虽然出栈等操作代码简单,但也需要严格使用函数接口,尽可能避免自己写代码,否则容易造成结构混乱。

五、完整代码

5.1 Stack.h

#include<iostream>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
 
typedef int STDataType;
 
typedef struct Stack
{
  STDataType* a;//动态数组
  int top;//栈顶的后一个
  int capacity;//数组大小
}ST;
 
// 初始化栈 
void StackInit(ST* ps);
// 销毁栈 
void StackDestroy(ST* ps);
// 入栈 
void StackPush(ST* ps, STDataType data);
// 出栈 
void StackPop(ST* ps);
// 获取栈顶元素 
STDataType StackTop(ST* ps);
// 获取栈中有效元素个数 
int StackSize(ST* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
bool StackEmpty(ST* ps);


5.2 Stack.c

#include"Stack.h"
 
void StackInit(ST* ps)
{
  assert(ps);
  ps->a = NULL;
  ps->top = 0;
  ps->capacity = 0;
}
 
void StackDestroy(ST* ps)
{
  assert(ps);
  free(ps->a);
  ps->a = NULL;
  ps->top = ps->capacity = 0;
}
 
void StackPush(ST* ps, STDataType x)
{
  assert(ps);
 
  if (ps->top = ps->capacity)
  {
    STDataType newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
    STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType));
    if (tmp == NULL)
    {
      perror("realloc fail");
    }
    ps->a = tmp;
    ps->capacity = newcapacity;
  }
 
  ps->a[ps->top] = x;
  ps->top++;
}
 
void StackPop(ST* ps)
{
  assert(ps);
  assert(ps->top > 0);
 
  ps->top--;
}
 
STDataType StackTop(ST* ps)
{
  assert(ps);
  assert(ps->top > 0);
 
  return ps->a[ps->top - 1];
}
 
int StackSize(ST* ps)
{
  assert(ps);
 
  return ps->top;
}
 
bool StackEmpty(ST* ps)
{
   assert(ps);
 
   return ps->top == 0;
}


相关文章
|
2天前
|
算法 程序员 索引
数据结构与算法学习七:栈、数组模拟栈、单链表模拟栈、栈应用实例 实现 综合计算器
栈的基本概念、应用场景以及如何使用数组和单链表模拟栈,并展示了如何利用栈和中缀表达式实现一个综合计算器。
数据结构与算法学习七:栈、数组模拟栈、单链表模拟栈、栈应用实例 实现 综合计算器
|
2天前
初步认识栈和队列
初步认识栈和队列
20 10
|
2天前
|
算法
数据结构与算法二:栈、前缀、中缀、后缀表达式、中缀表达式转换为后缀表达式
这篇文章讲解了栈的基本概念及其应用,并详细介绍了中缀表达式转换为后缀表达式的算法和实现步骤。
14 3
|
21小时前
|
存储 JavaScript 前端开发
为什么基础数据类型存放在栈中,而引用数据类型存放在堆中?
为什么基础数据类型存放在栈中,而引用数据类型存放在堆中?
12 1
|
3天前
|
存储 安全 Java
【用Java学习数据结构系列】探索栈和队列的无尽秘密
【用Java学习数据结构系列】探索栈和队列的无尽秘密
15 2
|
2天前
|
存储 C语言
栈和队列题目练习
栈和队列题目练习
5 0
|
3天前
|
C语言
数据结构------栈(Stack)和队列(Queue)
数据结构------栈(Stack)和队列(Queue)
12 0
|
4天前
|
编译器 Linux C语言
第五章 栈与队列
第五章 栈与队列
12 0
|
4天前
02(数据结构考研)栈相关操作代码
02(数据结构考研)栈相关操作代码
8 0
|
9天前
|
存储 算法 搜索推荐
探索常见数据结构:数组、链表、栈、队列、树和图
探索常见数据结构:数组、链表、栈、队列、树和图
82 64