i am writing a code to create a binary tree but something is going wrong i tried to debug but could not find can anyone find it.
code that i have tried is as follow..
header files are..
#include< stdio.h>
#include< stdlib.h>
structure is..
struct node {
int data;
struct node *left;
struct node *right;
}
other declaration..
*head,*p=NULL;
int count=0;
create() function is..
void create(int m) {
if(count==0)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=m;
p->left=NULL;
p->right=NULL;
head=p;
count++;
}
else {
p=(struct node *)malloc(sizeof(struct node));
p->data=m;
p->left=NULL;
p->right=NULL;
}
}
main function is..
int main()
{
int n,i,m;
scanf("%d",&n);
for(i=0;i<n;i++) {
scanf("%d",&m);
if(i==0) {create(m);}
if(i>0) {
while(1) {
if(m < p->data)
p=p->left;
else
p=p->right;
if(p==NULL)
{
create(m);
break;
}
}
}
p=head;
}
printf("%d",p->left->data); //printing the data
return 0;
}
Think, when i = 1, then count is also 1,
if(m<p->data)
{
p=p->left;
}
what will be happened when p->left is a null? Now p is a null pointer, right? Then you did this...
if(p==NULL)
{
create(m);
break;
}
Lets see, what is happening in create(m). Now in create() else portion will work.
else
{
p=(struct node *)malloc(sizeof(struct node));
p->data=m;
p->left=NULL;
p->right=NULL;
}
It is also OK. Now you have a child of head. Reference of this child is in p. Right? But the mistake which you have done is,
p=head;
That means, what have you done is gone. Now there is no reference of child of head. p is keeping reference of head. See, head->left or head->right is always NULL whatever you keep in p. So you have to create a node where you find a NULL, not in p. It is not giving you any result.
Hope, it will help you. :)
Related
I'm not sure if I'd sound dumb, but still..I want to develop a program using linked list where I could add upto 5 Polynomials. The code I could come up with is..
struct node
{
int exp,coeff;
struct node *next;
} *start[5]={NULL};
void read()
{
struct node *current,*newnode;
int n,i,max,j;
printf("How many poly?");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nDetails of poly %d\n",i);
printf("Max degree:");
scanf("%d",&max);
for(j=max;j>=0;j--)
{
newnode=(struct node *)malloc(sizeof(newnode));
printf("\nEnter coeff of %d:",j);
scanf("%d",&newnode->coeff);
newnode->exp=j;
if(start[i]==NULL)
{
printf("a%d",start[i]);
start[i]=newnode;
newnode->next=NULL;
printf("%d,%d",start[i],start[i]->next);
}
else if(start[i]->next==NULL)
{
printf("b");
start[i]->next=newnode;
newnode->next=NULL;
printf("%d",start[i]);
}
else
{
printf("c");
current=start[i];
while(current->next!=NULL)
current=current->next;
current->next=newnode;
newnode->next=NULL;
printf("%d",start[i]);
}
}
}
}
void sum()
{
struct node *curr1,*curr2,*tmp;
int i,co,ex;
curr1=start[0];
while(curr1!=NULL)
{
for(i=1;i<5;i++)
{
co=0;
if(curr1==start[i])
tmp=start[i];
else
tmp=tmp->next;
curr2=tmp;
if(curr1->exp==curr2->exp)
{
if(co==0)
co=co+curr1->coeff+curr2->coeff;
else
co=co+curr2->coeff;
ex=curr1->exp;
break;
}
else if(curr1->exp>curr2->exp)
{
ex=curr1->exp;
if(co==0)
co=co+curr1->coeff;
else
break;
}
else
{
ex=curr2->exp;
if(co==0)
co=co+curr2->coeff;
else
break;
}
}
printf("%dX^%d",co,ex);
curr1=curr1->next;
}
}
int main()
{
read();
printf("result:\n");
sum();
getch();
return 0;
}
But on executing,only the first polynomial is getting read.ie, 'i' loop work only once. I tried to check my read function and notice the second condition was not working.ie,this one:
else if(start[i]->next==NULL)
{
printf("b");
start[i]->next=newnode;
newnode->next=NULL;
printf("%d",start[i]);
}
That print b was to check if program entered this block LOL.
Somebody help me with this please...
This part
newnode=(struct node *)malloc(sizeof(newnode)); // This will allocate space for
// a pointer to struct node
should be
newnode=(struct node *)malloc(sizeof(struct node)); // This will allocate space for
// a struct node
or better as suggested by #chux
newnode = malloc(sizeof *newnode); // This will allocate space for
// whatever newnode points to. In
// this case a struct node
Not sure it makes a difference but I would change:
start[5]={NULL};
into
start[5]={NULL, NULL, NULL, NULL, NULL};
I'm trying to make link list which represents polynomial.
Each node consists of coefficient,power of x,link to the next node
I have tried this program without passing arguments it worked .
but when when tried passing arguments the link list isn't being created.
pls help to rectify my program..
*****THIS IS MY PROGRAM WITH STRUCTURE POINTERS AS AN
ARGUMENT IN THE INSERT FUNCTION*****
#include<stdio.h>
#include<stdlib.h>
struct node
{
float coff;
int expo;
struct node *next;
};
struct node *start1=NULL,*current1=NULL;
void insert(struct node *start,struct node *current)
{
struct node *new_node;
new_node=(struct node*)malloc(sizeof(struct node));
if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
printf("\nEnter coefficient:");
scanf("%f",&new_node->coff);
printf("\nEnter power of x:");
scanf("%d",&new_node->expo);
new_node->next=NULL;
}
void show(struct node *start)
{
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
printf(" %.2fx^%d",ptr->coff,ptr->expo);
ptr=ptr->next;
}
}
void find(float scoff,int sexpo,struct node *start)
{
//scoff=search coefficient, sexpo=search exponent
int flag=0;
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
if(ptr->coff==scoff && ptr->expo==sexpo)
{
printf("your term found-> %.1fx^%d",ptr->coff,ptr->expo);
flag=1;
break;
}
else
ptr=ptr->next;
}
if(flag==0)
printf("\nSorry term couldn't be found!!");
else
printf("\nSearch success!!");
}
int main()
{
int c=1,ex=0;
float cf=0;
while(1)
{
insert(start1,current1);
printf("\nWant to continue(1/0)?");
scanf("%d",&c);
if(c==0)
break;
}
show(start1);
while(1)
{
printf("\nEnter coff and expo respectively:");
scanf("%f%d",&cf,&ex);
find(cf,ex,start1);
printf("\nWant to continue(1/0)?");
scanf("%d",&c);
if(c==0)
break;
}
return 0;
}
The problem is that you're modifying a local copy of the start and current pointers (i.e. the copies passed as the function arguments), thus not actually changing the linked list. You should instead modify the start1 and current1 pointers. There's no need for the insertion function to take start and current parameters as it can simply modify the global variables. (I should note that this is a bad design choice.)
More specifically, the problem is when initializing the list in
if (start==NULL)
{
start=new_node;
current=new_node;
}
you only modify the local copy that gets destroyed when it goes out of scope. So start1 and current1 are still NULL after the function returns.
Change the insert function to
void insert(struct node *start,struct node *current)
{
struct node *new_node;
new_node=(struct node*)malloc(sizeof(struct node));
if(start1==NULL)
{
start1=new_node; // You previously had start instead of start1
current1=new_node; // You previously had current instead of current1
}
else
{
current1->next=new_node;
current1=new_node;
}
printf("\nEnter coefficient:");
scanf("%f",&new_node->coff);
printf("\nEnter power of x:");
scanf("%d",&new_node->expo);
new_node->next=NULL;
}
Beware: after this modification, you can call insert() (i.e. no need for insert to have parameters) as the function will operate on the global variables. The other option---better, but more time-consuming---is to somehow initialize the linked list and then use the functions in their current form (except for minor modifications).
I need to sort the numbers that are entered in a list, but I am doing something wrong and it's sorting all of them except the first one.Any ideas how to fix this?
Here's my code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node* List;
void Add (struct node* p, int d)
{
struct node* q;
q=malloc(sizeof(struct node));
if (q==NULL)
printf("Not enaugh memory!");
else
{
q->data=d;
if(List==NULL)
{
q->next=NULL;
List=q;
}
else
{
struct node *ptr=List;
while((ptr->next!=NULL)&&(ptr->next->data>d))
{
ptr=ptr->next;
}
q->next=ptr->next;
ptr->next=q;
}
}
}
int main()
{
int n,i,a;
printf("How much numbers are you going to enter? ");
scanf("%d",&n);
for (i=1; i<=n; i++)
{
printf("\nEnter a number: ");
scanf("%d",&a);
Add(List,a);
}
printf("\nThe sorted numbers are: ");
struct node *ptr=List;
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->next;
}
printf("\n\n");
system("PAUSE");
return 0;
}
Thanks for the help in advance :-)
in add() function,
if(List==p)
this statement is true for all elements you insert to list since the call to add is,
Add(List,a);
so p=List. therefore the sorting code written in else part is not executed.
Also add statements to check for empty initial list.
You can use code similar to this,
void Add (int d)
{
struct node* q;
q=malloc(sizeof(struct node));
if (q==NULL)
printf("Not enaugh memory!");
else
{
q->data=d;
if(List==NULL)
{
q->next=NULL;
List=q;
}
else
{
struct node *ptr=List;
while((ptr->next!=NULL)&&(ptr->next->data>d))
{
ptr=ptr->next;
}
q->next=ptr->next;
ptr->next=q;
}
}
}
Since list is a global variable you dont need to pass it to Add() function. change the function call to
Add(a);
void Add (struct node* p, int d){
struct node* q;
q=malloc(sizeof(struct node));
if (q==NULL)
printf("Not enaugh memory!");
else{
q->data=d;
if(List==NULL || List->data < d){//modify this line
q->next= List;//and change NULL to List
List=q;
} else {
struct node *ptr=List;
while((ptr->next!=NULL)&&(ptr->next->data>d)){
ptr=ptr->next;
}
q->next=ptr->next;
ptr->next=q;
}
}
}
You always call Add with List as the first parameter, so it's alway true inside Add that (List==p). Consequently each new item is just inserted at the front of the list and there is no sorting at all.
EDIT 1
A good practice would be sending a list to the Add routine as a parameter. Or, if you want to keep it external, just don't give it to Add at all and test if(List == NULL)
void Add( int d)
{
// ... alloc 'q' and fill q->d here, then:
if(List == NULL)
{
q->next = NULL;
List = q;
}
else
{
struct node *b; // put q after b
for( b = List; b->next != NULL; b = b->next)
if( b->next->data >= d)
break;
q->next = b->next;
b->next = q;
}
}
EDIT 2
Example of transferring the list to a function by parameter
void Add( struct node **n, int d)
{
// ... alloc 'q' and fill q->d here, then:
while( *n != NULL && (*n)->data < d)
n = & (*n)->next;
q->next = *n;
*n = q;
}
int main()
{
// ...
Add( &List, a);
// ...
}
I am getting error while executing this. Compiler does not give any error but when executed it give random output.
What am i doing is taking input from user & storing them to linked list & then implementing insertion sort.
( I am able to call the subroutine insertion sort, so I guess the problem lies within subroutine only)
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
};
typedef struct node *NODEPTR;
NODEPTR getnode();
NODEPTR insertionSortLinkedList(NODEPTR head);
int main()
{
int i,n,temp;
NODEPTR head,lptr,prevlptr;
printf("No of input integers to be sorted\n");
scanf("%d",&n);
if (n<2){printf("n should be atleast 2 \n");return 0;}
printf("\nType space-separated array of %d integers\n",n);
scanf("%d",&temp);
head=getnode();
head->info=temp;
head->next=NULL;
prevlptr=head;
for (i=0;i<n-1;i++)
{
scanf("%d",&temp);
lptr=getnode();
lptr->info=temp;
prevlptr->next=lptr;
lptr->next=NULL;
prevlptr=lptr;
}
head=insertionSortLinkedList(head);
lptr=head;
while(lptr!=NULL)
{
printf("%d ",lptr->info);
prevlptr=lptr;
lptr=lptr->next;
free(prevlptr);
}
return 0;
}
NODEPTR getnode()
{
NODEPTR p;
p=(struct node*)malloc(sizeof(struct node));
return p;
}
NODEPTR insertionSort(NODEPTR head)
{
NODEPTR listptr,tempptr,prevptr,prevtempptr;
prevptr=head;
listptr=prevptr->next;
while(listptr!=NULL)
{
while (listptr->info < prevptr->info)
{
prevptr->next=listptr->next;
tempptr=head;
prevtempptr=head;
while(tempptr->info <= listptr->info)
{
prevtempptr=tempptr;
tempptr=tempptr->next;
}
if(tempptr->info == prevtempptr->info)
{
listptr->next=head;
head=listptr;
}
else
{e
listptr->next=prevtempptr->next;
prevtempptr->next=listptr;
}
listptr=prevptr->next;
if (listptr==NULL)
break;
}
prevptr=prevptr->next;
if (prevptr=NULL)
break;
listptr=prevptr->next;
}
return(head);
}
What is wrong with my code and how do I fix it?
The 6th line form last
if (prevptr=NULL)
it should be
if (prevptr==NULL)
Single "=" does the assignment instead of comparison.
Sometime back I asked a question about linked list and got nice replies...Now I've written a new code using the suggestions but I've run into an error. The code is:
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *mknode()
{
return malloc(sizeof(node));
}
void create(node* h, int num)
{
int i;
node *temp=h;
for(i=0;i<num;i++)
{
temp->data=i;
if(i==(num-1))
temp->next=NULL;
else
temp->next=mknode();
temp=temp->next;
}
}
node* add_end(node *h,int num)
{
node *temp;
if(h==NULL)
{
h=mknode();
temp=h;
create(h,num);
}
else
{
temp=h;
while(h!=NULL){
h=h->next;}
h=mknode();
create(h,num);
}
return temp;
}
void display(node *h)
{
node *temp=h;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
int main()
{
node *head=NULL;
int num;
scanf("%d",&num);
head=add_end(head,num);
head=add_end(head,num);
display(head);
//printf("%d",list_len(head));
free(head);
return 0;
}
Now since I've called add_end twice for an input of 3 the output should be
0->1->2->0->1->2->
But instead I'm getting
0->1->2->
I've checked this much that the FOR loop inside create function is running 2n times for an input of n.
So the problem is that display function encounters a NULL but I can't figure out where in the code is it happening.
All help appreciated.
Thanking in advance
-user1614886
In add_end() you do not link the nodes correctly.
[blah blah]
else
{
temp=h;
while(h!=NULL){
h=h->next;}
h=mknode();
create(h,num);
}
You advance h until it is NULL but you should only move until h->next == NULL and append your next list there.