您的代码中存在几处问题:
在
isFull(SeqStack* stack)
函数中,应将SeqStack
改为Stack
,因为定义的结构体名字是Stack
。在
push(SeqStack* stack, int value)
函数中,同样需要将SeqStack
改为Stack
。在
pop(SeqStack* stack)
函数中,同样需要将SeqStack
改为Stack
。
请修正以上问题后的代码如下所示:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct Stack {
int data[MAX_SIZE];
int top;
} Stack;
void initStack(Stack* stack) {
stack->top = -1;
}
int isEmpty(Stack* stack) {
return stack->top == -1;
}
int isFull(Stack* stack) {
return stack->top == MAX_SIZE - 1;
}
void push(Stack* stack, int value) {
if (isFull(stack)) {
printf("Stack overflow\n");
return;
}
stack->data[++(stack->top)] = value;
}
int pop(Stack* stack) {
if (isEmpty(stack)) {
printf("Stack underflow\n");
return -1; // 表示栈为空值
}
return stack->data[(stack->top)--];
}
void convertDecimalToBinary(int decimal) {
Stack stack;
initStack(&stack);
while (decimal > 0) {
int remainder = decimal % 2;
push(&stack, remainder);
decimal /= 2;
}
while (!isEmpty(&stack)) {
printf("%d", pop(&stack));
}
}
void convertDecimalToOctal(int decimal) {
Stack stack;
initStack(&stack);
while (decimal > 0) {
int remainder = decimal % 8;
push(&stack, remainder);
decimal /= 8;
}
while (!isEmpty(&stack)) {
printf("%d", pop(&stack));
}
}
void convertDecimalToHexadecimal(int decimal) {
Stack stack;
initStack(&stack);
while (decimal > 0) {
int remainder = decimal % 16;
if (remainder < 10)
push(&stack, remainder + '0');
else
push(&stack, remainder - 10 + 'A');
decimal /= 16;
}
while (!isEmpty(&stack)) {
printf("%c", pop(&stack));
}
}
这样修改后的代码应该可以正确运行了。
内容由零声教学AI助手提供,问题来源于学员提问