栈是一种数据结构
栈里可以存放数字,这些数字有前后顺序
先放到栈里的数字在前面,后放到栈里的
数字在后面
从栈里获得的数字一定是最后一个放进去的
数字
这种使用数字的方法叫后进先出
在实现栈的时候需要提供一个push函数,它
负责向栈里放一个数字
在实现栈的时候还需要提供一个pop函数,它
负责从栈里获得一个数字

/*
    栈演示
*/
#ifndef     __02STACK_H__
#define     __02STACK_H__
typedef struct {
    int buf[SIZE];
    int qty;
} stack;
//初始化函数
void stack_init(stack *);
//清理函数
void stack_deinit(stack *);
//判断栈是不是空的函数
int stack_empty(const stack *);
//判断栈是不是满的函数
int stack_full(const stack *);
//获得栈里的数字个数
int stack_size(const stack *);
//向栈里放数字的函数
void stack_push(stack *, int );
//从栈里拿走最后一个数字
int stack_pop(stack *);
//获得栈里最后一个数字
int stack_top(const stack *);
#endif   //__02STACK_H__




/*
    栈演示
*/
#include "02stack.h"
//初始化函数
void stack_init(stack *p_stack) {
    p_stack->qty = 0;
}
//清理函数
void stack_deinit(stack *p_stack) {
    p_stack->qty = 0;
}
//判断栈是不是空的函数
int stack_empty(const stack *p_stack) {
    return !(p_stack->qty);
}
//判断栈是不是满的函数
int stack_full(const stack *p_stack) {
    return p_stack->qty == SIZE;
}
//获得栈里的数字个数
int stack_size(const stack *p_stack) {
    return p_stack->qty;
}
//向栈里放数字的函数
void stack_push(stack *p_stack, int num) {
    p_stack->buf[p_stack->qty] = num;
    p_stack->qty++;
}
//从栈里拿走最后一个数字
int stack_pop(stack *p_stack) {
    p_stack->qty--;
    return p_stack->buf[p_stack->qty];
}
//获得栈里最后一个数字
int stack_top(const stack *p_stack) {
    return p_stack->buf[p_stack->qty - 1];
}





/*
    栈测试
*/
#include <stdio.h>
#include "02stack.h"
int main() {
    stack stk = {0};
    stack_init(&stk);
    stack_push(&stk, 10);
    stack_push(&stk, 20);
    stack_push(&stk, 30);
    stack_push(&stk, 40);
    stack_push(&stk, 50);
    stack_push(&stk, 60);
    printf("数字个数是%d\n", stack_size(&stk));
    printf("判断满的结果是%d\n", stack_full(&stk));
    printf("%d ", stack_pop(&stk));
    printf("%d ", stack_pop(&stk));
    printf("%d ", stack_pop(&stk));
    printf("%d ", stack_pop(&stk));
    printf("%d ", stack_pop(&stk));
    printf("%d\n", stack_pop(&stk));
    stack_deinit(&stk);
    return 0;
}
/*
    栈演示
*/
#ifndef     __03STACK_H__
#define     __03STACK_H__
typedef struct node {
    int num;
    struct node *p_next;
} node;
typedef struct {
    node head, tail;
} stack;
//初始化函数
void stack_init(stack *);
//清理函数
void stack_deinit(stack *);
//判断栈是不是空的函数
int stack_empty(const stack *);
//判断栈是不是满的函数
int stack_full(const stack *);
//获得栈里的数字个数
int stack_size(const stack *);
//向栈里放数字的函数
void stack_push(stack *, int );
//从栈里拿走最后一个数字
int stack_pop(stack *);
//获得栈里最后一个数字
int stack_top(const stack *);
#endif   //__03STACK_H__

