Write A C++ Program To perform Insert,Delete,Reverse and Sort operation on Singly linked list

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class NODE
{
public:
      int data;
      NODE *next;
      NODE *create();
      void display();
      void insert_e(NODE*);
      void insert_s(NODE*);
      void insert_b(NODE*);
      void delet(NODE*);
      void reverse(NODE*);
      void sort(NODE*);

};
NODE *nw,*temp,*head,*t1,*pre,*p,*end;
NODE* NODE::create()
{
      head=new(NODE);
      head->data=0;
      head->next=NULL;
      return(head);
}
void NODE::insert_e(NODE* head)
{
      nw=new(NODE);
      cout<<"\nEnter the data of new node:";
      cin>>nw->data;
      cout<<"\nHead is entered successfully\n\n";
      nw->next=NULL;
      temp=head;
      while(temp->next!=NULL)
            temp=temp->next;
      temp->next=nw;
      head->data++;
}
void NODE::insert_s(NODE* head)
{
      nw=new(NODE);
      nw->next=NULL;
      cin>>nw->data;
      nw->next=head->next;
      head->next=nw;
      head->data++;
      cout<<"\nNode inserted Successfully";
}
void NODE::insert_b(NODE* head)
{
      int cnt=0;
      nw=new(NODE);
      nw->next=NULL;
      cin>>nw->data;
      cout<<"Enter the position:";
      cin>>cnt;
      temp=head;
      if(cnt>head->data)
            cout<<"Invalid position";
      else
      {
            while(cnt!=1)
            {
                  temp=temp->next;
                  cnt--;
            }
            nw->next=temp->next;
            temp->next=nw;
            head->data++;
      }
}
void NODE::delet(NODE* head)
{
      cout<<"Enter data to delete from above node:";
      cin>>data;
      temp=head;
      while(temp->next!=NULL)
      {
            if(temp->next->data!=data)
                  temp=temp->next;
            else
                  break;
      }
      if(temp->next==NULL)
            cout<<"Data is found";
      else
      {
            t1=temp->next;
            temp->next=t1->next;
            delete (t1);
            head->data--;
            cout<<"node deleted successfully\n";
      }
}
void NODE::reverse(NODE* head)
{
      int cnt=1;
      while(cnt<head->data)
      {
            temp=head;
            while(temp->next->next!=NULL)
                  temp=temp->next;
            if(cnt==1)
            {
                  t1=temp->next;
                  temp->next->next=temp;
                  temp->next=NULL;
            }
            cnt++;
      }
      temp->next=NULL;
      head->next=t1;
}
void NODE::sort(NODE* head)
{
      end=NULL;
      while(head->next!=end)
      {
            pre=head;
            temp=pre->next;
            while(temp->next!=end)
            {
                  p=temp->next;
                  if(temp->data>p->data)
                  {
                        pre->next=temp->next;
                        temp->next=p->next;
                        p->next=temp;
                  }
                  else
                        temp=temp->next;
                  pre=pre->next;
            }

            end=temp;
      }
}
void NODE::display()
{
      temp=head->next;
      while(temp!=NULL)
      {
            cout<<temp->data;
            temp=temp->next;
            cout<<"->";
      }
      cout<<"NULL\n";
}
void main()
{
      int no,data;
      NODE a;

      head=a.create();
      do
      {
            cout<<"\n\t\tMENU\n1.Insert head\n2.Insert at start\n3.Insert at between\n4.Insert at end\n5.Sort\n6.Reverse\n7.Delete\n8.Display\n9.Exit\n";
            cout<<"Enter your choice:";
            cin>>no;
            switch(no)
            {
                  case 1:
                        cout<<"\nInsert HEAD\n";
                        a.insert_s(head);
                        break;
                  case 2:
                        cout<<"\nInserting at start\nInsert data\n";
                        a.insert_s(head);
                        break;
                  case 3:
                        cout<<"\nInsert in between\n";
                        a.insert_b(head);
                        break;
                  case 4:
                        cout<<"\nInsert at end\n";
                        a.insert_e(head);
                        break;
                  case 5:
                        cout<<"\nSorted nodes are:";
                        a.sort(head);
                        a.display();
                        break;
                  case 6:
                        cout<<"The reversed nodes are:";
                        a.reverse(head);
                        a.display();
                        break;
                  case 7:
                        a.delet(head);
                        a.display();
                        break;
                  case 8:
                        cout<<"\n\nDisplaying NODES:";
                        a.display();
                        break;
                  case 9:
                        exit(0);
                        break;
                  default:
                        cout<<"PLEASE ENTER CORRECT CHOICE";
                        break;
            }
      }while(no<9);
      getch();
}





-----------------------------------Output----------------------------------------


                MENU
1.Insert head
2.Insert at start
3.Insert at between
4.Insert at end
5.Sort
6.Reverse
7.Delete
8.Display
9.Exit
Enter your choice:1

Insert HEAD:1
1->NULL

                MENU
1.Insert head
2.Insert at start
3.Insert at between
4.Insert at end
5.Sort
6.Reverse
7.Delete
8.Display
9.Exit
Enter your choice:2

Inserting at start:2
2->1->NULL

                MENU
1.Insert head
2.Insert at start
3.Insert at between
4.Insert at end
5.Sort
6.Reverse
7.Delete
8.Display
9.Exit
Enter your choice:3
Insert in between:3
Enter the position:2
2->3->1->NULL

                MENU
1.Insert head
2.Insert at start
3.Insert at between
4.Insert at end
5.Sort
6.Reverse
7.Delete
8.Display
9.Exit
Enter your choice:4

Insert at end:4
2->3->1->4->NULL

                MENU
1.Insert head
2.Insert at start
3.Insert at between
4.Insert at end
5.Sort
6.Reverse
7.Delete
8.Display
9.Exit

Enter your choice:5
Previous
Next Post »

Disqus Shortname

Comments system