鱼C论坛

 找回密码
 立即注册
查看: 1883|回复: 30

[已解决]C语言栈

[复制链接]
发表于 2021-11-3 19:39:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
怎么才能做到图片上的输入效果,比如push 56为入栈,pop为出栈,end表示结束 屏幕截图 2021-11-03 162110.png 屏幕截图 2021-11-03 162057.png
最佳答案
2021-11-3 22:41:34
/*
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define true 1
#define false 0
#define Status int

typedef struct {
    int *base;
    int *top;
    int stacksize;
} SqStack;

/*
Status SS_Init(SqStack *S, int max);
Status SS_IsFull(SqStack S);
Status SS_IsEmpty(SqStack S);
void SS_Length(SqStack S);
Status SS_push(SqStack *S, int e);
Status SS_pop(SqStack *S, int *e);
*/

Status SS_Init(SqStack *S, int max) {
    //S->base = (int *)malloc(max * sizeof(int));
    S->base = malloc(max * sizeof(int));
    //if(!(S->base)) exit(1);
    if(!S->base) exit(1);
    S->top = S->base;
    S->stacksize = max;
    return true;
}

// 为什么不写释放函数?
void stack_deinit(SqStack *s) {
    free(s->base);
}

//Status SS_IsFull(SqStack S) {
Status SS_IsFull(SqStack *S) {
    //if(S.top - S.base == S.stacksize) {
    if(S->top - S->base == S->stacksize) {
        return true;
    } else {
        return false;
    }
}

//Status SS_IsEmpty(SqStack S) {
Status SS_IsEmpty(SqStack *S) {
    //if(S.top == S.base) {
    if(S->top == S->base) {
        return true;
    } else {
        return false;
    }
}

//void SS_Length(SqStack S) { printf("Stack length:%d\n", S.top - S.base); }
//void SS_Length(SqStack S) { printf("Stack length:%ld\n", S.top - S.base); }
void SS_Length(SqStack *S) { printf("Stack length:%ld\n", S->top - S->base); }

Status SS_push(SqStack *S, int e) {
    /*
    int m;
    m = SS_IsFull(*S);
    */
    //int m = SS_IsFull(*S);
    int m = SS_IsFull(S);
    //if(!m) {
    if(m) {
        return false;
    } else {
        *(S->top) = e;
        S->top++;
        return true;
    }
}

Status SS_pop(SqStack *S, int *e) {
    /*
    int m;
    m = SS_IsEmpty(*S);
    */
    //int m = SS_IsEmpty(*S);
    int m = SS_IsEmpty(S);
    //if(!m) {
    if(m) {
        return false;
    } else {
        (S->top)--;
        *e = *(S->top);
    }
    return true;
}

//Status SS_print(SqStack S) {
Status SS_print(SqStack *S) {
    /*
    int m;
    m = SS_IsEmpty(S);
    */
    int m = SS_IsEmpty(S);
    //if(!m) {
    if(m) {
        printf("stack data: Empty!\n");
        return false;
    } else {
        //printf("stack data (from bottom to top):");
        printf("stack data (from top to bottom): ");
        //while(S.top != S.base) {
        while(S->top != S->base) {
            S->top--;
            //printf("%d ", *(S->top));
            printf("%d ", *S->top);
        }
        //printf("%d", S.base);
        //printf("%d", *S.base);
        //printf("%d", *S->base);
    }

    printf("\n");

    return true;
}