/*
    栈演示
*/
#include <stdlib.h>
#include "03stack.h"
//初始化函数
void stack_init(stack *p_stack) {
    p_stack->head.p_next = &(p_stack->tail);
    p_stack->tail.p_next = NULL;
}
//清理函数
void stack_deinit(stack *p_stack) {
    while (p_stack->head.p_next != &(p_stack->tail)) {
        node *p_first = &(p_stack->head);
        node *p_mid = p_first->p_next;
        node *p_last = p_mid->p_next;
        p_first->p_next = p_last;
        free(p_mid);
        p_mid = NULL;
    }
}
//判断栈是不是空的函数
int stack_empty(const stack *p_stack) {
    return p_stack->head.p_next == &(p_stack->tail);
}
//判断栈是不是满的函数
int stack_full(const stack *p_stack) {
    return 0;
}
//获得栈里的数字个数
int stack_size(const stack *p_stack) {
    int cnt = 0;
    const node *p_node = NULL;
    for (p_node = &(p_stack->head);p_node != &(p_stack->tail);p_node = p_node->p_next) {
        const node *p_first = p_node;
        const node *p_mid = p_first->p_next;
        const node *p_last = p_mid->p_next;
        if (p_mid != &(p_stack->tail)) {
            cnt++;
        }
    }
    return cnt;
}
//向栈里放数字的函数
void stack_push(stack *p_stack, int num) {
    node *p_first = NULL, *p_mid = NULL, *p_last = NULL;
    node *p_tmp = (node *)malloc(sizeof(node));
    if (!p_tmp) {
        return ;
    }
    p_tmp->num = num;
    p_tmp->p_next = NULL;
    p_first = &(p_stack->head);
    p_mid = p_first->p_next;
    p_last = p_mid->p_next;
    p_first->p_next = p_tmp;
    p_tmp->p_next = p_mid;
}
//从栈里拿走最后一个数字
int stack_pop(stack *p_stack) {
    int ret = 0;
    node *p_first = &(p_stack->head);
    node *p_mid = p_first->p_next;
    node *p_last = p_mid->p_next;
    p_first->p_next = p_last;
    ret = p_mid->num;
    free(p_mid);
    p_mid = NULL;
    return ret;
}
//获得栈里最后一个数字
int stack_top(const stack *p_stack) {
    return p_stack->head.p_next->num;
}




/*
    栈测试
*/
#include <stdio.h>
#include "03stack.h"
int main() {
    stack stk = {0};
    stack_init(&stk);
    stack_push(&stk, 10);
    stack_push(&stk, 20);
    stack_push(&stk, 30);
    stack_push(&stk, 40);
    stack_push(&stk, 50);
    stack_push(&stk, 60);
    printf("数字个数是%d\n", stack_size(&stk));
    printf("判断满的结果是%d\n", stack_full(&stk));
    printf("%d ", stack_pop(&stk));
    printf("%d ", stack_pop(&stk));
    printf("%d ", stack_pop(&stk));
    printf("%d ", stack_pop(&stk));
    printf("%d ", stack_pop(&stk));
    printf("%d\n", stack_pop(&stk));
    stack_deinit(&stk);
    return 0;
}
/*
    栈练习
*/
#include <stdio.h>
#include "02stack.h"
int main() {
    int num = 0, num1 = 0, opr = 0;
    stack stk = {0};
    char buf[50] = {0}, *p_ch = buf;
    printf("请输入一个公式:");
    fgets(buf, 50, stdin);
    stack_init(&stk);
    while (1) {
        if (*p_ch >= '0' && *p_ch <= '9') {
            stack_push(&stk, *p_ch - '0');
        }
        else if (*p_ch == '*' || *p_ch == '/') {
            num = stack_pop(&stk);
            num1 = *(p_ch + 1) - '0';
            if (*p_ch == '*') {
                stack_push(&stk, num * num1);
            }
            else {
                stack_push(&stk, num / num1);
            }
            p_ch++;
        }
        else if (*p_ch == '+' || *p_ch == '-') {
            if (stack_size(&stk) == 3) {
                num1 = stack_pop(&stk);
                opr = stack_pop(&stk);
                num = stack_pop(&stk);
                if (opr == '+') {
                    stack_push(&stk, num + num1);
                }
                else {
                    stack_push(&stk, num - num1);
                }
            }
            stack_push(&stk, *p_ch);
        }
        else {
            if (stack_size(&stk) == 3) {
                num1 = stack_pop(&stk);
                opr = stack_pop(&stk);
                num = stack_pop(&stk);
                if (opr == '+') {
                    printf("计算结果是%d\n", num + num1);
                }
                else {
                    printf("计算结果是%d\n", num - num1);
                }
            }
            else {
                printf("计算结果是%d\n", stack_pop(&stk));
            }
            break;
        }
        p_ch++;
    }
    stack_deinit(&stk);
    return 0;
}

 

posted @ 2018-03-30 10:22  Truman001  阅读(168)  评论(0编辑  收藏  举报