DS Internal Programs
DS Internal Programs
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
Node(int d){
data=d;
next=NULL;
}
};
class SLL{
public:
Node *hptr=NULL;
void insert_at_end(int d){
Node *nptr=new Node(d);
Node *temp=hptr;
if(temp==NULL){
hptr=nptr;
return;
}
else{
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=nptr;
return;
}
}
void deletion(int d){
Node *temp=hptr;
if(temp==NULL){
cout<<"SLL is empty"<<endl;
return;
}
else if(temp->data==d){
hptr=temp->next;
return;
}
else{
while(temp!=NULL && temp->next->data!=d){
temp=temp->next;
}
if(temp==NULL){
cout<<"Element not found"<<endl;
return;
}
else{
temp->next=temp->next->next;
return;
}
}
}
void display(){
Node *temp=hptr;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
};
int main(){
SLL sll;
int ch,x;
do{
cout<<"SLL operations:"<<endl;
cout<<"1.Insertion at
end"<<endl<<"2.Deletion"<<endl<<"3.Display"<<endl<<"4.Exit"<<endl;
cout<<"Enter your choice: ";
cin>>ch;
switch(ch){
case 1: cout<<"Enter an element to insert: ";
cin>>x;
sll.insert_at_end(x);
break;
case 2: cout<<"Enter an element to delete: ";
cin>>x;
sll.deletion(x);
break;
case 3: cout<<"The elements in SLL are:"<<endl;
sll.display();
break;
case 4: cout<<"Thank you, we are going to end"<<endl;
exit(0);
default:cout<<"Invalid choice!!!"<<endl;
break;
}
}while(ch!=4);
return 0;
}
2) //CLL
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
Node(int d){
data=d;
next=NULL;
}
};
class CLL{
public:
Node *hptr=NULL;
void insert_at_end(int d){
Node *nptr=new Node(d);
Node *temp=hptr;
if(temp==NULL){
hptr=nptr;
hptr->next=hptr;
return;
}
else{
while(temp->next!=hptr){
temp=temp->next;
}
temp->next=nptr;
nptr->next=hptr;
return;
}
}
void deletion(int d){
Node *temp=hptr;
if(temp==NULL){
cout<<"CLL is empty"<<endl;
return;
}
else if(temp->data==d){
while(temp->next!=hptr){
temp=temp->next;
}
temp->next=temp->next->next;
hptr=temp->next;
}
else{
while(temp->next!=hptr && temp->next->data!=d){
temp=temp->next;
}
if(temp->next==hptr){
cout<<"Element not found"<<endl;
}
else{
temp->next=temp->next->next;
}
}
}
void display(){
Node *temp=hptr;
do{
cout<<temp->data<<" ";
temp=temp->next;
}while(temp!=hptr);
cout<<endl;
}
};
int main(){
CLL cll;
int ch,x;
do{
cout<<"CLL operations:"<<endl;
cout<<"1.Insertion at
end"<<endl<<"2.Deletion"<<endl<<"3.Display"<<endl<<"4.Exit"<<endl;
cout<<"Enter your choice: ";
cin>>ch;
switch(ch){
case 1: cout<<"Enter an element to insert: ";
cin>>x;
cll.insert_at_end(x);
break;
case 2: cout<<"Enter an element to delete: ";
cin>>x;
cll.deletion(x);
break;
case 3: cout<<"The elements in CLL are:"<<endl;
cll.display();
break;
case 4: cout<<"Thank you, we are going to end"<<endl;
exit(0);
default:cout<<"Invalid choice!!!"<<endl;
break;
}
}while(ch!=4);
return 0;
}
int main() {
ITPConversion itp;
string expr;
cout<<"Enter expression: ";
cin>>expr;
cout<<"Postfix form: "<<itp.InfixToPostfix(expr)<<endl;
return 0;
}
5)//PALINDROME TEST
#include <iostream>
#include <stack>
using namespace std;
bool isPalindrome(string s){
int len=s.length();
stack<char> st;
int mid=len/2, i;
char ele;
for(i=0; i<mid; i++){
st.push(s[i]);
}
if(len%2!=0){
i++;
}
while(s[i]!='\0'){
ele=st.top();
st.pop();
if (ele!=s[i]){
return false;
}
i++;
}
return true;
}
int main(){
string s;
cout<<"Enter a string: ";
cin>>s;
if(isPalindrome(s)){
cout<<"The enetered string "<<s<<" is a palindrome"<<endl;
}
else{
cout<<"The enetered string "<<s<<" is *not* a palindrome"<<endl;
}
return 0;
}