使用两个堆栈的队列实现 - 指针不兼容错误

时间:2013-03-23 05:18:53

标签: c pointers compiler-errors

程序任务是使用两个堆栈实现一个队列。我知道指针需要作为参数传递 - pop(**&**headA)。但是当我这样做时,我得到一个'不兼容的指针'编译时错误。因此,当我执行如下所示的代码时,我会遇到逻辑错误。它没有按照我想要的方式运行。

#include<stdio.h>

typedef struct node{
     int data;
     struct node *link;
}node;
node *headA = NULL;
node *headB = NULL;


void push(node *,int);
int pop(node *);
void display(node *);
void enQ();
void dQ();

main(){
   int choice;
         system("cls");
         printf("\n\tWelcome to Queue implementation using two stacks.\n");         
         do{

         /* Menu */      

         printf("\n\tWhat would you like with your queue?\n\n");
         printf("1. Insert an element at the front (EnQ)\n2. Delete the last element (DQ)\n3. Exit program\n\nEnter your desired choice with the corresponding serial number :\n\n");
         scanf("%d",&choice);
         system("cls");
         switch(choice){

                        case 1: enQ();
                                break;
                        case 2: dQ();
                                break;
                        case 3: printf("\n\tSorry to see you go, hope to see you back soon!\n");

                                exit(0);
                        default: printf("\n\tSorry, That option is unavailable. Try again!\n\n"); 

         }
         }while (choice >= 0);
}


void enQ(){
     int add;
     int x,y;
     printf("\n\tEnter the item\n");
     scanf("%d",&add);
     if(headB == NULL){ 
       push(headA, add);
     }
     else{
          while(headB != NULL){
            x = pop(headB);
            push(headA, x);
          }
          push(headA, add);
     }
    while(headA != NULL){
              y = pop(headA);
              push(headB, y);
    }
    display(headB); 
}

void dQ(){
   int del;
   int x,y;
   if(headB == NULL){
           printf("Queue is empty.");
   }
   else{
      while(headB != NULL){
                  x = pop(headB);
                  push(headA, x);
      }
      del = pop(headA);
      printf("\n\tThe element deleted is : %d ", del);
      while(headA != NULL){
                   y = pop(headA);
                   push(headB, y);
      }
      display(headB);
   }
}


void push(node *head, int add){
   node *last_node;
   last_node = (node*)malloc(sizeof(node));
   last_node->data = add;
   last_node->link = head;
   head = last_node;
}

int pop(node *head){
   node *last_node;
   int flag = 0;
   if(head==NULL)
      return flag;
   else{
      last_node = head;
      flag = last_node->data;
      head = head->link;
      free(last_node);
      return flag;
   }
}

void display(node *head){
   node *my_stack;
   if(head == NULL)
       printf("\n\tStack is empty\n");
   else{
       my_stack = head;
       printf("\n\tThe elements of the queue are : \n\n\t");
       while(my_stack != NULL){
            printf("%d\t", my_stack->data);   
            my_stack = my_stack->link;
       }
   }
}

1 个答案:

答案 0 :(得分:0)

int pop(node *head);头部的类型是什么? node *headA = NULL; headA的类型是什么? y = pop(&headA);&amp; headA的类型是什么?

由此可以清楚地知道,如果你想将node **传递给pop,你需要声明pop有一个node **参数:int pop(node **head);

int pop(node **head){
    node *last_node;
    int flag = 0;
    if(*head==NULL)
       return flag;
    else{
       last_node = *head;
       flag = last_node->data;
       *head = last_node->link;
       free(last_node);
       return flag;
    }
}

您也希望对push进行此更改,因为这会发生变化,并且您希望在推送返回后看到这些更改:void push(node **head, int add);

void push(node **head, int add){
    node *last_node;
    last_node = malloc(sizeof *last_node); // don't cast malloc. #include <stdlib.h> and use a C compiler rather than a C++ compiler.
    last_node->data = add;
    last_node->link = *head;
    *head = last_node;
}