【DS】栈@栈和队列

简介:

@TOC

栈和队列的基本操作过后,依旧会更新一波题解,希望假期能把数据结构更完。

正文开始@边通书

1. 栈的概念及结构

1.1 栈的概念

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

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

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

查看源图像

1.2 栈的结构

栈的实现一般可以使用数组(数组栈)或者链表(链表栈)实现。理论上两者都可以,但相对而言数组栈实现更优一些。因为数组栈尾插尾删效率更高,且缓存利用率高。

单链表,更适合头插头删,如果用头作栈顶,就可以设计成单链表,可以但是不是有点怪。用尾作栈底,要设计成双向链表,否则增删数据效率低。

现在我们来实现动态数组栈。

typedef struct Stack
{
    STDataType* a;
    int top;//栈顶位置
    int capacity;
}ST;

2. 栈的实现

依旧是头文件文末贴出来,大家自己写嗷,顺序表写过了,这就很简单!在测试文件处有两处小注意,附在后边了。

2.1 初始化和销毁

2.1.1 初始化

  • [x] 初始化top给的是0,top指向栈顶数据的下一个

<img src=" title="">

  • [ ] 初始化top给的是-1,top指向栈顶数据

<img src=" title="">

我们采取第一种初始化方式,这是有理由的。

void StackInit(ST* ps)
{
    assert(ps);
    ps->a = NULL;
    ps->top = 0;
    //初始化时,top给-1,意味着top指向栈顶数据:先+1,再放数据
    //初始化时,top给0,意味着top指向栈顶数据的下一个:先放数据,再++
    ps->capacity = 0;
}

2.1.2 销毁

void StackDestroy(ST* ps)
{
    assert(ps);
    free(ps->a);
    ps->a = NULL;//有必要,不然就是野指针了
    ps->top = 0;
    ps->capacity = 0;
}

2.2 获取栈中有效元素个数、判断栈空

2.2.1 获取栈中有效元素个数

  • [ ] ps->top恰好就是栈中元素个数。

<img src=" title="">

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

2.2.2 判断栈空

  • [ ] 逻辑真,即为空栈
bool StackEmpty(ST* ps)
{
    assert(ps);
    return ps->top == 0;
}

2.3 压栈、出栈

2.3.1 压栈

<img src=" title="">

ps->top == ps->capacity时,就申请空间。

void StackPush(ST* ps, STDataType x)
{
    assert(ps);
    if (ps->top == ps->capacity)
    {
        int newcapacity = (ps->capacity == 0) ? 4 : 2 * ps->capacity;
        STDataType* ptr = (STDataType*)realloc(ps->a, newcapacity *sizeof(STDataType));
        if (ptr == NULL)
        {
            printf("realloc failed\n");
            exit(-1);
        }
        ps->a = ptr;
        ps->capacity = newcapacity;
    }
    ps->a[ps->top] = x;
    ps->top++;
}

2.3.2 出栈

void StackPop(ST* ps)
{
    assert(ps);
    assert(!StackEmpty(ps));//断言,栈不为空--这样调用函数不会受到你实现方式的影响
    ps->top--;
}

2.4 取栈顶元素

  • [ ] 注意,在外层调用时,出栈后,别忘记弹栈。这样才好取下一个。
STDataType StackTop(ST* ps)
{
    assert(ps);
    assert(!StackEmpty(ps));
    return ps->a[ps->top - 1]; // 注意是top-1
}

Stack.h

#pragma once

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.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 x);
//出栈
void StackPop(ST* ps);
//取栈顶数据
STDataType StackTop(ST* ps);
//栈中数据多少?
int StackSize(ST* ps);
//判断栈是否为空
bool StackEmpty(ST* ps);

Stack.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"Stack.h"

void StackInit(ST* ps)
{
    assert(ps);
    ps->a = NULL;
    ps->top = 0;
    //初始化时,top给-1,意味着top指向栈顶数据:先+1,再放数据
    //初始化时,top给0,意味着top指向栈顶数据的下一个:先放数据,再++
    ps->capacity = 0;
}

void StackDestroy(ST* ps)
{
    assert(ps);
    free(ps->a);
    ps->a = NULL;//有必要,不然就是野指针了
    ps->top = 0;
    ps->capacity = 0;
}

