2017-07-16 71 views
0

爲什麼我的代碼在運行時被破壞。它表示傳遞在Push()函數中傳遞的不兼容的指針類型。如何解決這個問題呢?使用C中的兩個堆棧實現隊列

這裏是我在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; 
    } 
    
+1

你爲什麼標籤這個C++? – user0042

+0

您需要在這裏傳遞指針的地址:'Push(&(Q-> S1),data);' – user0042

+0

C++與C具有向後兼容性,這可能就是原因。順便說一句,謝謝 –

回答

2

三個問題:

一)致電推 - 錯誤的參數類型:struct Stack **top預期不是struct Stack *top

二)調用流行 - 錯誤的參數類型: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