Hello;
I have little experience in c++ and I should implement insertion sort algorithm using linked list. I wrote a working code however I should have use friends for having the list elements from a seperate class and use templates for sorting arbitrary data elements. Can anybody help me how to use friends and templates??
Here is my code:

#include <iostream.h>
#include <stdlib.h>
struct listNode
{
   int data;
   listNode * next;
};


void enterList ( listNode *& );
void sortList ( listNode *& );
void printList ( listNode * );
#define test(n) if ( n == NULL ) exit ( 1 );
int main ()
{
   listNode * front = NULL;

   enterList ( front );
   sortList ( front );
   printList ( front );
   
    system("PAUSE");
    return 0;
   

}

void enterList ( listNode *& l )
{

   cout << "Enter your list " <<endl;

   int d;
   char ch;
   listNode * c = NULL; //current node


   do
   {
       cout << "Enter a number: " ;
       cin >> d;
       if ( l == NULL )
       {
           l = new listNode;
           test(l)
           l -> data = d ;
           l -> next = NULL;
           c = l;
       }
       else
       {
           c -> next = new listNode;
           test(c->next)
           c = c -> next;
           c -> data = d;
           c -> next = NULL;
       }
       cout << "Enter another ( y or n )? " ;
       cin >> ch;
   }while ( ch != 'n' );


 }

void sortList ( listNode *& l )
{

   listNode * t = NULL; //temporary
   listNode * s = NULL; //sorted list
   listNode * f = NULL; //follower node
   listNode * st = NULL;


   while ( l != NULL )
   {

       t = l;
       l = l -> next;
       t -> next = NULL;

       if ( s == NULL )
           s = t;
       else
       {



           if ( s -> data > t -> data )
           {
               t -> next = s;
               s = t;
           }
           else
           {
               f = s;
               st = s -> next;
               while ( st != NULL )
               {

                   if ( st -> data > t -> data )
                       break;
                   f = st;
                   st = st -> next;

               }
               f -> next = t;
               t -> next = st;
           }
       }
   }
   l = s;

}
void printList ( listNode * p )
{
   int x = 1;
   while ( p != NULL )
   {     
       cout << x << "-)" << p -> data << endl;
       p = p -> next;
       x++;
   }
}

Recommended Answers

All 3 Replies

Use code tags, as the quick reply box suggests.

Have you tried using templates? (The syntax can be hard to remember.) Why don't you search for some template tutorials or read a C++ book?

friend functions are allowed access to private data members of the respective classes. Lets say you had this:

class listNode
{
  int data;
  listNode * next;
};

Then let's say you want to let listNode objects display data on the screen. Remember, by default all member variables and functions of classes have private access which cannot be accessed outside of other class methods. So you have two options. You can create a public accessor function like:

class listNode
{
  int data;
  listNode * next;

  public:
    int getData();
};

and every time you want to display data for a given object you would call getData() on the listNode object OR you could overload the << operator so you could use the more typical output syntax. However, << is a member of the ostream class so it can't access private member variables of objects passed to it-----unless it is a friend of the class:

class listNode
{
   int data;
   listNode * next;
  
   public:
      friend ostream & operator<<(ostream &, const listNode &);
};

Now the << operator of the ostream class can now access data from any listNode object it is sent. So if you overloaded the << operator like this:

ostream & operator<<(ostream & os, const listNode & rhs)
{
   os << rhs.data;
   return os;
}

and you had a valid listNode object by the name of firstNode then you could do this:

cout << firstNode;

instead of this:

firstNode.getData();

Thank you very much for your help. I get it;)

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.