Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中使用两个堆栈实现队列_C_Data Structures_Stack_Queue - Fatal编程技术网

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

在C中使用两个堆栈实现队列,c,data-structures,stack,queue,C,Data Structures,Stack,Queue,为什么我的代码在运行时会被压碎。它表示在Push()函数中传递不兼容的指针类型。如何解决这个问题 下面是我用C语言实现的代码。下面是我如何尝试解决这个问题的简要总结 首先,我为堆栈创建了一个结构 为堆栈编写了Push和Pop函数 为队列编写了一个结构 第一个堆栈用于排队,第二个堆栈用于出列操作 #include <stdio.h> #include <stdlib.h> #include <limits.h> struct Stack { int

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

下面是我用C语言实现的代码。下面是我如何尝试解决这个问题的简要总结

  • 首先,我为堆栈创建了一个结构
  • 为堆栈编写了Push和Pop函数
  • 为队列编写了一个结构
  • 第一个堆栈用于排队,第二个堆栈用于出列操作

    #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;
    }
    
    #包括
    #包括
    #包括
    结构堆栈{
    int数据;
    结构堆栈*下一步;
    };
    结构堆栈*CreateStack(){
    返回NULL;
    }
    int isEmptyStack(结构堆栈*top){
    返回(top==NULL);
    }
    无效推送(结构堆栈**顶部,整数数据){
    结构堆栈*newNode=(结构堆栈*)malloc(sizeof(结构堆栈));
    如果(!newNode)
    返回;
    新建节点->数据=数据;
    新建节点->下一步=*顶部;
    *top=newNode;
    }
    int Pop(结构堆栈**顶部){
    结构堆栈*temp;
    int数据;
    如果(isEmptyStack(*顶部)){
    printf(“空堆栈。\n”);
    返回INT_MIN;
    }
    温度=*顶部;
    数据=(*顶部)->数据;
    *top=(*top)->下一步;
    免费(临时);
    返回数据;
    }
    结构队列{
    结构堆栈*S1;
    结构堆栈*S2;
    };
    结构队列*CreateQueue(){
    返回NULL;
    }
    无效队列(结构队列*Q,整数数据){
    推送(Q->S1,数据);
    }
    int出列(结构队列*Q){
    如果(!isEmptyStack(Q->S2)){
    返回Pop(Q->S2);
    }
    否则{
    而(!isEmptyStack(Q->S1)){
    推送(Q->S2,弹出(Q->S1));
    }
    返回Pop(Q->S2);
    }
    }
    int main(){
    结构队列*Q=CreateQueue();
    Q->S1=Q->S2=NULL;
    排队(Q,1);
    排队(Q,2);
    排队(Q,3);
    printf(“%d”,出列(Q));
    printf(“%d”,出列(Q));
    printf(“%d”,出列(Q));
    返回0;
    }
    
三个问题:

a) 调用推送-错误的参数类型:
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;
}
三个问题:

a) 调用推送-错误的参数类型:
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;
}

你为什么要标记这个C++?你需要传递指针的地址:<代码> PUP((q-> S1),数据);<代码>C++与C具有向后兼容性。这可能就是原因。顺便说一句,感谢“C++与C具有向后兼容性”,这并不完全正确。C和C++是不同的语言。为什么要标记这个C++?你需要传递指针的地址:<代码> PUP((q-> S1),数据);<代码>C++与C具有向后兼容性。这可能就是原因。顺便说一句,感谢“C++与C具有向后兼容性”,这并不完全正确。C语言和C++语言是不同的语言。