0

The below code is giving run time error after displaying two nodes. There must be some problem in display() or push(); pop() and insert() function are working fine. (I checked it separately.) We insert nodes until the value of their child is not equal -1.

#include<stdio.h>
#include<malloc.h>
struct node
{
   int data;
   struct node *left;
   struct node *right;
};
typedef struct node list;
list queue[20];
int back=-1,front=0;
void push(list x)
{
    if(back-front==1)
       printf("queue full");
    else
    {
        if(back==19)
           back=1;
        else
            back++;
        queue[back]=x;
    }
}
list pop()
{
/*if(back-front==1)
  printf("queue empty");
  else
    {*/
   list x=queue[front];
        if(front==19)
           front=1;
        else
           front++;
        return x;
    //}

}
void insert(list *ptr,int x)
{
    ptr->data=x;
    int p,q;
    scanf("%d",&p);
    scanf("%d",&q);
    if(p!=-1)
    {
        ptr->left=(list *)malloc(sizeof(list));
        insert(ptr->left,p);
    }
    else
        ptr->left==NULL;
    if(q!=-1)
    {
        ptr->right=(list *)malloc(sizeof(list));
        insert(ptr->right,q);
    }
    else
        ptr->right==NULL;
}
void display(list *ptr)
{
    push(*ptr);
    /*printf("%d",queue[back].data);
    printf("%d",(queue[back].left)->data);
    printf("%d",(queue[back].right)->data);*/
    while(front<=back)
    {
        list x=pop();
        printf("%d\n",x.data);

        if(x.left!=NULL)
            push(*(x.left));

        if(x.right!=NULL)
          push(*(x.right));

    }
}

int main()
{
    int x;
    scanf("%d",&x);
    list *head=(list *)malloc(sizeof(list));
    insert(head,x);
    display(head);
    return 0;
}
Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
Aman Jagga
  • 301
  • 5
  • 15
  • 2
    Have you tried using a debugger? – 1'' Aug 28 '13 at 18:38
  • Also, can we see 1) what the error itself is and 2) the input that you gave that caused it? – Dennis Meng Aug 28 '13 at 18:40
  • You will have name clashes with std::list by naming your struct "list". – Étienne Aug 28 '13 at 18:56
  • for every input the same error in coming.Its a run time error which the compiler is not able to detect[segementation fault] – Aman Jagga Aug 28 '13 at 18:57
  • Étienne--but when there is clash local defination is given the priority.Moreover i have already implemented dfs with same notation and same insert function with no issues – Aman Jagga Aug 28 '13 at 18:59
  • But anybody reading list in your code would except it to be std::list, making the code unreadable. You could just change add one letter for example. – Étienne Aug 28 '13 at 19:07
  • While an interesting topic, don't derail. The bottom line is this code needs the **serious** attention of a debugger and a 3rd party code review. I count three instances of either undefined behavior or outright logic errors. VTC to move this [codereview](http://codereview.stackexchange.com). – WhozCraig Aug 28 '13 at 19:14

1 Answers1

3

Your insert is not fine. Check again.

void insert(list *ptr,int x)
{
    ptr->data=x;
    int p,q;
    scanf("%d",&p);
    scanf("%d",&q);
    if(p!=-1)
    {
        ptr->left=(list *)malloc(sizeof(list));
        insert(ptr->left,p);
    }
    else
        ptr->left==NULL; // <<== should be =, not ==
    if(q!=-1)
    {
        ptr->right=(list *)malloc(sizeof(list));
        insert(ptr->right,q);
    }
    else
        ptr->right==NULL; // <<== should be =, not ==
}

Note: this is by-far not the only problem, but it is a solid start. And don't cast malloc() in C.

Community
  • 1
  • 1
WhozCraig
  • 65,258
  • 11
  • 75
  • 141