c - 在 C 中使用两个堆栈实现一个队列

标签 c data-structures stack queue

为什么我的代码在运行时会崩溃。它说在 Push() 函数中传递不兼容的指针类型。如何解决这个问题?

这是我在 C 中实现的代码。这是我如何尝试解决问题的快速总结。

  • 首先我为 Stack 创建了一个结构
  • 为堆栈编写 Push 和 Pop 函数
  • 为队列编写了一个结构
  • EnQueue 的第一个堆栈和 DeQueue 操作的第二个堆栈。

    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    
    struct Stack {
        int data;
        struct Stack *next;
    };
    
    
    struct Stack *CreateStack () {
        return NULL;
    }
    
    int isEmptyStack(struct Stack *top) {
        return (top == NULL);
    }
    
    void Push(struct Stack **top, int data) {
        struct Stack *newNode = (struct Stack*) malloc(sizeof(struct Stack));
        if(!newNode)
            return;
        newNode->data = data;
        newNode->next = *top;
        *top = newNode;
    }
    
    int Pop(struct Stack **top) {
        struct Stack *temp;
        int data;
    
        if(isEmptyStack(*top)) {
            printf("Empty Stack.\n");
            return INT_MIN;
        }
    
        temp = *top;
        data = (*top)->data;
        *top = (*top)->next;
        free(temp);
        return data;
    }
    
    struct Queue {
        struct Stack *S1;
        struct Stack *S2;
    };
    
    struct Queue *CreateQueue() {
        return NULL;
    }
    
    void EnQueue(struct Queue *Q, int data) {
        Push(Q->S1, data);
    }
    
    int DeQueue(struct Queue *Q) {
        if(!isEmptyStack(Q->S2)) {
            return Pop(Q->S2);
        }
        else {
            while(!isEmptyStack(Q->S1)) {
                Push(Q->S2, Pop(Q->S1));
            }
            return Pop(Q->S2);
        }
    }
    
    int main() {
        struct Queue *Q = CreateQueue();
        Q->S1 = Q->S2 = NULL;
        EnQueue(Q, 1);
        EnQueue(Q, 2);
        EnQueue(Q, 3);
    
        printf("%d ", DeQueue(Q));
        printf("%d ", DeQueue(Q));
        printf("%d ", DeQueue(Q));
    
        return 0;
    }
    

最佳答案

三个问题:

a) 调用 Push - 错误的参数类型:struct Stack **top 应该不是 struct Stack *top

b) 调用 Pop - 错误的参数类型:struct Stack **top 应该不是 struct Stack *top

c) 队列 *CreateQueue - 未分配内存

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

struct Stack {
    int data;
    struct Stack *next;
};


struct Stack *CreateStack () {
    return NULL;
}

int isEmptyStack(struct Stack *top) {
    return (top == NULL);
}

void Push(struct Stack **top, int data) {
    struct Stack *newNode = (struct Stack*) malloc(sizeof(struct Stack));
    if(!newNode)
        return;
    newNode->data = data;
    newNode->next = *top;
    *top = newNode;
}

int Pop(struct Stack **top) {
    struct Stack *temp;
    int data;

    if(isEmptyStack(*top)) {
        printf("Empty Stack.\n");
        return INT_MIN;
    }

    temp = *top;
    data = (*top)->data;
    *top = (*top)->next;
    free(temp);
    return data;
}

struct Queue {
    struct Stack *S1;
    struct Stack *S2;
};

struct Queue *CreateQueue() {

    struct  Queue  *newNode = (struct Queue *) malloc(sizeof(struct  Queue ));

    return newNode;
}

void EnQueue(struct Queue *Q, int data) {
    Push(&Q->S1, data);
}

int DeQueue(struct Queue *Q) {
    if(!isEmptyStack(Q->S2)) {
        return Pop(&Q->S2);
    }
    else {
        while(!isEmptyStack(Q->S1)) {
            Push(&Q->S2, Pop(&Q->S1));
        }
        return Pop(&Q->S2);
    }
}

int main() {
    struct Queue *Q = CreateQueue();
    Q->S1 = Q->S2 = NULL;
    EnQueue(Q, 1);
    EnQueue(Q, 2);
    EnQueue(Q, 3);

    printf("%d ", DeQueue(Q));
    printf("%d ", DeQueue(Q));
    printf("%d ", DeQueue(Q));

    return 0;
}

输出:

1 2 3

关于c - 在 C 中使用两个堆栈实现一个队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45127513/

相关文章:

C信号量奇怪的优先行为

c - 这两条指令在下面的函数中做了什么?

c - libjpeg 输出缩放

c - 在 C 中声明函数会给出预期的错误 ';' 、 ',' 或 ')'

algorithm - 使用动态数组处理哈希表中的冲突

c++ - 字符串变成链表然后检查括号平衡

ios - 优化一组 UIImages

java - 我需要一个 trie 样式的数据结构来存储自定义类的附加信息

arrays - 如何在 Swift 中的方向数组缩减挑战中正确实现堆栈

c++ - LinkedStack 及其模板类