C语言栈
怎么才能做到图片上的输入效果,比如push 56为入栈,pop为出栈,end表示结束 这问题有意思,帮你写了不过还是建议自己动手写,自己在写的过程中遇到了问题再提问,而不是直接要一个答案,直接看答案对你自己没有什么好处
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; 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;
}
c++ ?
我竟然写代码不看题目要求,还写了半天 stack
我服了,^_^
人造人 发表于 2021-11-3 21:41
c++ ?
我竟然写代码不看题目要求,还写了半天 stack
那个输入格式是怎么实现的,比如push 65 这题目对于 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;
}
434773632 发表于 2021-11-3 21:52
那个输入格式是怎么实现的,比如push 65
输入交给 get_instruction 函数
434773632 发表于 2021-11-3 21:52
那个输入格式是怎么实现的,比如push 65
push后面没有回车,直接加空格和数字 434773632 发表于 2021-11-3 21:56
push后面没有回车,直接加空格和数字
怎么实现
434773632 发表于 2021-11-3 21:57
怎么实现
什么? #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;
}
?
究竟是 C语言还是 C++
人造人 发表于 2021-11-3 22:02
什么?
就是你要入栈的话,输入'push 56', push后面不能加回车 后缀 cpp,明显是 C++ 没问题
人造人 发表于 2021-11-3 22:10
?
究竟是 C语言还是 C++
都行 434773632 发表于 2021-11-3 22:11
就是你要入栈的话,输入'push 56', push后面不能加回车
对呀,你看我的 get_instruction 函数
这个函数要求加回车了? 人造人 发表于 2021-11-3 22:13
对呀,你看我的 get_instruction 函数
这个函数要求加回车了?
我看不懂c++,我只学过c语言,你把那个函数改成c语言的吧,感谢 434773632 发表于 2021-11-3 22:15
我看不懂c++,我只学过c语言,你把那个函数改成c语言的吧,感谢
给你第 0 次发的代码就是 C语言的
人造人 发表于 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;
scanf("%d", &max);
SS_Init(&S, max);
while(1)
{
while((ch = getchar()) != ' ')
{
a = 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‘就入栈的形式 434773632 发表于 2021-11-3 22:15
我看不懂c++,我只学过c语言,你把那个函数改成c语言的吧,感谢
instruction_t get_instruction(void) {
char buff; 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;
} 434773632 发表于 2021-11-3 22:16
你在我这个基础上改改,改成’push 56‘就入栈的形式
我看看
页:
[1]
2