Circular Linked List

Source code:
#include <iostream>using namespace std;struct node{ int num; node*next;};node*head=NULL;node*temp=NULL;void add(int n){ bool flag=false; node*newnode=new node; newnode->num=n; if(head==NULL) { head=newnode; newnode->next=head; flag=true; } else { temp=head; while(temp->next!=head) { temp=temp->next; } temp->next=newnode; newnode->next=head; flag=true; } if(flag==true) cout<<"value is entered \n"; else cout<<"Error \n";}void Delete(){ bool flag=false; node*temp2=NULL; if(head==NULL) { cout<<"list is empty \n"; } else if(head->next==head) { head->next=NULL; head=NULL; delete head; flag=true; } else { temp=head; while(temp->next!=head) { temp2=temp; temp=temp->next; } temp2->next=head; delete temp; flag=true; } if(flag==true) cout<<"value is Deleted \n"; else cout<<"Error \n";}void display(){ if(head==NULL) { cout<<" list is empty \n"; } else {temp=head; while(temp->next!=head) { cout<<temp->num<<"  "; temp=temp->next; } cout<<temp->num<<endl; }}int main(){ int ch,n; do{ cout<<"************************ \n"; cout<<"      Circular Linked list \n"; cout<<"************************ \n"; cout<<"1) insert at end \n"; cout<<"2) delete from end \n"; cout<<"3) display all \n"; cout<<"4) Exit \n"; cout<<"************************ \n"; cout<<"Enter Choice: "; cin>>ch; switch(ch) { case 1: { cout<<"Enter NUmber: "; cin>>n; add(n); break; } case 2: { Delete(); break; } case 3: { display(); break; } default: { cout<<"Invalid input \n"; break; } } }while(ch!=4);}

Comments

Popular posts from this blog

Computer Programming Lab 5

Computer Programming lab 11

Computer Programming Lab 4