c语言is stack empty,怎样用C语言写出对栈进行的五种运算:push()、pop()、top()、empty()、makempty()...

满意答案

这是我用链表写的:

#include

#include

typedef struct node

{

int x;

struct node *next;

}Node;

typedef struct stack

{

Node *top;

}Stack;

void InitStack(Stack *s)

{

s->top=NULL;

}

int IsEmpty(Stack *s)

{

if(s->top==NULL)

return 1;

else

return 0;

}

void PushStack(Stack *s,int x)

{

Node *p;

p=(Node*)malloc(sizeof(Node));

p->x=x;

//    p->next=NULL;

p->next=s->top;

s->top=p;

}

int PopStack(Stack *s)

{

int data;

Node *p;

p=(Node *)malloc(sizeof(Node));

if(IsEmpty(s))

{

printf("the stack is empty!\n");

free(p);

return -1;

}

else

{

p=s->top;

data=p->x;

s->top=p->next;

free(p);

return data;

}

}

int main (int argc,char **argv)

{

int i;

Stack s;

InitStack(&s);

for(i=0;i<1000;i++)

{

PushStack(&s,i);

}

for(i=0;i<1000;i++)

{

printf("%d\n",PopStack(&s));

}

}

13分享举报

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C语言实现栈实现四则运算的代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 #define ERROR -2147483648 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 x) { if (isFull(s)) { printf("Error: stack is full\n"); return; } s->data[++s->top] = x; } int pop(Stack *s) { if (isEmpty(s)) { printf("Error: stack is empty\n"); return ERROR; } return s->data[s->top--]; } int peek(Stack *s) { if (isEmpty(s)) { printf("Error: stack is empty\n"); return ERROR; } return s->data[s->top]; } int priority(char c) { if (c == '+' || c == '-') { return 1; } else if (c == '*' || c == '/') { return 2; } else { return 0; } } int calc(int a, int b, char op) { if (op == '+') { return a + b; } else if (op == '-') { return a - b; } else if (op == '*') { return a * b; } else if (op == '/') { return a / b; } else { printf("Error: invalid operator\n"); return ERROR; } } int evaluate(char *expr) { Stack numStack, opStack; initStack(&numStack); initStack(&opStack); int i = 0; while (expr[i] != '\0') { if (expr[i] >= '0' && expr[i] <= '9') { int num = 0; while (expr[i] >= '0' && expr[i] <= '9') { num = num * 10 + (expr[i] - '0'); i++; } push(&numStack, num); } else if (expr[i] == '+' || expr[i] == '-' || expr[i] == '*' || expr[i] == '/') { while (!isEmpty(&opStack) && priority(peek(&opStack)) >= priority(expr[i])) { int b = pop(&numStack); int a = pop(&numStack); char op = pop(&opStack); int result = calc(a, b, op); push(&numStack, result); } push(&opStack, expr[i]); i++; } else { printf("Error: invalid character\n"); return ERROR; } } while (!isEmpty(&opStack)) { int b = pop(&numStack); int a = pop(&numStack); char op = pop(&opStack); int result = calc(a, b, op); push(&numStack, result); } return pop(&numStack); } int main() { char expr[MAX_SIZE]; printf("Please enter the expression: "); scanf("%s", expr); int result = evaluate(expr); printf("Result = %d\n", result); return 0; } ``` 在该代码中,我们定义了一个栈的结构体,并实现了栈的初始化、判空、判满、入栈、出栈和查看栈顶元素等基本操作。然后我们定义了两个栈,分别用于存储数字和运算符。在处理表达式时,我们遍历表达式中的每个字符,如果是数字则将其压入数字栈中,如果是运算符则根据其优先级决定是否需要先计算其他运算符,并将该运算符压入运算符栈中。最后,当整个表达式处理完毕后,我们依次从运算符栈中取出运算符,并从数字栈中取出两个数字进行运算,将运算结果压入数字栈中,直到运算符栈为空。最终,数字栈中的唯一元素即为表达式的计算结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值