C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 23rd, 2014, 08:10 AM
solstice solstice is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2014
Posts: 1 solstice User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 19 sec
Reputation Power: 0
General - Please help me out with this Binarty Tree C code

#include<stdio.h>
#include<stdlib.h>

typedef struct b_node bnode;

struct b_node{
bnode *left;
bnode *right;
int id;
};

typedef bnode* bst;

bst readVal()
{
int num;
int ctr=1;
bnode *cur=(bnode*)malloc(sizeof(bnode));
cur=NULL;
bst top=cur;
fscanf(stdin,"%d",&num);
while(num!=0)
{
bnode *newn=(bnode*)malloc(sizeof(bnode));
newn->id=num;
newn->left=NULL;
newn->right=NULL;
next:
if(cur==NULL)
{

cur=newn;

printf("\nFirst node created\n");
free(newn);
fscanf(stdin,"%d",&num);
continue;
}
else
{
printf("\nNode %d Creating ",ctr);
cur=top;
if(num>cur->id)
cur=cur->right;
else
cur=cur->left;
goto next;
}

fscanf(stdin,"%d",&num);
}
printf("\nTree created\n");
return top;
}

int countnodes(bnode *tree)
{
if(tree==NULL)
return 0;
else
return 1+countnodes(tree->left)+countnodes(tree->right);
}

int main()
{
bnode *tree=(bnode*)malloc(sizeof(bnode));
tree=readVal();
int num;
num=countnodes(tree);
printf("No. of nodes : %d ",num);
}



I am getting segmentation fault after the creation of the first node.

Reply With Quote
  #2  
Old February 19th, 2015, 11:51 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15
This is a bit weird:
bnode *cur=(bnode*)malloc(sizeof(bnode));
cur=NULL;

You're allocating memory and then never using it.

Besides that, as far as I can see, you're creating newn, then setting cur to point to it, then freeing it, then setting cur to top, which is still pointing at NULL. Which is bad.
__________________
There is no such thing as C/C++, you either program C or C++

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - Please help me out with this Binarty Tree C code


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap