/* Singly Linked List ( SLL )*/
/***********************************************
NAME :-
JABIR DAUD PATHAN
PROGRAM :-
IMPLEMENTATION OF SLL
***********************************************/
PROGRAM
/*------------<< Header File
>>------------*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct
node
{
int data;
node*next;
};
/*-------<< Declaration of class sll >>--------*/
class
sll
{
private :
int ch,no,i,j;
node*head,*nwnode;
public :
sll() // Default constructor
{
head=NULL;
}
void create(); // [
Functions
void display();
// declaraton ]
void insert_beginning();
void insert_between();
void insert_end();
void delete_beginning();
void delete_between();
void delete_end();
void search();
void reverse();
};
/*-------<< Function defination for create sll
>>-------*/
void
sll :: create()
{
cout<<"\nEnter no. of
nodes==>";
cin>>no;
cout<<"\n\nEnter values of
node==>";
head=new node;
cin>>head->data;
head->next=NULL;
nwnode=head;
for(i=1;i<no;i++)
{
nwnode->next=new node;
nwnode=nwnode->next;
cout<<"\nEnter values of
node==>";
cin>>nwnode->data;
nwnode->next=NULL;
}
}
/*-------<< Function defination for display sll
>>-------*/
void
sll :: display()
{
node*p;
cout<<"\n\nCreated link list
:";
cout<<" head--->";
for(p=head;p!=NULL;p=p->next)
{
cout<<p->data<<"--->";
}
cout<<"NULL";
}
/*-----<< Function defination for inserting node at
beginning >>-----*/
void
sll :: insert_beginning()
{
node *nwnode;
cout<<"\nEnter the value to be
inserted==>";
cin>>j;
nwnode=new node;
nwnode->data=j;
nwnode->next=head;
head=nwnode;
}
/*----<< Function defination for inserting node in
between >>------*/
void
sll :: insert_between()
{
node *nwnode,*prev;
int m,n;
cout<<"\nEnter the value to be
inserted==>";
cin>>m;
nwnode=new node;
nwnode->data=m;
nwnode->next=NULL;
cout<<"\n\nEnter the previous
node==>";
cin>>n;
prev=head;
while(prev!=NULL && prev->data!=n)
{
prev=prev->next;
}
if(prev!=NULL)
{
nwnode->next=prev->next;
prev->next=nwnode;
}
else
cout<<"\nData not found";
}
/*-----<< Function defination for inserting node at end
>>------*/
void
sll :: insert_end()
{
node *nwnode,*p;
cout<<"\nEnter the value to be
inserted==>";
cin>>j;
nwnode=new node;
nwnode->data=j;
nwnode->next=NULL;
p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=nwnode;
}
/*-----<< Function defination for deleting beginning
node >>------*/
void
sll :: delete_beginning()
{
node *p;
if(head==NULL)
{
cout<<"\n\n\tUnderflow.......!";
}
p=head;
head=head->next;
delete p;
}
/*-----<< Function defination for deleting any node
from between >>------*/
void
sll :: delete_between()
{
node *p,*q;
int x,i;
cout<<"\nEnter the data to be
deleted : ";
cin>>x;
if(head==NULL)
{
cout<<"\n\n\tUnderflow.......!";
}
if(head->data==x)
{
p=head;
head=head->next;
delete p;
}
q=head;
while(q->next->data!=x &&
q->next !=NULL)
{
q=q->next;
}
p=q->next;
q->next=q->next->next;
delete p;
}
/*-----<< Function defination for deleting end node
>>------*/
void
sll :: delete_end()
{
node *p,*q;
if(head==NULL)
{
cout<<"\n\n\tUnderflow.......!";
}
p=head;
if(head->next==NULL)
{
head=NULL;
delete p;
}
q=head;
while(q->next->next!=NULL)
{
q=q->next;
}
p=q->next;
q->next=NULL;
delete p;
}
/*-----<< Function defination for search any node
>>------*/
void
sll :: search()
{
node *p;
int m,cnt=1;
cout<<"\n\nEnter the data to be
searched: ";
cin>>m;
p=head;
while(p!=NULL && p->data !=m)
{
cnt++;
p=p->next;
}
if(p==NULL)
cout<<"\n\tNot found";
else
cout<<"\n\tFound at location "<<cnt;
}
/*-----<< Function defination for reversing the sll
>>------*/
void
sll :: reverse()
{
node *p,*q,*r;
p=NULL;
q=head;
r=q->next;
while(q!=NULL)
{
q->next=p;
p=q;
q=r;
if(q!=NULL)
r=q->next;
}
cout<<"\n\nCreated link list
:";
cout<<" head--->";
for(q=p;q!=NULL;q=q->next)
{
cout<<q->data<<"--->";
}
cout<<"NULL";
}
/*-------------<< Main Function Starts >>------------*/
void
main()
{
sll s;
char ans;
int ch;
do
{
clrscr();
cout<<"\n\t\t\t---<<
Operations on SLL >>---";
cout<<"\n\n\t\t\t\t1.Create";
cout<<"\n\t\t\t\t2.Display";
cout<<"\n\t\t\t\t3.Insert";
cout<<"\n\t\t\t\t4.Delete";
cout<<"\n\t\t\t\t5.Search";
cout<<"\n\t\t\t\t6.Reverse";
cout<<"\n\t\t\t\t7.Exit";
cout<<"\n\n\t\t\t-----------------------------";
cout<<"\n\n\t\t\tEnter your choice
: ";
cin>>ch;
switch(ch)
{
case 1:
s.create();
break;
case 2:
s.display();
break;
case 3:
s.display();
do
{
cout<<"\n\n\tInsert:";
cout<<"\n\t\t1.Beginning";
cout<<"\n\t\t2.In Between";
cout<<"\n\t\t3.End";
cout<<"\n\n\t\tEnter your choice
: ";
cin>>ch;
switch(ch)
{
case 1:
s.insert_beginning();
s.display();
break;
case 2:
s.insert_between();
s.display();
break;
case 3:
s.insert_end();
s.display();
break;
}
cout<<"\n\n\n\t\tDo you want to
continue(Y/N)? :";
ans=getche();
}while(ans=='Y'||ans=='y');
break;
case 4:
s.display();
do
{
cout<<"\n\n\tDelete:";
cout<<"\n\t\t1.Beginning";
cout<<"\n\t\t2.In Between";
cout<<"\n\t\t3.End";
cout<<"\n\n\t\tEnter your choice
: ";
cin>>ch;
switch(ch)
{
case 1:
s.delete_beginning();
s.display();
break;
case 2:
s.delete_between();
s.display();
break;
case 3:
s.delete_end();
s.display();
break;
}
cout<<"\n\n\n\t\tDo you want to
continue(Y/N)? :";
ans=getche();
}while(ans=='Y'||ans=='y');
break;
case 5:
s.display();
s.search();
break;
case 6:
s.display();
s.reverse();
break;
case 7:
exit(0);
break;
}
cout<<"\n\n\n\t\tDo you want to
continue(Y/N)? :";
ans=getche();
}while(ans=='Y'||ans=='y');
getch();
}
/*-------------<< End Of Main Function >>---------------*/
/*-----------------<< OUTPUT SCREEN
>>-------------------*/
---<< Operations
on SLL >>---
1.Create
2.Display
3.Insert
4.Delete
5.Search
6.Reverse
7.Exit
-----------------------------
Enter your choice : 1
Enter
no. of nodes==>3
Enter
values of node==>1
Enter
values of node==>2
Enter
values of node==>3
Do you want to continue(Y/N)?
:Y
---<< Operations
on SLL >>---
1.Create
2.Display
3.Insert
4.Delete
5.Search
6.Reverse
7.Exit
-----------------------------
Enter your choice : 2
Created
link list :
head--->1--->2--->3--->NULL
Do you want to continue(Y/N)?
:Y
---<< Operations
on SLL >>---
1.Create
2.Display
3.Insert
4.Delete
5.Search
6.Reverse
7.Exit
-----------------------------
Enter your choice : 3
Created
link list :
head--->1--->2--->3--->NULL
Insert:
1.Beginning
2.In Between
3.End
Enter your choice : 1
Enter
the value to be inserted==>4
Created
link list :
head--->4--->1--->2--->3--->NULL
Do you want to continue(Y/N)?
:Y
Insert:
1.Beginning
2.In Between
3.End
Enter your choice : 2
Enter
the value to be inserted==>5
Enter
the previous node==>1
Created
link list :
head--->4--->1--->5--->2--->3--->NULL
Do you want to continue(Y/N)?
:Y
Insert:
1.Beginning
2.In Between
3.End
Enter your choice : 3
Enter
the value to be inserted==>7
Created
link list :
head--->4--->1--->5--->2--->3--->7--->NULL
Do you want to continue(Y/N)?
:N
Do you want to continue(Y/N)?
:Y
---<< Operations
on SLL >>---
1.Create
2.Display
3.Insert
4.Delete
5.Search
6.Reverse
7.Exit
-----------------------------
Enter your choice : 4
Created
link list :
head--->4--->1--->5--->2--->3--->7--->NULL
Delete:
1.Beginning
2.In Between
3.End
Enter your choice : 1
Created
link list :
head--->1--->5--->2--->3--->7--->NULL
Do you want to continue(Y/N)?
:Y
Delete:
1.Beginning
2.In Between
3.End
Enter your choice : 2
Enter
the data to be deleted : 5
Created
link list :
head--->1--->2--->3--->7--->NULL
Do you want to continue(Y/N)?
:Y
Delete:
1.Beginning
2.In Between
3.End
Enter your choice : 3
Created
link list :
head--->1--->2--->3--->NULL
Do you want to continue(Y/N)?
:N
Do you want to continue(Y/N)?
:Y
---<< Operations
on SLL >>---
1.Create
2.Display
3.Insert
4.Delete
5.Search
6.Reverse
7.Exit
-----------------------------
Enter your choice : 5
Created
link list :
head--->1--->2--->3--->NULL
Enter
the data to be searched: 2
Found at location 2
Do you want to continue(Y/N)?
:Y
---<< Operations
on SLL >>---
1.Create
2.Display
3.Insert
4.Delete
5.Search
6.Reverse
7.Exit
-----------------------------
Enter your choice : 6
Created
link list :
head--->1--->2--->3--->NULL
Created
link list :
head--->3--->2--->1--->NULL
Do you want to continue(Y/N)?
:Y
---<< Operations
on SLL >>---
1.Create
2.Display
3.Insert
4.Delete
5.Search
6.Reverse
7.Exit
-----------------------------
Enter your choice : 7
/*********************************************************************/