Amazon Affiliate

Thursday 31 May 2012

DELETING FIRST NODE FROM A SIMPLE LINKED LIST...

// DELETING FIRST NODE FROM A SIMPLE LINKED LIST


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

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

    class D_node
      {
    private:
       int i;
       int number ;
       link start, *node, *previous;
   public:

   void  Delete_node();
   void  creat_node();
   };

   void D_node :: creat_node()
   {
     char ch;
     i = 0;

     start.next = NULL;  // Empty list
     node = &start;      // Point to the start of the list

     cout<<"\n Input choice n for break:";
     ch = getche();

   while(ch != 'n')
     {
       node->next = (struct link* ) malloc(sizeof(struct link));
       node = node->next;
       cout<<"\n Input the node :"<<(i+1)<<" : ";
       cin>>node->data;
       node->next = NULL;

       cout<<"\n Input choice n for break:";
       ch = getche();
       i++;
     }

     node = start.next;
     previous = &start;
     cout<<"\n Created list is as follows\n";

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


 // Removing a node
  void D_node:: Delete_node()
   {
     node = start.next;
     previous = &start;

     if (node == NULL)
       {
     cout<<"\n Under flow";
       }
       else
      {
         previous->next = node->next;
         free(node);
      }

  // Display the list

    node = start.next;
    cout<<"\n After deleting first node list is as follows\n";
    while (node)
      {
    cout<<"\n "<<node;
    cout<<"  "<< node->data;
    node = node->next;
      }
 }


    void main()
    {
       D_node del_node;
       del_node.creat_node();
       del_node.Delete_node();
    }

No comments:

Post a Comment