簡體   English   中英

在C中使用兩個堆棧實現一個隊列

[英]Implement a Queue using two Stacks in C

為什么我的代碼在運行時會被壓縮。 它說在Push()函數中傳遞不兼容的指針類型。 如何解決這個問題呢?

這是我在C語言中實現的代碼。這是一個簡短的概述,我是如何嘗試解決問題的。

  • 首先,我為Stack創建了一個結構
  • 為堆棧編寫了推入和彈出功能
  • 為隊列寫一個結構
  • 第一個堆棧用於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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM