Hello all...this is my first visit to this site.... anywayz i really need help in this...it's stupid maybe...but am not really into c++ ....anwayz this is a linked list...i just need a function to print a certain node.. like print(0); which prints the data in node 0..... here is the code plz help :D
:

#include<iostream.h>
enum error_code { success, fail, overflow, underflow, range_error};
//struct definition
template <class node_entry>
struct node
{ node_entry entry;
  node<node_entry>  *next;
   
  node( );
  node (node_entry &, node<node_entry> * link= NULL);
  
};//struct

//class definition
template <class node_entry>
class list
{ private:
  int count;
  node<node_entry> * head;
  node<node_entry>  * set_position(int position)const;  //locate List position;
  public:
        list( );
        error_code  insert (int  position,node_entry &x) ;
        
};

//*********************************************************
//***********   functions definitions **********************

//node default constructor
template <class node_entry>
node<node_entry>::node()
{ next = NULL;}

//node overloading constructor
template <class node_entry>
node<node_entry>::node (node_entry & x, node<node_entry> * link)
{ next = link;
   entry = x;
}
    
//list default constructor
template <class node_entry>
list<node_entry>::list()
{ head = NULL; }

//set position function
template < class node_entry>
node<node_entry>  * list<node_entry> :: set_position(int position) const   
/* pre: position is a valid position in the list,    0 position<count
    post: return a pointer to the Node in the position.  */
{ node<node_entry> * q = head;
   for (int I=0; I<position; I++)        
        q = q-> next;
  return q;
}

//insert fuction 
template <class node_entry>
error_code list<node_entry>::insert(int position,node_entry & x) 
{
 
  if (position < 0 || position > count)     //check range erro
     return range_error;
  node<node_entry> *new_node, *previous, *following;
    if (position>0)                        // if the addition is not in the first position
  {previous = set_position(position-1) ;             
     following = previous -> next;
   }
  else  following = head;        //if the insertion is in the first position
  new_node = new node<node_entry>(x,following);
   if (new_node ==NULL)   //checking for overflow
         return overflow;
    if(position ==0)          //set the pointer head to the newly created node
         head = new_node;
  else
     previous -> next = new_node;
     count++;
     return success; 
}
 

void main()
{ list <int> l;
  int c=4; 
  l.insert(0,c);
}

all you have to do is iterate through the linked list, counting the nodes as you go, until you reach the node you want. Since you wrote all the code you posted that should not be difficult for you to write a method to do that.

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.