在 C 中处理堆栈的问题

Troubles with handling stacks in C

我在用 C 语言处理堆栈时遇到问题。我想创建两个,然后用它们做各种事情。当前代码在编译和运行的意义上是有效的,但输出还不完全正确。

这是代码:

#include <stdio.h>
#include <stdlib.h>

#define STACK_SIZE 5

typedef struct stackADT {
    int elements[STACK_SIZE];
    int count;
} stack;

void initialize(stack *s) {
    s->count = 0;
}

int push(stack *s, int value) {
    if (s->count < STACK_SIZE) {
        s->elements[s->count++] = value;
        return s->count - 1;
    }
    else return -1;
}

int pop(stack *s) {
    if (s->count == 0) return -1;
    else s->count--;
}

int compareStack(stack *sA, stack *sB) {
    int i = 0;

    if (sA->count != sB->count) return 0;

    else {
        for (i; i < sA->count + 1; i++) {
            if (sA->elements[i] != sB->elements[i]) {
                return 0;
                break;
            }
            else return 1;
        }
    }
}

int printStack(stack *s) {
    if (s->count == 0) printf("ERROR: Stack is empty!\n");
    else {
        int i;
        printf("The contents of the stack are:\n");
        for (i = s->count; i != 0; i--) printf("%d: %d\n", i - 1, s->elements[i - 1]);
    }
}

void test() {

    // 1.
    stack *sA = malloc(sizeof(stack));

    stack *sB = malloc(sizeof(stack));

    initialize(sA);
    initialize(sB);

    // 2.
    push(sA, 3);
    push(sA, 4);
    push(sA, 5);
    push(sA, 6);
    push(sA, 7);
    push(sA, 8);

    printf("Removed: %d\n", pop(sA));
    printf("Removed: %d\n", pop(sA));

    // 3.
    push(sB, 12);
    push(sB, 13);
    push(sB, 6);
    push(sB, 7);

    // 4.
    printStack(sB);

    // 5.
    printf("Removed: %d\n", pop(sB));

    // 6.
    push(sB, 8);
    push(sB, 9);

    // 7.
    printf("1 if stacks are equal: %d\n", compareStack(sA, sA));

    // 8.
    printf("1 if stacks are equal: %d\n", compareStack(sA, sB));

    // 9.
    printf("Removed: %d\n", pop(sA));
    printf("Removed: %d\n", pop(sA));
    printf("Removed: %d\n", pop(sA));
    printf("Removed: %d\n", pop(sA));

    // 10.
    free(sA);
    free(sB);

}

int main() {
    test();
}

这是输出:

Removed: 8523848
Removed: 8523848
The contents of the stack are:
3: 7
2: 6
1: 13
0: 12
Removed: 8523880
1 if stacks are equal: 1
1 if stacks are equal: 0
Removed: 8523848
Removed: 8523848
Removed: 8523848
Removed: -1

Process returned 1 (0x1)   execution time : 0.251 s
Press any key to continue.

关于push,应该是return它在元素数组中添加的位置。我目前不确定它 return 是否是正确的值,但这可能主要是因为我现在很头疼,而不是因为它特别难弄清楚。尽管如此,如果有人可以给我意见,我将不胜感激。

关于compareStack,我觉得我仍然对链表和堆栈的工作方式感到困惑。从输出看来它可能是正确的,因为它认为 sA 和 sB 不相等。但这可能只是因为我将它们的大小相互比较的第一点,而不是因为以下代码工作正常。

显然堆栈出了点问题,因为删除的消息和 sB 的最高元素不正确。

测试用例有点奇怪,但这是我需要使用的。

提前感谢您对我的代码和问题的任何帮助!

尝试以下更改

int push(stack *s, int value) {
    if (s->count < STACK_SIZE) {
        s->elements[s->count++] = value;
        return s->count - 1; // not sure whether this is right
    }
    else return -1;
}


int pop(stack *s) {
    if (s->count == 0) return -1;
    else return s->elements[--s->count];
}

int compareStack(stack *sA, stack *sB) {
    int i = 0;

    if (sA->count != sB->count) return 0;

    while ( i < sA->count && sA->elements[i] == sB->elements[i] ) ++i;

    return i == sA->count;
}

void printStack(stack *s) {
    if (s->count == 0) printf("ERROR: Stack is empty!\n");
    else {
        int i;
        printf("The contents of the stack are:\n");
        for (i = s->count; i != 0; i--) printf("%d: %d\n", i - 1, s->elements[i-1]);
    }
}