Amazon Affiliate

Thursday 31 May 2012

CREATING CIRCULAR HEADER LINKED LIST....

// CREATING CIRCULAR HEADER LINKED LIST..


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

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

     class Circular
     {
    private:
        int i; // Represents number of nodes in the list
        int number;
        link *start, *new1;

    public:
         void insertion(link *);
         void create_circular_list(link *);
         void display(link *);

      };

// Function create a circular header linked list

void Circular :: create_circular_list( link *node)
 {

   node = start;      // Point to the header node in the list
   node->next = (link* ) malloc(sizeof(link));
   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;
       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 in circular header linked
    void Circular :: insertion(link *node)
    {
     node = start;
     int count = node->info;
     node = node->next;

     int node_number = 0;
     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+1) == 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;
          count--;
           }
        else
        {
          node = node->next;
          count--;
        }
           node_number ++;
         }
         if (count == 0)
          {
        node = start;  // Points to header node
        node->info = node->info+1;
          }
    }

// Display the list

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

    node = node->next;

    while (count)
      {
    cout<<"\n "<<node;
    cout<<"  "<< node->info;
    node = node->next;
    count --;
      }
    }

// Function main

     void main()
     {
        Circular Circular_List;
        link *node = (link *) malloc(sizeof(link));
        Circular_List.create_circular_list(node);
        cout<<"\n Before inserting a node list is as follows:\n";
        Circular_List.display(node);
        Circular_List.insertion(node);
        cout<<"\n After inserting a node list is as follows:\n";
        Circular_List.display(node);

     }

No comments:

Post a Comment