Operations on Linked Lists in C++

#include <iostream.h>
#include <conio.h>
 
class listNode
{
   public:  int data;
	    listNode *ptr;
};
 
class linkedList
{
   private: listNode *head;
 
   public: linkedList()  //constructor
	   {
	      head = NULL;
	   }
	   void insertFront(void);
	   int deleteFront(void);
	   void showList(void);
 
	   ~linkedList()   //destructor
	   {
	      delete head;
	   }
}; //End of linkedList
 
void linkedList::insertFront(void)
{
      listNode *newNode;
      int item;
 
      newNode = new listNode;
      cout<<"Enter the element to be inserted"<<endl;
      cin>>item;
      newNode->data = item;
      newNode->ptr = head;
      head = newNode;
}
 
int linkedList::deleteFront(void)
{
       listNode *temp;
       int item;
       if (head == NULL)
       {
	   cout<<"List is empty"<<endl;
	   return 1;
       }
       temp = head;
       cout<<"Deleted element is "<<temp->data<<endl;
       item = temp->data;
       head = head->ptr;
       delete(temp);
 
       return item;
}
 
void linkedList::showList(void)
{
      listNode *temp;
      temp = head;
      cout<<"\nContents of the list..."<<endl;
      while(temp != NULL)
      {
	 cout<<temp->data<<"->";
	 temp=temp->ptr;
      }
      cout<<"NULL"<<endl;
}
 
void main()
{
   linkedList ll;
   int choice, rpt;
   clrscr();
 
   while(rpt)
   {
       cout<<"1 --> Insert at Front"<<endl;
       cout<<"2 --> Delete from Front"<<endl;
       cout<<"3 --> Show the status "<<endl;
       cout<<"4 --> Exit "<<endl;
 
       cout<<"Enter your choice"<<endl;
       cin>>choice;
 
       switch(choice)
       {
	 case 1: ll.insertFront();
		 break;
	 case 2: ll.deleteFront();
		 break;
	 case 3: ll.showList();
		 break;
	 case 4: return;
       }
       cout<<"Do you want to continue(Type 1 or 0)?"<<endl;
       cin>>rpt;
   }
}

Output
1 –> Insert at Front
2 –> Delete from Front
3 –> Show the status
4 –> Exit
Enter your choice
1
Enter the element to be inserted
12
Do you want to continue(Type 1 or 0)?
1
1 –> Insert at Front
2 –> Delete from Front
3 –> Show the status
4 –> xit
Enter your choice
1
Enter the element to be inserted
34
Do you want to continue(Type 1 or 0)?
1
1 –> Insert at Front
2 –> Delete from Front
3 –> Show the status
4 –> Exit
Enter your choice
1
Enter the eeement to be inserted
67
Do you want to continue(Type 1 or 0)?
1
1 –> Insert at Front
2 –> Delete from Front
3 –> Show the status
4 –> Exit
Enter your choice
3

Contents of the list…
67->34->12->NULL
Do you want to continue(Type 1 or 0)?
1
1 –> Insert at Front
2 –> Delete from Front
3 –> Show the status
4 –> Exit
Enter your choice
2
Deleted element is 67
Do you want to continue(Type 1 or 0)?
1
1 –> Insert at Front
2 –> Delete from Front
3 –> Show the status
4 –> Exit
Enter your choice
2
Deleted element is 34
Do you want to continue(Type 1 or 0)?
1
1 –> Insert at Front
2 –> Delete from Front
3 –> Show the status
4 –> Exit
Enter your choice
2
Deleted element is 12
Do you want to continue(Type 1 or 0)?
1
1 –> Insert at Front
2 –> Delete from Front
3 –> Show the status
4 –> Exit
Enter your choice
2
List is empty
Do you want to continue(Type 1 or 0)?
0

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.