Singly Linked List Watch Video Click Here
Source Code
#include<iostream>
#include<conio.h>
using namespace std;
class singly_linklist
{
private:
struct node
{
int data;
node *next_add;
};
node *head = NULL;
public:
void insert(int &n);
void display();
void search();
void sum_count();
void update();
void del();
};
void singly_linklist::insert(int &n)
{
node *new_node = new node;
new_node -> data = n;
new_node -> next_add = NULL;
if(head == NULL)
{
head = new_node;
}
else
{
node *ptr = head;
while(ptr -> next_add != NULL)
{
ptr = ptr -> next_add;
}
ptr -> next_add = new_node;
}
}
void singly_linklist::display()
{
system("cls");
cout<<"\n\n\t\t\t\t Display Record";
if(head == NULL)
{
cout<<"\n\n Link List is Empty...";
}
else
{
node *ptr = head;
while(ptr != NULL)
{
cout<<"\n Value : "<<ptr -> data;
ptr = ptr -> next_add;
}
}
}
void singly_linklist::search()
{
system("cls");
int n,count=0;
cout<<"\n\n\t\t\t\tSearch Record";
if(head == NULL)
{
cout<<"\n\n Link List is Empty...";
}
else
{
cout<<"\n\n Search Value : ";
cin>>n;
node *ptr = head;
while(ptr != NULL)
{
if(n == ptr -> data)
{
cout<<"\n\n Value is Found : "<<n;
count++;
}
ptr = ptr -> next_add;
}
if(count == 0)
{
cout<<"\n\n Value Not Found : "<<n;
}
}
}
void singly_linklist::sum_count()
{
system("cls");
int sum=0,count=0;
cout<<"\n\n\t\t\t\tSum & Count Nodes";
if(head == NULL)
{
cout<<"\n\n Link List is Empty...";
}
else
{
node *ptr = head;
while(ptr != NULL)
{
sum = sum + ptr -> data;
count++;
ptr = ptr -> next_add;
}
cout<<"\n\n Total Values of Nodes in Link List : "<<sum;
cout<<"\n\n Total Nodes in Link List : "<<count;
}
}
void singly_linklist::update()
{
system("cls");
int n,count=0;
cout<<"\n\n\t\t\t\tUpdate Record";
if(head == NULL)
{
cout<<"\n\n Link List is Empty...";
}
else
{
cout<<"\n\n Value For Update : ";
cin>>n;
node *ptr = head;
while(ptr != NULL)
{
if(n == ptr -> data)
{
cout<<"\n\n New Value : ";
cin>>n;
ptr -> data = n;
count++;
break;
}
ptr = ptr -> next_add;
}
if(count == 0)
{
cout<<"\n\n Record Not Found...";
}
}
}
void singly_linklist::del()
{
system("cls");
int n,count=0;
cout<<"\n\n\t\t\t\tDelete Record";
if(head == NULL)
{
cout<<"\n\n Link List is Empty...";
}
else
{
cout<<"\n\n Value For Delete : ";
cin>>n;
if(n == head -> data)
{
node *ptr = head;
head = ptr -> next_add;
cout<<"\n\n Node is Deleted...";
delete ptr;
count++;
}
else
{
node *pre = head;
node *ptr = head -> next_add;
while(ptr != NULL)
{
if(n == ptr -> data)
{
pre -> next_add = ptr -> next_add;
cout<<"\n\n Node is Deleted...";
count++;
delete ptr;
break;
}
pre = ptr;
ptr = ptr -> next_add;
}
}
if(count == 0)
{
cout<<"\n\n Record Not Found...";
}
}
}
main()
{
int n,choice;
singly_linklist s;
p:
system("cls");
cout<<"\n\n\t\t\t\t Control Panel";
cout<<"\n\n 1. Insert Record";
cout<<"\n 2. Display Record";
cout<<"\n 3. Search Record";
cout<<"\n 4. Sum & Count";
cout<<"\n 5. Update Record";
cout<<"\n 6. Delete Record";
cout<<"\n 7. Exit";
cout<<"\n\n Enter 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;
s.insert(n);
break;
case 2:
s.display();
break;
case 3:
s.search();
break;
case 4:
s.sum_count();
break;
case 5:
s.update();
break;
case 6:
s.del();
break;
case 7:
exit(0);
default:
cout<<"\n\n Invalid Value...Please Try Again...";
}
getch();
goto p;
}