2017-07-16 120 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