We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

linked list

#include<iostream>
#include<conio.h>

using namespace std;

struct node
{
    int info;
    node *left,*right;
};

int main()
{
    struct node *root;
    root=NULL;
    //root->info=NULL;
    //root = (struct node*)malloc(sizeof(struct node));
    if(root==NULL) //cout<<root->info;
    cout<<root->info<< " "<<root->left<<" "<<root->right<<"\n";
    getch();
    return 0;


}

why it is giving an error?

3
Contributors
3
Replies
7 Hours
Discussion Span
9 Months Ago
Last Updated
4
Views
Question
Answered
shanki himanshu
Junior Poster
130 posts since Dec 2010
Reputation Points: 37
Solved Threads: 1
Skill Endorsements: 0

are you sure you posted in the right forum, the code is in C++ yet this is the C forum and you forgot to post the error messages
Now in a glance, 1 error I see is you should initialize the following variables inside the structure as follows

struct node *left; 
struct node *right
zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

oh yes..my mistake..this is C forum.sorry
it gives run time error.

shanki himanshu
Junior Poster
130 posts since Dec 2010
Reputation Points: 37
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 9 Months Ago by zeroliken

This is also incorrect:

> if(root==NULL) //cout<<root->info;
> cout<<root->info<< " "<<root->left<<" "<<root->right<<"\n";

You are trying to access the info member of root, which is NULL (due to your if statement); this will cause the program to crash.

It is best practice to always wrap statements in curly brackets to prevent unwanted behavior.

MonsieurPointer
Junior Poster
143 posts since Jun 2011
Reputation Points: 45
Solved Threads: 15
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0898 seconds using 2.7MB