Amazon Affiliate

Thursday 31 May 2012

CREATING HEADER LINKED LIST....

// CREATING HEADER LINKED LIST....


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

  struct link
    {
      int info;
      struct link *next;
    };

     class Header
     {
    private:
        int i;
        int number;
        link *start, *new1;

    public:
         void insertion(link *);
         void create_header_list(link *);
         void Traverse (link *);
      };

// Function to create a header linked list

  void Header :: create_header_list( link *node)
     {

   start->next = NULL;  // Empty list

   node = start;      // Point to the header node of the list
   node->next = (link* ) malloc(sizeof(link)); // Create header node
   i = 0;
   cout<<"\n Input choice n for break: ";
   char ch = getche();
   while(ch != 'n')
     {
       node->next = (link* ) malloc(sizeof(link));
       node = node->next;
       cout<<"\n Input the node :"<<(i+1)<<" : ";
       cin>>node->info;
       node->next = NULL;
       cout<<"\n Input choice n for break: ";
       ch = getche();
       i++;
     }
     cout<<"\n Total nodes = "<< i;
     node = start;
     node->info = i; // Assign total number of nodes to the header node
   }

// Inserting a node
    void Header :: insertion(link *node)
    {
     node = start;
     int count = node->info;
     node = node->next;

     int node_number = 1;
     int insert_node;

     cout<<"\n Input node number you want to insert:";
     cout<<"\n Value should be less are equal to the";
     cout<<"\n number of nodes in the list: ";
     cin >> insert_node;

     while(count)
      {
        if((node_number) == insert_node)
           {
         new1 = (link* ) malloc(sizeof(link));
         new1->next = node->next ;
         node->next = new1;
         cout<<"\n Input the node value: ";
         cin>>new1->info;
         node = node->next;
           }
        else
        {
          node = node->next;
          count--;
        }
           node_number ++;
         }
         node = start;
         node->info = node->info+1;
    }

// Traversing the header list

  void Header :: Traverse(link *node)
    {
    node = start;
    int count = node->info;

    node = node->next;
    cout<<"\n After Inserting a node list is as follows:\n";
    while (count)
      {
    cout<<"\n "<<node;
    cout<<"  "<< node->info;
    node = node->next;
    count --;
      }
    }

// Function main

     void main()
     {
        Header Header_mode;
        link *node = (link *) malloc(sizeof(link));

        Header_mode.create_header_list (node);
        Header_mode.Traverse (node);
        Header_mode.insertion (node);
        Header_mode.Traverse (node);

     }

No comments:

Post a Comment