【C-数据结构】顺序栈(int)

概述


栈特点

  1. 本质是操作受限的线性表,只能在一端进行插入删除操作
  2. 后进先出(LIFO)




用途

  1. 模拟递归
  2. 深度优先遍历,实现DFS
  3. 中缀表达式求值
  4. 求任意两个成员的关系(如括号匹配问题,先用栈预处理)




## 设计数据结构
typedef struct Stack {
    TYPENAME *_pdata;	//栈内元素
    int _capaticy;		//栈的上限
    int _top;		//栈顶,末尾元素的下标
}Stack_t, *pStack_t;




单向链表的相关操作

  1. 初始化
  2. 判空
  3. 销毁(销毁堆空间)
  4. 压栈
  5. 弹栈
  6. 查看栈顶




操作实现步骤


初始化(参数:栈地址,栈大小)

  1. 按照大小申请堆空间
  2. top = -1;

判空(参数:栈地址)

  1. return top == -1;

判满(参数:栈地址)

  1. return top == capaciy;

销毁(参数:栈地址)

  1. 如果堆空间存在,就释放

压栈(参数:栈地址,新元素)

  1. 如果栈满,报错返回

  2. 如果栈未满,插入栈顶后的位置

弹栈(参数:栈地址)

  1. 如果栈空,报错返回
  2. 如果栈非空,栈顶减一

查看栈顶(参数:栈地址)

  1. 如果栈空,报错返回
  2. 如果栈非空,返回栈顶元素




代码

头文件 SeqStack.h

#ifndef __SEQSTACK_H__
#define __SEQSTACK_H__

#define TYPENAME int

typedef struct Stack {
    TYPENAME *_pdata;	//栈内元素
    int _capaticy;		//栈的上限
    int _top;		//栈顶,末尾元素的下标
}Stack_t, *pStack_t;

void stack_init(pStack_t pSt, int size);
void stack_destroy(pStack_t pSt);
int stack_empty(pStack_t pSt);
int stack_full(pStack_t pSt);
int stack_push(pStack_t pSt, TYPENAME val);
int stack_pop(pStack_t pSt);

//使用前需判断有无元素
TYPENAME stack_top(pStack_t pSt);
#endif




实现文件 SeqStack.c

#include "SeqStack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void stack_init(pStack_t pSt, int size)
{
    pSt->_pdata = (TYPENAME*)calloc(size, sizeof(TYPENAME));
    pSt->_capaticy = size;
    pSt->_top = -1;
}

void stack_destroy(pStack_t pSt) 
{
    if (!stack_empty(pSt)) {
        free(pSt->_pdata);
        pSt->_pdata = NULL;
        memset(pSt, 0, sizeof(Stack_t));
    }
}

int stack_empty(pStack_t pSt)
{
    return -1 == pSt->_top;
}

int stack_full(pStack_t pSt)
{
    return pSt->_top + 1 == pSt->_capaticy;
}

int stack_push(pStack_t pSt, TYPENAME val)
{
    if (!stack_full(pSt)) {
        pSt->_pdata[++pSt->_top] = val;
        return 0;
    }

    printf("Stack is full, cannot push!\n");
    return -1;
}

int stack_pop(pStack_t pSt)
{
    if (!stack_empty(pSt)) {
        --pSt->_top;
        return 0;
    }

    printf("Stack is empty, cannot pop!\n");
    return -1;
}

//使用前需判断有无元素
TYPENAME stack_top(pStack_t pSt)
{
    return pSt->_pdata[pSt->_top];
}




测试文件 test_seqstack.c

#include "SeqStack.h"
#include <stdio.h>

void test0()
{
    Stack_t st;
    stack_init(&st, 6);
    stack_top(&st);

    printf("\n-----------test stack_push------------\n");
    stack_push(&st, 4);
    stack_push(&st, 5);
    stack_push(&st, 3);
    stack_push(&st, 9);
    stack_push(&st, 4);
    stack_push(&st, 5);
    stack_push(&st, 3);
    stack_push(&st, 9);

    if (!stack_empty(&st)) {
        printf("st.top = %d\n", stack_top(&st));
    }
    printf("st.size = %d, st.capacity = %d\n", st._top + 1, st._capaticy);

    
    printf("\n-----------test stack_push------------\n");
    stack_pop(&st);
    stack_pop(&st);
    stack_pop(&st);
    stack_pop(&st);
    stack_pop(&st);
    stack_pop(&st);
    stack_pop(&st);
    stack_pop(&st);
    stack_pop(&st);

    if (!stack_empty(&st)) {
        printf("st.top = %d\n", stack_top(&st));
    }
    printf("st.size = %d, st.capacity = %d\n", st._top + 1, st._capaticy);

    /* stack_destroy(&st); */
}

int main()
{
    test0();
    return 0;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
顺序栈是一种基于数组实现的栈,它的特点是具有随机存取的特性。顺序栈的基本运算包括进栈、出栈和查看栈顶元素。进栈操作将元素插入到栈顶,出栈操作将栈顶元素删除并返回,查看栈顶元素操作返回栈顶的元素值,但不修改栈的状态。 在C语言中,顺序栈的存储结构可以使用一个一维数组来存放栈中的元素,同时使用一个指示器top来指示栈顶的位置。在进行进栈和出栈操作时,需要更新top的值,使其指向栈顶元素。 下面是一种常见的顺序栈的定义和基本操作的示例代码: ```c // 定义栈中元素的数据类型 typedef int StackElementType; // 定义顺序栈的存储结构 #define Stack_Size 100 // 栈的最大容量 typedef struct { StackElementType elem[Stack_Size]; // 用数组存放栈中元素 int top; // 栈顶指针 } SeqStack; // 初始化顺序栈 void Init_SeqStack(SeqStack *S) { S->top = -1; // 初始时栈为空,栈顶指针置为-1 } // 进栈操作 void Push_SeqStack(SeqStack *S, StackElementType x) { if (S->top == Stack_Size - 1) { printf("栈已满,无法进栈"); return; } S->top++; // 栈顶指针加1 S->elem[S->top] = x; // 将新元素放入栈顶位置 } // 出栈操作 StackElementType Pop_SeqStack(SeqStack *S) { if (S->top == -1) { printf("栈为空,无法出栈"); return -1; // 返回一个特殊值表示出错 } StackElementType x = S->elem[S->top]; // 获取栈顶元素的值 S->top--; // 栈顶指针减1 return x; // 返回栈顶元素的值 } // 查看栈顶元素 StackElementType GetTop_SeqStack(SeqStack *S) { if (S->top == -1) { printf("栈为空"); return -1; // 返回一个特殊值表示出错 } return S->elem[S->top]; // 返回栈顶元素的值 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值