Pages

Custom Search

Saturday, October 15, 2011

Singly linked list (in C++) to insert and delete node at front.


//Singly Linked list in C++...
//Inserting and deleting a node from front.


 #include <iostream>  
 #include <cstdlib>  
 using namespace std;  
 class NODE  
 {  
     public:  
     int data;  
     NODE* link;  
 };  
 class LIST  
 {  
     NODE* first;  
     public:  
     LIST();  
     ~LIST();  
     void ins_front(int);  
     void del_front();  
     void display();  
 };  
 LIST::LIST()         //Constructor  
 {  
     first = NULL;  
 }  
 LIST::~LIST()           //Destructor  
 {  
     cout<<"\nDestroyed\n";  
 }  
 //Function to insert node at front  
 void LIST::ins_front(int inum)  
 {  
     NODE* newnode;  
     newnode = (NODE*)malloc(sizeof(NODE));     //Allocating memory to newnode  
     newnode->data= inum;  
     (newnode->link) = first;  
     first = newnode;  
 }  
 //Function to delete front node  
 void LIST::del_front()  
 {  
     NODE* temp;  
     if(first == NULL)  
     {  
         cout<<"\nSingly Linked list is empty";  
         return;  
     }  
     if(first->link == NULL)      //to check if only one node is present  
     {  
         temp = first;  
         first = NULL;  
         cout<<"\nDeleted node: "<<temp->data;  
         free(temp);  
      }  
     else             //If more than one nodes are present  
     {  
         temp = first;  
         first = first->link;  
         cout<<"\nDeleted node: "<<temp->data;  
         free(temp);  
      }  
 }  
 void LIST::display()  
 {  
     NODE* temp;  
     if(first == NULL)  
     {  
         cout<<"\nSingly Linked list is empty";  
         return;  
     }  
     temp = first;  
     cout<<"\nSingly linked list is:\n";  
     while(temp!=NULL)  
     {  
         cout<<temp->data<<" ";  
         temp = temp->link;  
     }  
 }  
 int main()  
 {  
     LIST obj;         //Creating object of the class LIST  
     int choice, inum;  
  do  
  {  
         cout<<"\n\nSingly linked list operations: ";      //Menu for operations on linked list  
         cout<<"\n1.) Insert node at front";  
         cout<<"\n2.) Delete front node";  
         cout<<"\n3.) Display";  
         cout<<"\n4.) Exit";  
         cout<<"\n\nEnter your choice: ";  
         cin>>choice;  
         switch(choice)  
         {  
             case 1: cout<<"\nEnter element to insert at the front: ";  
                   cin>>inum;  
                   obj.ins_front(inum);  
                   break;  
             case 2: obj.del_front();  
                   break;  
             case 3: obj.display();  
                   break;  
             case 4: break;  
             default: cout<<"\nInvalid choice!! Enter choice again.";  
         }  
     }while(choice != 4);  
 }  

2 comments:

Anonymous said...

can u tell me what exactly happens when "first=NULL" in the constructor function is executed ??does the value of first->link become zero or what really happens ??

Anonymous said...

i meant does first->link become NULL or something else

Custom Search