0x00数据结构——C语言实现(栈+后缀表达式计算)

0x00数据结构——C语言实现(栈)

栈的实现

/*
 栈(tack)是限制插入和删除只能在一个位置上进行的表,该位置是表的末端,叫做栈的顶(top)。
 对栈的基本操作有Push(进栈)和Pop(出栈)。

 Functions:
 (在链表中增加附加头结点的版本)
    创建一个空栈
    将栈置为空
    计算栈长度
    返回栈的地址
    栈push操作函数
    栈pop函数
    取栈顶元素Top函数
    判断栈空,空返回真,否则返回假
    输出

*/

#ifndef STACK_H
#define STACK_H


#define MAXLEN 100
typedef enum {
    false = 0,
    true
} BOOL;

//数据结构。
struct node;
typedef struct node node;
typedef struct node *to_node;
typedef to_node pos;

struct stack_node;
typedef struct stack_node stack_node;
typedef stack_node *stack;


//创建一个空栈
stack create_stack(void);

//将栈置为空
BOOL set_empty(stack s);

//计算栈长度
int calc_length(stack s);


//栈push操作函数
int push(stack s, int x);

//栈pop函数
int pop(stack s);

//取栈顶元素Top函数
int get_val(stack s);

//判断栈空,空返回真,否则返回假
BOOL is_empty(stack s);

//输出
void output(stack s);

#endif
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>


/*
struct node;
typedef struct node node;
typedef struct node *to_node;
typedef to_node pos;

struct stack_node;
typedef struct stack_node stack_node;
typedef stack_node *stack;
*/
struct node {
    int val;
    struct node *next;
};
struct stack_node {
    int capacity;
    struct node *top;
};
/*
    用链表来实现栈,这里设计一个额外的栈头节点,该节点指向栈顶(便于进栈和出栈)
*/
//创建一个空栈
stack create_stack(void)
{
    stack tmp = (stack)malloc(sizeof(stack_node));
    if(tmp != NULL) {
        tmp->top = NULL;
        tmp->capacity = 0;
    }
    return tmp;
}

//将栈置为空
BOOL set_empty(stack s)
{
    pos tmp;
    while(s->capacity != 0) {
        tmp = s->top;
        s->top = tmp->next;
        free(tmp);
        (s->capacity)--;
    }
    return true;
}

//计算栈长度
int calc_length(stack s)
{
    return s->capacity;
}


//栈push操作函数
int push(stack s, int x)
{
    node *tmp = (node *)malloc(sizeof(node));
    tmp->val = x;
    tmp->next = s->top;
    s->top = tmp;
    (s->capacity)++;
    return x;
}

//栈pop函数
int pop(stack s)
{
    node *tmp;
    int r = 0;
    tmp = s->top;
    s->top = tmp->next;
    (s->capacity)--;
    r = tmp->val;
    free(tmp);
    return r;
}

//取栈顶元素Top函数
int get_val(stack s)
{
    return s->top->val;
}

//判断栈空,空返回真,否则返回假
BOOL is_empty(stack s)
{
    return (s->capacity == 0);
}

//输出
void output(stack s)
{
    pos tmp = s->top;
    while(tmp != NULL) {
        printf("%d->", tmp->val);
        tmp = tmp->next;
    }
    printf("|
");
}

利用栈进行后缀表达式的计算

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "stack.h"


int main()
{
//  后缀表达式的计算

    stack s;
    int i = 0, len = 0;
    int tmp1, tmp2;
    int op;
//  char in[100] = {0};
    char in[] = {'6','5','2','3','+','8','*','+','3','+','*'};
    s = create_stack();
//  while((in[i] = getchar())!=EOF) {
//      i++;
//  }

    len = strlen(in);

    for(i = 0; i<len; i++) {
        tmp1 = in[i];
        if(tmp1=='+' || tmp1=='-' || tmp1 == '*' || tmp1 == '/') {
            op = tmp1;
            tmp1 = pop(s);
            tmp2 = pop(s);
            switch(op) {
                case '+': push(s, tmp1+tmp2);break;
                case '-': push(s, tmp1-tmp2);break;
                case '*': push(s, tmp1*tmp2);break;
                case '/': push(s, tmp1/tmp2);break;
                default: break;
            }
        } else if(isdigit(tmp1)) {
            push(s,tmp1-48);
        }
        output(s);
    }

    output(s);
    return 0;
}

实验结果

6->|
5->6->|
2->5->6->|
3->2->5->6->|
5->5->6->|
8->5->5->6->|
40->5->6->|
45->6->|
3->45->6->|
48->6->|
288->|
288->|
Time elapsed: 000:00:047
Press any key to continue
原文地址:https://www.cnblogs.com/born2run/p/9581342.html