【问题标题】:Implement a Queue using two Stacks in C在 C 中使用两个堆栈实现一个队列
【发布时间】:2017-12-21 00:05:24
【问题描述】:

为什么我的代码在运行时会崩溃。它说在 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;
    }
    

【问题讨论】:

  • 你为什么要标记这个c++?
  • 这里需要传递指针的地址:Push(&amp;(Q-&gt;S1), data);
  • C++ 与 C 具有向后兼容性。可能这就是原因。顺便说一句谢谢
  • “C++ 向后兼容 C” 这并不完全正确。 C 和 C++ 是不同的语言。

标签: c data-structures stack queue


【解决方案1】:

三个问题:

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

【讨论】:

    猜你喜欢
    • 2014-04-21
    • 2010-10-15
    • 2010-09-09
    • 2014-12-23
    • 1970-01-01
    • 2021-09-09
    • 2018-06-14
    • 2012-09-02
    • 1970-01-01
    相关资源
    最近更新 更多