【问题标题】:Queue Implementation using two stacks-Pointers Incompatibility Error使用两个堆栈的队列实现-指针不兼容错误
【发布时间】:2013-03-23 05:18:52
【问题描述】:

程序任务是使用两个堆栈实现一个队列。我知道指针需要像这样作为参数传递 - 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;
       }
   }
}

【问题讨论】:

    标签: c pointers compiler-errors


    【解决方案1】:

    int pop(node *head);什么是头部类型? node *headA = NULL;headA是什么类型的? y = pop(&amp;headA);&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;
    }
    

    【讨论】:

    • @user2201545 很高兴为您服务。你在看哪本书?
    • Michael T. Goodrich 和 Roberto Tamassia - 算法设计。
    • @User2201545 不,我的意思是......你在读哪本书来学习 C?
    • 嗯,我很久以前从 Yashwant Kanetkar 的一本名为“Let us C”的书中学习了 C。然而,那是很久以前的事了。不过,我现在没有为 C 阅读任何内容。
    • 那是一本可怕的书。你自己看;阅读名为“应避免的东西”here 的部分。
    猜你喜欢
    • 2010-10-15
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 2018-06-14
    • 2014-12-23
    • 1970-01-01
    • 2021-07-08
    相关资源
    最近更新 更多