堆栈的基本代码【C语言版本】

堆栈只能后进先出。
我的理解是一个特殊的链表,只能在链表头部进行插入和删除,其他位置不能更改。

顺序栈的基本代码:

typedef int Position;
struct SNode {
    ElementType *Data; /* 存储元素的数组 */
    Position Top;      /* 栈顶指针 */
    int MaxSize;       /* 堆栈最大容量 */
};
typedef struct SNode *Stack;
 
Stack CreateStack( int MaxSize )
{
    Stack S = (Stack)malloc(sizeof(struct SNode));
    S->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
    S->Top = -1;
    S->MaxSize = MaxSize;
    return S;
}
 
bool IsFull( Stack S )
{
    return (S->Top == S->MaxSize-1);
}
 
bool Push( Stack S, ElementType X )
{
    if ( IsFull(S) ) {
        printf("堆栈满");
        return false;
    }
    else {
        S->Data[++(S->Top)] = X;
        return true;
    }
}
 
bool IsEmpty( Stack S )
{
    return (S->Top == -1);
}
 
ElementType Pop( Stack S )
{
    if ( IsEmpty(S) ) {
        printf("堆栈空");
        return ERROR; /* ERROR是ElementType的特殊值,标志错误 */
    }
    else 
        return ( S->Data[(S->Top)--] );
}

链栈的基本代码

typedef struct SNode *PtrToSNode;
struct SNode {
    ElementType Data;
    PtrToSNode Next;
};
typedef PtrToSNode Stack;
 
Stack CreateStack( ) 
{ /* 构建一个堆栈的头结点,返回该结点指针 */
    Stack S;
 
    S = (Stack)malloc(sizeof(struct SNode));
    S->Next = NULL;
    return S;
}
 
bool IsEmpty ( Stack S )
{ /* 判断堆栈S是否为空,若是返回true;否则返回false */
    return ( S->Next == NULL );
}
 
bool Push( Stack S, ElementType X )
{ /* 将元素X压入堆栈S */
    PtrToSNode TmpCell;
 
    TmpCell = (PtrToSNode)malloc(sizeof(struct SNode));
    TmpCell->Data = X;
    TmpCell->Next = S->Next;
    S->Next = TmpCell;
    return true;
}
 
ElementType Pop( Stack S )  
{ /* 删除并返回堆栈S的栈顶元素 */
    PtrToSNode FirstCell;
    ElementType TopElem;
 
    if( IsEmpty(S) ) {
        printf("堆栈空"); 
        return ERROR;
    }
    else {
        FirstCell = S->Next; 
        TopElem = FirstCell->Data;
        S->Next = FirstCell->Next;
        free(FirstCell);
        return TopElem;
    }
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是基于堆栈实现基本算术表达式求值的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 // 堆栈结构体 typedef struct { int top; int data[MAX_SIZE]; } Stack; // 初始化堆栈 void initStack(Stack *s) { s->top = -1; } // 判断堆栈是否为空 int isEmpty(Stack *s) { return s->top == -1; } // 判断堆栈是否已满 int isFull(Stack *s) { return s->top == MAX_SIZE - 1; } // 入栈 void push(Stack *s, int item) { if (isFull(s)) { printf("Stack Overflow\n"); exit(EXIT_FAILURE); } s->data[++(s->top)] = item; } // 出栈 int pop(Stack *s) { if (isEmpty(s)) { printf("Stack Underflow\n"); exit(EXIT_FAILURE); } return s->data[(s->top)--]; } // 获取栈顶元素 int peek(Stack *s) { if (isEmpty(s)) { printf("Stack Underflow\n"); exit(EXIT_FAILURE); } return s->data[s->top]; } // 判断是否为运算符 int isOperator(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; } // 执行运算 int operate(int a, int b, char op) { switch(op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; default: exit(EXIT_FAILURE); } } // 表达式求值 int evaluate(char *expr) { Stack s; initStack(&s); int i; for (i = 0; expr[i] != '\0'; i++) { char c = expr[i]; if (c == ' ' || c == '\t') { continue; } else if (isdigit(c)) { int operand = 0; while (isdigit(c)) { operand = operand * 10 + (c - '0'); c = expr[++i]; } i--; push(&s, operand); } else if (isOperator(c)) { int op2 = pop(&s); int op1 = pop(&s); int result = operate(op1, op2, c); push(&s, result); } else { printf("Invalid Expression\n"); exit(EXIT_FAILURE); } } return pop(&s); } int main() { char expr[MAX_SIZE]; printf("Enter an arithmetic expression: "); fgets(expr, MAX_SIZE, stdin); int result = evaluate(expr); printf("Result = %d\n", result); return 0; } ``` 使用方法:运行程序后,输入一个算术表达式,程序将输出表达式的求值结果。例如,输入 "3 + 4 * 2",程序将输出 "Result = 11"。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

摆烂.MVP

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

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

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

打赏作者

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

抵扣说明:

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

余额充值