int main() {
    SqStack S;
    //int max, b, i = 0;
    //int b, i = 0;
    int b;
    //char ch;
    char a[zxsq-anti-bbcode-10];
    /*
    scanf("%d", &max);
    SS_Init(&S, max);
    */
    int temp;
    scanf("%d", &temp);
    SS_Init(&S, 1024);
    while(1) {
        scanf("%s", a);
        /*
        while((ch = getchar()) != ' ') {
            a[zxsq-anti-bbcode-i] = ch;
            i++;
        }
        */
        if(strcmp(a, "push") == 0) {
            scanf("%d", &b);
            SS_push(&S, b);
        }
        if(strcmp(a, "pop") == 0) {
            //SS_push(&S, b);
            SS_pop(&S, &b);
        }
        if(strcmp(a, "end") == 0) {
            break;
        }
    }
    //SS_Length(S);
    SS_Length(&S);
    //SS_print(S);
    SS_print(&S);
    stack_deinit(&S);
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-3 21:38:43 | 显示全部楼层
这问题有意思,帮你写了
不过还是建议自己动手写,自己在写的过程中遇到了问题再提问,而不是直接要一个答案,直接看答案对你自己没有什么好处

stack.h
#ifndef _STACK_H_
#define _STACK_H_

#include <stddef.h>
#include <stdbool.h>

typedef int element_t;

struct stack_node_tag {
    element_t data;
    struct stack_node_tag *next;
};

typedef struct {
    struct stack_node_tag *head;
    size_t size;
} stack_t;

stack_t *stack_init(void);
void stack_deinit(stack_t *s);
bool stack_push(stack_t *s, element_t e);
bool stack_pop(stack_t *s, element_t *e);
bool stack_empty(const stack_t *s);
bool stack_size(const stack_t *s, size_t *size);
bool stack_clean(stack_t *s);

#endif

stack.c
#include "stack.h"
#include <stdlib.h>

stack_t *stack_init(void) {
    stack_t *s = malloc(sizeof(*s));
    if(!s) return NULL;
    s->head = NULL;
    s->size = 0;
    return s;
}

void stack_deinit(stack_t *s) {
    if(!s) return;
    stack_clean(s); free(s);
}

bool stack_push(stack_t *s, element_t e) {
    if(!s) return false;
    struct stack_node_tag *node = malloc(sizeof(*node));
    if(!node) return false;
    node->data = e;
    node->next = s->head;
    s->head = node;
    ++s->size;
    return true;
}

bool stack_pop(stack_t *s, element_t *e) {
    if(!s) return false;
    struct stack_node_tag *temp = s->head;
    s->head = temp->next;
    if(e) *e = temp->data;
    free(temp); --s->size;
    return true;
}

bool stack_empty(const stack_t *s) {
    if(!s) abort();
    return s->size == 0;
}

bool stack_size(const stack_t *s, size_t *size) {
    if(!s) return false;
    if(!size) return false;
    *size = s->size;
    return true;
}

bool stack_clean(stack_t *s) {
    if(!s) return false;
    while(!stack_empty(s)) stack_pop(s, NULL);
    return true;
}

main.c
#include "stack.h"
#include <stdio.h>
#include <string.h>

enum {ERR, PUSH, POP, END};

typedef struct {
    int type;
    int value;
} instruction_t;

instruction_t get_instruction(void) {
    char buff[1024]; scanf("%s", buff);
    instruction_t ins;
    if(!strcmp(buff, "end")) {
        ins.type = END;
    } else if(!strcmp(buff, "push")) {
        ins.type = PUSH;
        scanf("%d", &ins.value);
    } else if(!strcmp(buff, "pop")) {
        ins.type = POP;
    } else ins.type = ERR;
    return ins;
}

void output(stack_t *stack) {
    size_t length; stack_size(stack, &length);
    printf("Stack length: %lu\n", length);
    printf("stack data (from bottom to top): ");
    stack_t *temp = stack_init();
    while(!stack_empty(stack)) {
        int data; stack_pop(stack, &data);
        stack_push(temp, data);
    }
    while(!stack_empty(temp)) {
        int data; stack_pop(temp, &data);
        printf("%d ", data);
    }
    printf("\n");
    stack_deinit(temp);
}

int main(void) {
    int temp; scanf("%d", &temp);   // 完全不需要这个 6
    stack_t *stack = stack_init();
    while(1) {
        instruction_t ins = get_instruction();
        if(ins.type == END) break;
        switch(ins.type) {
            case PUSH: stack_push(stack, ins.value); break;
            case POP: stack_pop(stack, NULL); break;
        }
    }
    output(stack);
    stack_deinit(stack);
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-11-3 21:41:45 | 显示全部楼层
c++ ?

我竟然写代码不看题目要求,还写了半天 stack
我服了,^_^
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2021-11-3 21:52:03 | 显示全部楼层
人造人 发表于 2021-11-3 21:41
c++ ?

我竟然写代码不看题目要求,还写了半天 stack

那个输入格式是怎么实现的,比如push 65
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-3 21:52:27 | 显示全部楼层
这题目对于 C语言来说有意思,对于 C++ 来说没意思
#include <iostream>
#include <string>
#include <stack>

enum {ERR, PUSH, POP, END};

typedef struct {
    int type;
    int value;
} instruction_t;

instruction_t get_instruction(void) {
    std::string str; std::cin >> str;
    instruction_t ins;
    if(str == "end") {
        ins.type = END;
    } else if(str == "push") {
        ins.type = PUSH;
        std::cin >> ins.value;
    } else if(str == "pop") {
        ins.type = POP;
    } else ins.type = ERR;
    return ins;
}

int main() {
    {int temp; std::cin >> temp;}
    std::stack<int> stack;
    while(1) {
        instruction_t ins = get_instruction();
        if(ins.type == END) break;
        switch(ins.type) {
            case PUSH: stack.push(ins.value); break;
            case POP: stack.pop(); break;
        }
    }
    size_t length = stack.size();
    printf("Stack length: %lu\n", length);
    printf("stack data (from bottom to top): ");
    std::stack<int> temp;
    while(!stack.empty()) {
        int data = stack.top(); stack.pop();
        temp.push(data);
    }
    while(!temp.empty()) {
        int data = temp.top(); temp.pop();
        std::cout << data << " ";
    }
    std::cout << std::endl;
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-3 21:53:37 | 显示全部楼层
434773632 发表于 2021-11-3 21:52
那个输入格式是怎么实现的,比如push 65

输入交给 get_instruction 函数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-3 21:56:24 | 显示全部楼层
434773632 发表于 2021-11-3 21:52
那个输入格式是怎么实现的,比如push 65

push后面没有回车,直接加空格和数字
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-3 21:57:20 | 显示全部楼层
434773632 发表于 2021-11-3 21:56
push后面没有回车,直接加空格和数字

怎么实现
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-3 22:02:52 | 显示全部楼层

什么?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-3 22:03:22 | 显示全部楼层
#include <iostream>
#include <string>
#include <stack>

enum {ERR, PUSH, POP, END};

typedef struct {
    int type;
    int value;
} instruction_t;

instruction_t get_instruction(void) {
    std::string str; std::cin >> str;
    instruction_t ins;
    if(str == "end") {
        ins.type = END;
    } else if(str == "push") {
        ins.type = PUSH;
        std::cin >> ins.value;
    } else if(str == "pop") {
        ins.type = POP;
    } else ins.type = ERR;
    return ins;
}

int main() {
    {int temp; std::cin >> temp;}
    std::stack<int> stack;
    while(1) {
        instruction_t ins = get_instruction();
        if(ins.type == END) break;
        switch(ins.type) {
            case PUSH: stack.push(ins.value); break;
            case POP: stack.pop(); break;
        }
    }
    std::cout << "Stack length: " << stack.size() << std::endl;
    std::cout << "stack data (from bottom to top): ";
    std::stack<int> temp;
    while(!stack.empty()) {
        int data = stack.top(); stack.pop();
        temp.push(data);
    }
    while(!temp.empty()) {
        int data = temp.top(); temp.pop();
        std::cout << data << " ";
    }
    std::cout << std::endl;
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-3 22:10:50 | 显示全部楼层
?
究竟是 C语言还是 C++
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-3 22:11:45 | 显示全部楼层

就是你要入栈的话,输入'push 56', push后面不能加回车
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-3 22:12:35 | 显示全部楼层
后缀 cpp,明显是 C++ 没问题


1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-3 22:12:36 | 显示全部楼层
人造人 发表于 2021-11-3 22:10
?
究竟是 C语言还是 C++

都行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-3 22:13:40 | 显示全部楼层
434773632 发表于 2021-11-3 22:11
就是你要入栈的话,输入'push 56', push后面不能加回车

对呀,你看我的 get_instruction 函数
这个函数要求加回车了?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-3 22:15:03 | 显示全部楼层
人造人 发表于 2021-11-3 22:13
对呀,你看我的 get_instruction 函数
这个函数要求加回车了?

我看不懂c++,我只学过c语言,你把那个函数改成c语言的吧,感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-3 22:16:35 | 显示全部楼层
434773632 发表于 2021-11-3 22:15
我看不懂c++,我只学过c语言,你把那个函数改成c语言的吧,感谢

给你第 0 次发的代码就是 C语言的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-3 22:16:50 | 显示全部楼层
人造人 发表于 2021-11-3 22:13
对呀,你看我的 get_instruction 函数
这个函数要求加回车了?
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#define true 1
#define false 0
#define Status int

typedef struct
{
        int *base;
        int *top;
        int stacksize;
}SqStack;

Status SS_Init(SqStack *S, int max);
Status SS_IsFull(SqStack S);
Status SS_IsEmpty(SqStack S);
void SS_Length(SqStack S);
Status SS_push(SqStack *S, int e);
Status SS_pop(SqStack *S, int *e);


Status SS_Init(SqStack *S, int max)
{
        S->base = (int *)malloc(max * sizeof(int));
        if(!(S->base)) exit(1);
        S->top = S->base;
        S->stacksize = max;
        return true;
}

Status SS_IsFull(SqStack S)
{
        if(S.top - S.base == S.stacksize)
        {
                return true;
        }
        else
        {
                return false;
        }
}

Status SS_IsEmpty(SqStack S)
{
        if(S.top == S.base)
        {
                return true;
        }
        else
        {
                return false;
        }
}

void SS_Length(SqStack S)
{
        printf("Stack length:%d\n", S.top - S.base);
}

Status SS_push(SqStack *S, int e)
{
        int m;
        m = SS_IsFull(*S);
        if(!m)
        {
                return false;
        }
        else
        {
                *(S->top) = e;
                S->top++;
                return true;
        }
}

Status SS_pop(SqStack *S, int *e)
{
        int m;
        m = SS_IsEmpty(*S);
        if(!m)
        {
                return false;
        }
        else
        {
                (S->top)--;
                *e = *(S->top);
        }
}

Status SS_print(SqStack S)
{
        int m;
        m = SS_IsEmpty(S);
        if(!m)
        {
                printf("stack data: Empty!\n");
                return false;
        }
        else
        {
                printf("stack data (from bottom to top):");
                while(S.top != S.base)
                {
                        S.top--;
                        printf("%d ", *(S.top));
                }
                printf("%d", S.base);
        }
}

int main()
{
        SqStack S;
        int max, b, i = 0;
        char ch;
        char a[10];
        scanf("%d", &max);
        SS_Init(&S, max);
        while(1)
        {
                while((ch = getchar()) != ' ')
                {
                        a[i] = ch;
                        i++;
                }
                if(strcmp(a, "push") == 0)
                {
                        scanf("%d", &b);
                        SS_push(&S, b);
                }
                if(strcmp(a, "pop") == 0)
                {
                        SS_push(&S, b);
                }
                if(strcmp(a, "end") == 0)
                {
                        break;
                }
        }
        SS_Length(S);
        SS_print(S); 
        
}
你在我这个基础上改改,改成’push 56‘就入栈的形式
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-3 22:17:27 | 显示全部楼层
434773632 发表于 2021-11-3 22:15
我看不懂c++,我只学过c语言,你把那个函数改成c语言的吧,感谢
instruction_t get_instruction(void) {
    char buff[1024]; scanf("%s", buff);
    instruction_t ins;
    if(!strcmp(buff, "end")) {
        ins.type = END;
    } else if(!strcmp(buff, "push")) {
        ins.type = PUSH;
        scanf("%d", &ins.value);
    } else if(!strcmp(buff, "pop")) {
        ins.type = POP;
    } else ins.type = ERR;
    return ins;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-3 22:21:37 | 显示全部楼层
434773632 发表于 2021-11-3 22:16
你在我这个基础上改改,改成’push 56‘就入栈的形式

我看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 21:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表