Write a C++ Program to Implementation of circular 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 search(NODE*);
      void delet(NODE*);

};
NODE *nw,*temp,*head,*t1,*pre,*p,*end;
NODE* NODE::create()
{
      head=new(NODE);
      head->data=0;
      head->next=head;
      return(head);
}
void NODE::insert_e(NODE* head)
{
      nw=new(NODE);
      cin>>nw->data;
      cout<<"\nHead is entered successfully\n\n";
      nw->next=head;
      temp=head;
      while(temp->next!=head)
            temp=temp->next;
      temp->next=nw;
      head->data++;
}
void NODE::search(NODE* head)
{
      int srch,cnt=1;
      temp=head;
      cout<<"\nEnter the node data to be searched:";
      cin>>srch;
      while(head->next!=head)
      {
            if(temp->next->data==srch)
            {
                  cout<<"\nNode Found!!"<<"At position:"<<cnt;
                  break;
            }
            else
            {
                  temp=temp->next;
                  cout<<"\nNode not found";
                  break;
            }
            cnt++;
      }
}

void NODE::delet(NODE* head)
{
      cout<<"Enter data to delete from above node:";
      cin>>data;
      temp=head;
      while(temp->next!=head)
      {
            if(temp->next->data!=data)
                  temp=temp->next;
            else
                  break;
      }
      if(temp->next==head)
            cout<<"Data is found";
      else
      {
            t1=temp->next;
            temp->next=t1->next;
            delete (t1);
            head->data--;
            cout<<"node deleted successfully\n";
      }
}
void NODE::display()
{
      temp=head->next;
      while(temp!=head)
      {
            cout<<temp->data;
            temp=temp->next;
            cout<<"->";
      }
      cout<<"Head";
}
void main()
{
      int no,data;
      NODE a;
      clrscr();
      head=a.create();
      do
      {
            cout<<"\n\t\tMENU\n1.Insert at end\n 2.Display\n3.Search\n       4.Delete\n5.Exit\n";
            cout<<"Enter your choice:";
            cin>>no;
            switch(no)
            {

                  case 1:
                        cout<<"\nEnter data:";
                        a.insert_e(head);
                        break;
                  case 2:
                        cout<<"\n\nDisplaying NODES:";
                        a.display();
                        break;
                  case 3:
                        a.search(head);
                        break;
                  case 4:
                        a.delet(head);
                        a.display();
                        break;
                  case 5:
                        exit(0);
                        break;
                  default:
                        cout<<"PLEASE ENTER CORRECT CHOICE";
                        break;
            }
      }while(no<5);
      getch();
}






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

                MENU
1.Insert at end
2.Display
3.Search
4.Delete
5.Exit
Enter your choice:1

Enter data:4

Node is entered successfully


                MENU
1.Insert at end
2.Display
3.Search
4.Delete
5.Exit
Enter your choice:1

Enter data:5

Node is entered successfully


                MENU
1.Insert at end
2.Display
3.Search
4.Delete
5.Exit
Enter your choice:1

Enter data:1

Node is entered successfully


                MENU
1.Insert at end
2.Display
3.Search
4.Delete
5.Exit
Enter your choice:2

Displaying NODES:4->5->1->Head
                MENU
1.Insert at end
2.Display
3.Search
4.Delete
5.Exit
Enter your choice:3

Enter the node data to be searched:4

Node Found!! at position:1
                MENU
1.Insert at end
2.Display
3.Search
4.Delete
5.Exit
Enter your choice:4
Enter data to delete from above node:5
node deleted successfully
4->1->Head
                MENU
1.Insert at end
2.Display
3.Search
4.Delete
5.Exit

Enter your choice:5
Previous
Next Post »

Disqus Shortname

Comments system