Circular Linked List Data Structure Code

 

Circular Linked List Watch Video Click Here


Source Code

#include<iostream>
#include<conio.h>
using namespace std;
int size=0;
class linklist
{
private:
struct node
{
int data;
node *next;
};
node *head = NULL;
node *tail = NULL;
public:
void insert(int &n);
void display();
void search();
void update();
void sum_count_avg();
void del();
};
void linklist::insert(int &n)
{
node *new_ptr = new node;
new_ptr -> data = n;
new_ptr -> next = NULL;
size++;
if(head == NULL)
{
head = new_ptr;
tail = new_ptr;
    new_ptr -> next = head;
}
else
{
tail -> next = new_ptr;
tail = new_ptr;
tail -> next = head;
}
cout<<"\n\n Node is Created Successfully...";
}
void linklist::display()
{
system("cls");
int c=0;
cout<<"\n\n\t\t\t\tDisplay Record";
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
node *ptr = head;
while(c != size)
{
cout<<"\n\n Value : "<<ptr -> data;
ptr = ptr -> next;
c++;
}
}
}
void linklist::search()
{
system("cls");
int c=0,n,found=0;
cout<<"\n\n\t\t\t\tSearch Record";
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
cout<<"\n\n Enter Value : ";
cin>>n;
node *ptr = head;
while(c != size)
{
if(n == ptr -> data)
{
cout<<"\n\n Value "<<n<<" is Found...";
found++;
break;
}
ptr = ptr -> next;
c++;
}
if(found == 0)
cout<<"\n\n Value "<<n<<" can't Found...";
}
}
void linklist::update()
{
system("cls");
int n,c=0,found=0;
cout<<"\n\n\t\t\t\tUpdate Record";
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
cout<<"\n\n Enter Value : ";
cin>>n;
node *ptr = head;
while(c != size)
{
if(n == ptr -> data)
{
cout<<"\n\n New Value : ";
cin>>n;
ptr -> data = n;
found++;
cout<<"\n\n Value Updated Successfully...";
break;
}
ptr = ptr -> next;
c++;
}
if(found == 0)
cout<<"\n\n Value "<<n<<" Can't Found...";
}
}
void linklist::sum_count_avg()
{
system("cls");
int c=0,sum=0;
cout<<"\n\n\t\t\t Sum, Count, Avg Record";
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
node *ptr = head;
while(c != size)
{
sum = sum + ptr -> data;
ptr = ptr -> next;
c++;
}
cout<<"\n\n Number of Nodes : "<<size;
cout<<"\n\n Sum : "<<sum;
cout<<"\n\n Average : "<<sum/size;
}
}
void linklist::del()
{
system("cls");
int n,found=0,c=1;
cout<<"\n\n\t\t\t\tDelete Record";
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
node *ptr = head;
cout<<"\n\n Enter Value : ";
cin>>n;
if(size == 1 && n == head -> data)
{
head = NULL;
tail = NULL;
size--;
cout<<"\n\n Node Deleted Successfully...";
found++;
delete ptr;
}
else if(n == head -> data)
{
head = head -> next;
tail -> next = head;
size--;
cout<<"\n\n Node Deleted Successfully...";
found++;
delete ptr;
}
else if(n == tail -> data)
{
while(ptr -> next != tail)
{
ptr = ptr -> next;
}
node *pre = tail;
tail = ptr;
tail -> next = head;
size--;
cout<<"\n\n Node Deleted Successfully...";
found++;
delete pre;
}
else
{
node *pre_ptr = head;
ptr = head -> next;
while(c != size)
{
if(n == ptr -> data)
{
pre_ptr -> next = ptr -> next;
size--;
cout<<"\n\n Node Deleted Successfully...";
found++;
delete ptr;
break;
}
pre_ptr = ptr;
ptr = ptr -> next;
c++;
}
}
if(found == 0)
cout<<"\n\n Value "<<n<<" Can't Found...";
}
}
main()
{
linklist l;
int choice,n;
p:
system("cls");
cout<<"\n\n\t\t\t\tControl Panel";
cout<<"\n\n 1. Insert Record";
cout<<"\n 2. Display Record";
cout<<"\n 3. Search Record";
cout<<"\n 4. Update Record";
cout<<"\n 5. Sum, Count, Avg Record";
cout<<"\n 6. Delete Record";
cout<<"\n 7. Exit";
cout<<"\n\n Your Choice : ";
cin>>choice;
switch(choice)
{
case 1:
system("cls");
cout<<"\n\n\t\t\t\tInsert Record";
cout<<"\n\n Enter Value : ";
cin>>n;
l.insert(n);
break;
case 2:
l.display();
break;
case 3:
l.search();
break;
case 4:
l.update();
break;
case 5:
l.sum_count_avg();
break;
case 6:
l.del();
break;
case 7:
exit(0);
default:
cout<<"\n\n Invalid Value...Please Try Again...";
}
getch();
goto p;
}

Post a Comment

Previous Post Next Post