void StackPush(ST* ps, STDataType x)
{
    assert(ps);
    if (ps->top == ps->capacity)
    {
        int newcapacity = (ps->capacity == 0) ? 4 : 2 * ps->capacity;
        STDataType* ptr = (STDataType*)realloc(ps->a, newcapacity *sizeof(STDataType));
        if (ptr == NULL)
        {
            printf("realloc failed\n");
            exit(-1);
        }
        ps->a = ptr;
        ps->capacity = newcapacity;
    }
    ps->a[ps->top] = x;
    ps->top++;
}

void StackPop(ST* ps)
{
    assert(ps);
    assert(!StackEmpty(ps));//断言,栈不为空--这样调用函数不会受到你实现方式的影响
    ps->top--;
}

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

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

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

test.c

注意:

  • [ ] 取栈顶元素制后,别忘记弹栈
  • [ ] 遍历栈时,要符合栈的性质,只能取完栈顶数据才能取下一个
#define _CRT_SECURE_NO_WARNINGS 1

#include"Stack.h"

//测试压栈,出栈
void testStack1()
{
    ST st;
    StackInit(&st);
    StackPush(&st, 1);
    StackPush(&st, 2);
    StackPush(&st, 3);
    StackPush(&st, 4);
    StackPush(&st, 5);

    StackPop(&st);
    /*StackPop(&st);
    StackPop(&st);
    StackPop(&st);
    StackPop(&st);
    StackPop(&st);*/

    StackDestroy(&st);
}

void testStack2()
{
    ST st;
    StackInit(&st);
    StackPush(&st, 1);
    StackPush(&st, 2);
    StackPush(&st, 3);
    StackPush(&st, 4);
    StackPush(&st, 5);
    StackPush(&st, 6);
    StackPush(&st, 7);

    printf("top:%d\n", StackTop(&st));
    StackPop(&st);

    printf("top:%d\n", StackTop(&st));
    StackPop(&st);

    printf("size:%d\n", StackSize(&st));

    //由于栈的性质,遍历栈--只能取完栈顶数据才能取下一个
    while (!StackEmpty(&st))
    {
        printf("%d ", StackTop(&st));
        StackPop(&st);
    }
    StackDestroy(&st);
}

int main()
{
    //testStack1();
    testStack2();
    return 0;
}
相关文章
|
5天前
|
算法 C语言
【数据结构与算法 经典例题】使用栈实现队列(图文详解)
【数据结构与算法 经典例题】使用栈实现队列(图文详解)
|
5天前
|
算法 C语言
【数据结构与算法 经典例题】使用队列实现栈(图文详解)
【数据结构与算法 经典例题】使用队列实现栈(图文详解)
|
2天前
|
存储 算法 调度
算法与数据结构-栈篇
算法与数据结构-栈篇
11 0
|
3天前
|
存储 人工智能 程序员
技术心得记录:堆(heap)与栈(stack)的区别
技术心得记录:堆(heap)与栈(stack)的区别
|
5天前
【海贼王的数据航海】栈和队列
【海贼王的数据航海】栈和队列
5 0
|
6天前
|
存储 测试技术
【数据结构】操作受限的线性表,栈的具体实现
【数据结构】操作受限的线性表,栈的具体实现
17 5
|
6天前
|
算法
【C/数据结构和算法】:栈和队列
【C/数据结构和算法】:栈和队列
15 1
|
10天前
|
C++
【洛谷 P1044】[NOIP2003 普及组] 栈 题解(递归+记忆化搜索)
**NOIP2003普及组栈问题**:给定操作数序列1到n,仅允许push(进栈)和pop(出栈)操作。目标是计算所有可能的输出序列总数。输入包含一个整数n(1≤n≤18)。示例输入3,输出5。当队列空时返回1,栈空则只能入栈,栈非空时可入栈或出栈。AC C++代码利用记忆化搜索求解。
9 1
|
13天前
|
算法
$停车场管理系统 栈与队列
$停车场管理系统 栈与队列
10 1
|
5天前
|
存储 算法 编译器
【数据结构与算法】使用数组实现栈:原理、步骤与应用
【数据结构与算法】使用数组实现栈:原理、步骤与应用