0% found this document useful (0 votes)
66 views

Sortedll

The document defines a template class for a sorted singly linked list. The class contains methods to insert nodes in sorted order, traverse the list to print elements, search for a value, and delete a node by value. The main function demonstrates usage by prompting the user to insert, traverse, search, or delete nodes from a list of integers.

Uploaded by

RAHUL16398
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Sortedll

The document defines a template class for a sorted singly linked list. The class contains methods to insert nodes in sorted order, traverse the list to print elements, search for a value, and delete a node by value. The main function demonstrates usage by prompting the user to insert, traverse, search, or delete nodes from a list of integers.

Uploaded by

RAHUL16398
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

using namespace std;

template<class tem>
class slnodesort
{
public:
tem information;
slnodesort<tem>*next;
slnodesort(tem n)
{
information=n;
}
};
template<class tem>
class sllist
{
public:

slnodesort<tem>*head=0 ;
slnodesort<tem>*tail=0;
void insertsort(int x)
{
slnodesort<tem>*temp2= new slnodesort<tem>(x);
if(head==0)
{
head=temp2;
tail=temp2;
}
else
{ if(x<head->information)
{
temp2->next=head;
head=temp2;
}
else
{
slnodesort<tem>*temp=head;
while(temp->next!=0)
{
if(x<temp->next->information)
break;
temp=temp->next;
}
if(temp->next!=0)
{
temp2->next=temp->next;
temp->next=temp2;
}
if(temp->next==0)
{
temp->next=temp2;
tail=temp2;
}

}
}
}
void traverse()
{
if(head==0)
{
cout<<"EMPTY LIST"<<endl;
}
else
{
slnodesort<tem>*temp = head;
while(temp!=NULL)
{
cout<<temp->information<<endl;
temp = temp->next;
}
}
}
bool search1(int value)
{
if(head==0)
{
cout<<"EMPTY LIST"<<endl;

}
else
{
slnodesort<tem>*temp = head;
while(temp!=NULL)
{
if(value==temp->information)
{
return true;
}
if(value>temp->information)
{
return false;
}
temp = temp->next;
}

}
}
int deleteValue(int value)
{
int num = 1;
slnodesort<tem>*temp = head;
slnodesort<tem>*temp1;
if(head==0)
{
cout<<"EMPTY LIST"<<endl;
return 0;
}
if(head==tail || value==1 )
{
head = head->next;
delete temp;
if(head==tail)
{
head = tail =0;
}
}
else if(head!=tail)
{
num = temp->information;
while(temp->next!=0)
{
if(num == value)
{
break;
}
if(value<num)
{
cout<<value<<" NOT PRESENT IN LIST "<<endl<<endl;
return 0;
}
temp1 = temp;
temp= temp->next;
num = temp->information;
}
if(num==value){
if(temp->next==0){
temp1->next =0;
tail = temp1;
delete temp;

}else{
temp1->next = temp->next;
delete temp;
}
}
}

};
int main()
{
int x;int p;
slnodesort<int>obj;
int choice;
do
{
cout<<"ENTER 1 TO INSERT IN A SORTED WAY"<<endl;
cout<<"ENTER 2 TO TRAVERSE"<<endl;
cout<<"ENTER 3 TO SEARCH "<<endl;
cout<<"ENTER 4 TO DELETE"<<endl;
cout<<"ENTER 0 TO EXIT"<<endl;
cin>>choice;
switch(choice)
{
case 1:{
cout<<"ENTER ELEMENT"<<endl;
cin>>x;
obj.insertsort(x);
break;}
case 2:{
obj.traverse();
break;}
case 3:{
cout<<"ENTER THE VALUE WHICH YOU WANT TO SEARCH"<<endl;
cin>>x;
obj.search1(x);
break;}
case 4:{
cout<<"ENTER THE VALUE TO DELETE"<<endl;
cin>>x;
obj.deleteValue(x);
break;}
default:
{
cout<<"INVALID CHOICE , PRESS 0 TO EXIT AND ANY OTHER KEY TO
CONTINUE"<<endl;
}
}
while(choice!=0);
return 0;
}

You might also like