链式栈操作(C语言)

链式栈

链式栈是栈的链式存储结构,它主要通过链表来实现栈的操作。在链式栈中,每个节点包含两部分:一部分是存储数据的数据域,另一部分是指向下一个节点的指针域。链式栈通常包含以下基本操作:创建栈、栈的入栈操作(Push)、栈的出栈操作(Pop)、判断栈空、获取栈顶元素、销毁栈。

1. 定义链式栈的节点结构体

typedef struct StackNode {
    int data; // 数据域
    struct StackNode* next; // 指针域
} StackNode, *LinkStack;

2. 创建栈

创建栈本质上是初始化栈顶指针。

void InitStack(LinkStack* top) {
    *top = NULL;
}

 3. 入栈操作(Push)

void Push(LinkStack* top, int e) {
    StackNode* newNode = (StackNode*)malloc(sizeof(StackNode));
    newNode->data = e;
    newNode->next = *top;
    *top = newNode;
}

 4. 出栈操作(Pop)

int Pop(LinkStack* top, int* e) {
    if (*top == NULL) return 0; // 栈为空
    StackNode* temp;
    *e = (*top)->data;
    temp = *top;
    *top = (*top)->next;
    free(temp);
    return 1;
}

 5. 判断栈空

int IsEmpty(LinkStack top) {
    return top == NULL;
}

 6. 获取栈顶元素

int GetTop(LinkStack top, int* e) {
    if (top == NULL) return 0; // 栈为空
    *e = top->data;
    return 1;
}

 7. 销毁栈

void DestroyStack(LinkStack* top) {
    StackNode* temp;
    while (*top != NULL) {
        temp = *top;
        *top = (*top)->next;
        free(temp);
    }
}

 以上是链式栈的基本操作。在实际应用中,这些操作可根据具体需求进行调整和扩展。链式栈的优点是不需要事先指定栈的大小,它可以动态地进行内存分配,但每个节点需要额外的空间来存储指针。总体代码如下:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct LinkStack{
    int data;
    struct LinkStack *next;
};
//初始化
void Init_Stack(LinkStack *&S)
{
    S=(LinkStack *)malloc(sizeof(LinkStack));
    S->next=NULL;
}
//进栈
int push_Stack(LinkStack *&S,int e)
{
    LinkStack *s;
    s=(LinkStack *)malloc(sizeof(LinkStack));
    s->data=e;
    s->next=S->next;
    S->next=s;
}
//出栈
int pop_Stack(LinkStack *&S,int &e)
{
    LinkStack *s;
    if(S->next==NULL)
        return 0;
    s=(LinkStack *)malloc(sizeof(LinkStack));
    s=S->next;
    e=s->data;
    S->next=s->next;
    free(s);
    return 1;
}
//显示
int show_Stack(LinkStack *S)
{
    LinkStack *p;
    p=S->next;
    while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
}
int main()
{
    LinkStack *S;
    Init_Stack(S);
    push_Stack(S,1);
    push_Stack(S,2);
    push_Stack(S,3);
    show_Stack(S);
    int a=0;
    pop_Stack(S,a);
    show_Stack(S);
    printf("%d",a);
}

常用场景:

1. 函数调用栈

在计算机程序中,函数调用经常使用栈来管理。每当一个函数被调用时,其参数、返回地址及局部变量被压入栈中,当函数执行完成后,这些信息被弹出栈。链式栈可以动态地管理这种栈空间,特别是在嵌套或递归函数调用较多的情况下。

2. 表达式求值

链式栈被广泛用于算术或逻辑表达式的求值,特别是在处理复杂表达式(包括中缀、前缀和后缀表达式)的转换和计算中。栈用于存储操作数和操作符,以及在整个求值过程中临时保存中间结果。

3. 括号匹配

在编程语言中,链式栈可以用来检查括号(如圆括号、方括号、花括号)是否正确匹配。每次遇到开放括号时,将其压入栈中;遇到闭合括号时,检查并弹出栈顶的开放括号,以判断括号是否成对出现。

4. 逆序输出

链式栈可用于实现数据的逆序输出。例如,字符串或数字的逆序打印,通过将字符或数字依次压入栈中,然后弹出,就可以实现逆序输出。

5. 回溯算法

在解决某些递归问题时,如深度优先搜索(DFS)、迷宫问题、八皇后问题等,链式栈可以用于实现回溯算法,记录搜索路径和状态,方便在退回到上一步时恢复状态。

6. 页面历史记录

在浏览器中,链式栈可以用来管理用户的页面访问历史。每次用户访问新页面时,地址被压入栈中。当用户点击后退按钮时,可以从栈中弹出最近访问的页面地址。

7. 语法分析

在编译器设计中,链式栈可用于语法分析阶段,特别是在实现某些解析算法(如LL(1)分析、LR分析)时,用于存储待分析的符号和状态。

8. 逆波兰表达式(后缀表达式)计算

在计算机科学中,逆波兰表达式的计算经常使用栈。链式栈可以用于存储操作数,遇到操作符时进行相应的计算。

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是C语言的基本操作代码展示: ```c #include <stdio.h> #include <stdlib.h> // 定义链结构体 struct node { int data; struct node *next; }; // 初始化链 void initStack(struct node **top) { *top = NULL; } // 判断链是否为空 int isEmpty(struct node *top) { return top == NULL; } // 入操作 void push(struct node **top, int data) { struct node *newNode = (struct node*)malloc(sizeof(struct node)); if (newNode == NULL) { printf("内存分配失败"); exit(1); } newNode->data = data; newNode->next = *top; *top = newNode; } // 出操作 int pop(struct node **top) { if (isEmpty(*top)) { printf("链为空"); exit(1); } int data = (*top)->data; struct node *temp = *top; *top = (*top)->next; free(temp); return data; } // 获取顶元素 int peek(struct node *top) { if (isEmpty(top)) { printf("链为空"); exit(1); } return top->data; } // 输出链 void display(struct node *top) { if (isEmpty(top)) { printf("链为空"); return; } struct node *temp = top; while (temp != NULL) { printf("%d->", temp->data); temp = temp->next; } printf("NULL"); } int main() { struct node *top; initStack(&top); push(&top, 1); push(&top, 2); push(&top, 3); printf("链元素为:"); display(top); printf("\n"); printf("顶元素为:%d\n", peek(top)); printf("出元素为:%d\n", pop(&top)); printf("链元素为:"); display(top); printf("\n"); return 0; } ``` 注:上述代码中,`struct node` 表示链的节点,包含 `data` 和 `next` 两个成员变量。`top` 表示链顶节点。`initStack` 函数用于初始化链,将顶节点设置为 `NULL`。`isEmpty` 函数用于判断链是否为空。`push` 函数用于入操作,将新的节点插入链表头部。`pop` 函数用于出操作,从链表头部取出节点并删除。`peek` 函数用于获取顶元素。`display` 函数用于输出链。`main` 函数中展示了链的基本操作,包括初始化、入、获取顶元素、出和输出链等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fanstuck

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值