Right now Im still beggining to learn the idea of pointers,basically I just want to ask a question about the code I have found, I mean I want to know what is happening right here...the comments on the code serve as my question just look at below...

#include<iostream.h>
 
struct node          
{
	int data;
     node *left;
     node *right;
};
node *root;

class tree
  {
     public :   
         tree()
            {
               
        	    root=NULL;
            };           
       void insert();
       void display_inorder(node *);  
       void display_inorder() 
        {  
          display_inorder(root);   
          
         };
      };

void tree::insert()
 {
  cout<<"\nEnter 0 to exit\nEnter a number to insert\n";
  node *nn=new node;
  nn->left=NULL;
  nn->right=NULL;
  cin>>nn->data;
  
  if(nn->data == 0)  
  {
   cout<<"\n\t\t\tProgram will now exit....\n";	
   system("PAUSE");		  
   exit(1);  			  
  }
                 
 if(root==NULL)
  {
  root=nn;
 
  }
  else     
  {   
  node *prev=root;          
  node *current=root;                 
                 
  while(current!=NULL)
 {
   prev=current;  
   
   if(nn->data >= current->data)  //can someone explain me in this scene
     
     current=current->right;   
      
     else   
     current=current->left;    
          
 }
                 
  if(nn->data >= prev->data)  //can someone also tell me what is happening right here  please
     prev->right=nn;
     else
      prev->left=nn;
  }
 }//insert() 
  
void tree::display_inorder(node *n)
  {
     
   if(n!=NULL)
   { 
   display_inorder(n->left);     
   cout<<n->data<<" ";
   display_inorder(n->right);    
   }
}   
 
int main()
      {
       tree t;
       int num; 
        
       
      while(num!=0)  
         {                   
          t.insert();
          cout<<"\nBinary Tree -> ";
          t.display_inorder(); 
          cout<<"\n";  
        }//while
                  
      exit(0);  
                  
   system("PAUSE");
   return 0;
 }//main

the part in the if statement(there are 2 of these) under insert() a simple explanation please..Any help can do!just curious about the program :cool:

Recommended Answers

All 3 Replies

It looks like you're confused about the arrow operator. This:

pointer_to_struct->member

is equivalent to this:

(*pointer_to_struct).member

It's just a convenient notation for merging a dereference and member access into one.

Q: What is a pointer?
A: A pointer 'points' to an address in your computer's memory ...

A pointer can not only point to a variable, but also to an object, a struct and much more other types of data ...

If you think in the following way: "A variable is stored in your computer's memory, but also functions, structs, unions, enumerations, objects, and so on are stored in your computer's memory, so a pointer can 'point' / 'refer' to this ... "

Hope this solves your problem !
(Together with Narue's explanation)

ok thanks...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.