22HCS4114
22HCS4114
Submitted By-
Ashhar Kamran Ansari (22HCS4114)
INDEX
• Singly Linked List
• Doubly Linked List
• Circular Linked List
• Evaluate an Infix expression by converting it into
Postfix/Prefix
• Implement a Queue with Array
• Binary Search Tree
• AVL Tree
• Implement Addition of Long Integer with Linked List
• Implement Addition of Two polynomials with Linked List
• Find Next Greater elements in an Array
• Implement Heap
• Implement Queue with Circular Linked List
• Implement Double Ended Queue with Doubly Linked List
• Sorted Linked List
• Implement Stack with Array
• Code to check if parenthesis is valid
• Code to implement Tower of Hanoi solving.
Q1. Code to implement Singly Linked List as an ADT.
#include <iostream>
#include <cstring>
using namespace std;
class Node{
int ele;
Node* next;
friend class SLL;
public :
Node(int a=0){
ele=a;
next=NULL;
}
};
class SLL{
Node* head;
public:
SLL(){
head=NULL;
}
SLL(const SLL& a);
~SLL();
bool empty();
void addfront(int a);
void addlast(int a);
void addindex(int ind,int a);
void delfront();
void dellast();
void delindex(int a);
bool search(int a);
void reverse();
int middle();
void display();
};
SLL::SLL(const SLL& a) {
if (a.head == NULL) {
head = NULL;
return;
}
head = new Node(a.head->ele);
Node* tempnode = a.head;
Node* currentnode = head;
while (tempnode->next != NULL) {
currentnode->next = new Node(tempnode->next->ele);
currentnode = currentnode->next;
tempnode = tempnode->next;
}
}
SLL::~SLL(){
Node* currentnode=head;
while(currentnode!=NULL){
Node* tempnode=currentnode;
currentnode=currentnode->next;
delete tempnode;
}
}
bool SLL::empty(){
if(head==NULL) return 1;
else return 0;
}
void SLL::addfront(int a){
Node* newnode= new Node;
newnode->next=head;
newnode->ele=a;
head=newnode;
}
void SLL::addlast(int a){
Node* newnode= new Node;
newnode->next=NULL;
newnode->ele=a;
Node* tempnode= head;
if(this->empty()){
head=newnode;
return;
}
while(tempnode->next!=NULL)tempnode =tempnode->next;
tempnode->next=newnode;
}
void SLL::addindex(int ind,int a){
Node* newnode =new Node;
newnode->ele=a;
Node* tempnode=head;
if(ind==1){
tempnode->next= head;
head=newnode;
return;
}
else{
int count=1;
while(tempnode!=NULL and count<ind-1){
tempnode=tempnode->next;
count++;
}
if(tempnode==NULL) cout<<"Invalid Position"<<endl;
else{
newnode->next=tempnode->next;
tempnode->next=newnode;
}
}
}
void SLL::delfront(){
if(head==NULL){
cout<<"Empty List"<<endl;
return;
}
Node* newnode= new Node;
newnode=head;
head=newnode->next;
delete newnode;
}
void SLL::dellast(){
if(head==NULL){
cout<<"Empty List"<<endl;
return;
}
else if(head->next==NULL){
this->delfront();
return;
}
else{
Node* prevnode=new Node;
Node* tempnode=new Node;
tempnode=head;
prevnode=NULL;
while(tempnode->next!=NULL){
prevnode=tempnode;
tempnode=tempnode->next;
}
delete tempnode;
prevnode->next=NULL;
}
}
void SLL::delindex(int a){
if (head==NULL){
cout<<"Empty List, can't delete"<<endl;
return;
}
else if (a==1){
this->delfront();
return;
}
else{
Node* tempnode = head;
int c=1;
while(tempnode!=NULL and c<a-1){
tempnode=tempnode->next;
c++;
}
if(tempnode==NULL)
{
cout<<"Invalid Position"<<endl;
return;
}
else{
Node* nextnode=tempnode->next;
tempnode->next=nextnode->next;
delete nextnode;
}
}
}
bool SLL::search(int a){
if(head==NULL)throw "Empty List";
else{
Node* tempnode=head;
while(tempnode!=NULL and tempnode->ele!=a){
tempnode=tempnode->next;
}
if(tempnode==NULL) return 0;
else if(tempnode->ele!=a) return 1;
}
}
void SLL::reverse(){
Node* prevnode=NULL;
Node* nextnode=NULL;
Node* currentnode=head;
while(currentnode!=NULL){
nextnode=currentnode->next;
currentnode->next=prevnode;
prevnode=currentnode;
currentnode=nextnode;
}
head=prevnode;
}
int SLL::middle(){
if(head==NULL)throw "Empty List";
int c=0;
Node* tempnode=head;
while(tempnode!=NULL){
tempnode=tempnode->next;
c++;
}
int mid=c/2;
c=0;
tempnode=head;
while(tempnode!=NULL and c<mid){
tempnode=tempnode->next;
c++;
}
return tempnode->ele;
}
void SLL::display(){
if(head==NULL)cout<<"Empty List";
else{
cout<<"The list is: ";
Node* tempnode=head;
while(tempnode!=NULL){
cout<<tempnode->ele<<" ";
tempnode=tempnode->next;
}
}
cout<<endl;
}
int main()
{
string ans="Y";
cout<<"1. Add an element at front."<<endl;
cout<<"2. Add an element at last."<<endl;
cout<<"3. Add an element at a given index."<<endl;
cout<<"4. Delete an element from front."<<endl;
cout<<"5. Delete an element from last."<<endl;
cout<<"6. Delete an element at given index."<<endl;
cout<<"7. Search an element."<<endl;
cout<<"8. Reverse the list."<<endl;
cout<<"9. Return Middle element."<<endl;
cout<<"10. Display the list."<<endl;
SLL A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
switch(choice){
case 1:
n=0;
cout<<" Enter element to add at first: ";cin>>n;
A.addfront(n);
break;
case 2:
n=0;
cout<<" Enter element to add at last: ";cin>>n;
A.addlast(n);
break;
case 3:
ind=0;
n=0;
cout<<" Enter index: ";cin>>ind;
cout<<" Enter element to add: ";cin>>n;
A.addindex(ind,n);
break;
case 4:
A.delfront();
cout<<"First element deleted."<<endl;
break;
case 5:
A.dellast();
cout<<"Last element deleted."<<endl;
break;
case 6:
ind=0;
cout<<" Enter index: ";cin>>ind;
A.delindex(ind);
cout<<" Element "<<ind<<" deleted."<<endl;
break;
case 7:
n=0;
cout<<" Enter element to search: ";cin>>n;
if(A.search(n)) cout<<"Found."<<endl;
else cout<<"Not Found."<<endl;
break;
case 8:
A.reverse();
cout<<"List Reversed."<<endl;
break;
case 9:
cout<<" Middle Element is:"<<A.middle()<<endl;
break;
case 10:
A.display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
}
Output-
Q2. Code to implement Doubly Linked List as an ADT.
#include <iostream>
using namespace std;
class Node{
int ele;
Node* next;
Node* prev;
friend class DLL;
public :
Node(int a=0,Node* p=NULL,Node* n=NULL ){
ele=a;
next=n;
prev=p;
}
};
class DLL{
Node* header;
Node* trailer;
protected:
void add(Node*,int&);
void remove(Node*);
public:
DLL(){
header=new Node;
trailer=new Node;
header->next=trailer;
trailer->prev=header;
header->prev=NULL;
trailer->next=NULL;
}
~DLL(){
Node* currentnode=header->next;
while(currentnode!=trailer){
Node* tempnode=currentnode;
currentnode=currentnode->next;
delete tempnode;
}
delete header;
delete trailer;
}
bool empty();
int& frontele();
int& lastele();
void addfront(int);
void addlast(int);
void delfront();
void dellast();
void display();
int& findmiddle();
void reverse();
};
void DLL::add(Node* nextnode,int& x){ //add before
Node* newnode=new Node(x,nextnode->prev,nextnode); //element,prev,next
nextnode->prev->next=newnode;
nextnode->prev=newnode;
}
void DLL::remove(Node* currentnode){
currentnode->next->prev=currentnode->prev;
currentnode->prev->next=currentnode->next;
delete currentnode;
}
bool DLL::empty(){
if(header->next==trailer)return 1;
else return 0;
}
int& DLL::frontele(){
if (empty())throw"Empty List";
return header->next->ele;
}
int& DLL::lastele(){
if (empty())throw"Empty List";
return trailer->prev->ele;
}
void DLL::addfront(int x){
Node* newnode=new Node;
newnode->ele=x;
newnode->next=header->next;
newnode->prev=header;
header->next->prev=newnode;
header->next=newnode;
}
void DLL::addlast(int x){
Node* newnode=new Node;
newnode->ele=x;
newnode->next=trailer;
newnode->prev=trailer->prev;
trailer->prev->next=newnode;
trailer->prev=newnode;
}
void DLL::delfront(){
Node* newnode=header->next;
header->next=newnode->next;
newnode->next->prev=newnode->prev;
delete newnode;
}
void DLL::dellast(){
Node* newnode=trailer->prev;
newnode->prev->next=newnode->next;
trailer->prev=newnode->prev;
delete newnode;
}
void DLL::display(){
if(empty()) cout<<"Empty List";
else{
Node* newnode=header->next;
cout<<"List is: ";
while (newnode!=trailer){
cout<<newnode->ele<<" ";
newnode=newnode->next;
}
}
cout<<endl;
}
int& DLL::findmiddle(){
Node* newnode=header->next;
Node* new2=trailer->prev;
while(newnode!=new2 and newnode->next!=new2){
newnode=newnode->next;
new2=new2->prev;
}
return newnode->ele;
}
void DLL::reverse(){
if (empty()) return;
Node* first=header->next;
Node* last=trailer->prev;
while(first!=last and first->next!=last){
int temp=first->ele;
first->ele=last->ele;
last->ele=temp;
first=first->next;
last=last->prev;
}
}
int main()
{
string ans="Y";
cout<<"1. Add an element at front."<<endl;
cout<<"2. Add an element at last."<<endl;
cout<<"3. Delete an element from front."<<endl;
cout<<"4. Delete an element from last."<<endl;
cout<<"5. Reverse the list."<<endl;
cout<<"6. Return Middle element."<<endl;
cout<<"7. Display the list."<<endl;
DLL A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
switch(choice){
case 1:
n=0;
cout<<" Enter element to add at first: ";cin>>n;
A.addfront(n);
break;
case 2:
n=0;
cout<<" Enter element to add at last: ";cin>>n;
A.addlast(n);
break;
case 3:
A.delfront();
cout<<"First element deleted."<<endl;
break;
case 4:
A.dellast();
cout<<"Last element deleted."<<endl;
break;
case 5:
A.reverse();
cout<<"List Reversed."<<endl;
A.display();
break;
case 6:
cout<<" Middle Element is:"<<A.findmiddle()<<endl;
break;
case 7:
A.display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
}
Output-
Q3. Code to implement Circular Linked List as an ADT.
#include <iostream>
using namespace std;
class Node{
int ele;
Node* next;
friend class CLL;
public :
Node(int a=0){
ele=a;
next=NULL;
}
};
class CLL{
Node* cursor;
public:
CLL(){
cursor=NULL;
}
~CLL(){
while(!empty()){
remove();
}
}
bool empty()const;
int front()const;
int back()const;
void advance();
void add(int e);
void remove();
void removecursor();
void display()const;
bool search(int a);
void removeint(int a);
};
bool CLL::empty()const{
return cursor==NULL;
}
int CLL::front()const{
if(empty())throw "Empty";
return cursor->next->ele;
}
int CLL::back()const{
if(empty())throw "Empty";
return cursor->ele;
}
void CLL::advance(){
if(empty())throw "Empty";
cursor=cursor->next;
}
void CLL::add(int e){
Node* newnode=new Node;
newnode->ele=e;
if(empty()){
newnode->next=newnode;
cursor=newnode;
}
else{
newnode->next=cursor->next;
cursor->next=newnode;
}
}
void CLL::remove(){
Node* tempnode=cursor->next;
if(tempnode==cursor)cursor=NULL;
else cursor->next=tempnode->next;
delete tempnode;
}
void CLL::removecursor(){
Node* tempnode=cursor->next;
if(tempnode==cursor)cursor=NULL;
while(tempnode->next!=cursor)tempnode=tempnode->next;
tempnode->next=cursor->next;
delete cursor;
cursor=tempnode;
}
void CLL::display()const{
if(empty()){
throw "Empty list.";
}
cout<<"The list is: ";
Node* tempnode=cursor->next;
do{
cout<<tempnode->ele<<" ";
tempnode=tempnode->next;
}
while(tempnode!=cursor->next);
cout<<endl;
}
bool CLL::search(int a){
if(empty()){
throw "Empty list.";
}
Node* tempnode=cursor->next;
do{
if (tempnode->ele==a)return 1;
tempnode=tempnode->next;
}
while(tempnode!=cursor->next);
return 0;
}
void CLL::removeint(int a){
if(empty()){
throw "Empty list.";
}
Node* tempnode=cursor->next;
Node* prevnode=cursor;
do{
if (tempnode->ele==a){
prevnode->next=tempnode->next;
delete tempnode;
return;
}
tempnode=tempnode->next;
prevnode=prevnode->next;
}
while(tempnode!=cursor->next);
throw "Not found.";
return;
}
int main(){
string ans="Y";
cout<<"1. Add an element after cursor."<<endl;
cout<<"2. Delete an element after cursor."<<endl;
cout<<"3. Delete an element at cursor."<<endl;
cout<<"4. Search an element."<<endl;
cout<<"5. Remove an element from the list."<<endl;
cout<<"6. Display the list."<<endl;
CLL A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
try{
switch(choice){
case 1:
n=0;
cout<<" Enter element to add after cursor: ";cin>>n;
A.add(n);
break;
case 2:
A.remove();
cout<<"First element deleted."<<endl;
break;
case 3:
A.removecursor();
cout<<"Last element deleted."<<endl;
break;
case 4:
n=0;
cout<<" Enter element to search: ";cin>>n;
if(A.search(n)) cout<<"Found."<<endl;
else cout<<"Not Found."<<endl;
break;
case 5:
n=0;
cout<<" Enter element to delete: ";cin>>n;
A.removeint(n);
cout<<"Element deleted."<<endl;
break;
case 6:
A.display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
}
catch(...){
cout<<"Invalid Input."<<endl;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
}
Output-
Q4. Code to evaluate postfix and prefix expressions.
#include <iostream>
#include <stack>
#include <cstring>
#include <math.h>
using namespace std;
while(!op.empty()){
output+=op.top();
op.pop();
}
return output;
}
int evalpost(string S){
char* arr=new char [S.length()];
int index=0;
for (int i=0;i<S.length();i++){
if(infunc(S[i])==1 and notin(S[i],arr)){
arr[index]=S[i];
index++;
}
}
int ind=0;
int* val=new int[index];
while(ind<index){
cout<<" Enter value of "<<arr[ind]<<": ";
cin>>val[ind];
ind++;
}
stack <char> solve;
for (int i=0;i<S.length();i++){
int r=infunc(S[i]);
if(r==1){
solve.push(indval(S[i],arr,val));
}
else if(r==2){
int a1=solve.top();
solve.pop();
int a2=solve.top();
solve.pop();
if (S[i]=='+'){
solve.push(a2+a1);
}
else if(S[i]=='-'){
solve.push(a2-a1);
}
else if(S[i]=='*'){
solve.push(a2*a1);
}
else if(S[i]=='/'){
solve.push(a2/a1);
}
else if(S[i]=='^'){
solve.push(pow(a2,a1));
}
}
}
return solve.top();
}
string prefix(string S){
string rev="";
int size=S.length();
for(int i=size-1;i>=0;i--){
if (S[i]=='(')rev+=")";
else if (S[i]==')')rev+="(";
else rev+=S[i];
}
string inf=postfix(rev);
rev="";
for(int j=size-1;j>=0;j--){
if(infunc(inf[j])==1 or infunc(inf[j])==2)rev+=inf[j];
}
return rev;
}
int evalpre(string S){
char* arr=new char [S.length()];
int index=0;
for (int i=0;i<S.length();i++){
if(infunc(S[i])==1 and notin(S[i],arr)){
arr[index]=S[i];
index++;
}
}
int ind=0;
int* val=new int[index];
while(ind<index){
cout<<" Enter value of "<<arr[ind]<<": ";
cin>>val[ind];
ind++;
}
stack <char> solve;
for (int i=S.length()-1;i>=0;i--){
int r=infunc(S[i]);
if(r==1){
solve.push(indval(S[i],arr,val));
}
else if(r==2){
int a1=solve.top();
solve.pop();
int a2=solve.top();
solve.pop();
if (S[i]=='+'){
solve.push(a2+a1);
}
else if(S[i]=='-'){
solve.push(a2-a1);
}
else if(S[i]=='*'){
solve.push(a2*a1);
}
else if(S[i]=='/'){
solve.push(a2/a1);
}
else if(S[i]=='^'){
solve.push(pow(a2,a1));
}
}
}
return solve.top();
}
int main(){
string equation;
cout<<"Please give expression in form of Variables, Eg= A^B+c*d-A"<<endl<<"Values for
the variables will be asked later..."<<endl;
cout<<"Enter equation: ";
cin>>equation;
cout<<"Original Expression: "<<equation<<endl<<endl;
string post=postfix(equation);
cout<<"Postfix Expression: "<<post<<endl;
string pre=prefix(equation);
cout<<"Prefix Expression: "<<pre<<endl;
int a=evalpost(post);
cout<<"Value is(by postfix): "<<a<<endl;
int b=evalpre(pre);
cout<<"Value is(by prefix): "<<b<<endl;
}
Output-
Q5. Code to implement Queue by array as ADT.
#include <iostream>
using namespace std;
class Queue{
int *CircularArray;
int capacity;
int n;
int f;
int r;
public:
Queue(int size){
CircularArray= new int[size];
capacity=size;
n=0;
f=0;
r=0;
}
~Queue(){
delete CircularArray;
}
bool empty()const;
void enqueue(int e);
void dequeue();
int front()const;
int size()const;
void display()const;
};
bool Queue::empty()const{
return (n==0);
}
void Queue::enqueue(int e){
if(n==capacity) throw "Queue Full";
CircularArray[r]=e;
r=(r+1)%capacity;
n++;
}
void Queue::dequeue(){
if(n==0) throw "Queue Empty";
f=(f+1)%capacity;
n--;
}
int Queue::front()const{
if(n==0) throw "Queue Empty";
return CircularArray[f];
}
int Queue::size()const{
return n;
}
void Queue::display()const{
int counter=0;
while(counter<n-1){
cout<<CircularArray[(f+counter)%capacity]<<"->";
counter++;
}
cout<<CircularArray[(f+counter)%capacity]<<endl;
}
int main(){
string ans="Y";
cout<<"1. Enqueue."<<endl;
cout<<"2. Dequeue."<<endl;
cout<<"3. Front element."<<endl;
cout<<"4. Size of queue."<<endl;
cout<<"5. Display the queue."<<endl;
int size;
cout<<" Enter Capacity of Queue: ";cin>>size;
Queue A(size);
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
try{
switch(choice){
case 1:
n=0;
cout<<" Enter element to add at first: ";cin>>n;
A.enqueue(n);
break;
case 2:
A.dequeue();
cout<<"Element dequeued."<<endl;
break;
case 3:
cout<<"Front element is:"<<A.front()<<endl;
break;
case 4:
cout<<"Size of queue is:"<<A.size()<<endl;
break;
case 5:
A.display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
}
catch(...){
cout<<"Invalid Command."<<endl;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
}
Output-
Q6. Code to implement Binary Search Tree as ADT.
#include <iostream>
#include <queue>
using namespace std;
class Node{
public:
Node(){
left=right=0;
}
Node(int e, Node* l=NULL, Node* r=NULL){
ele=e;
left=l;
right=r;
}
int ele;
Node* left;
Node* right;
friend class BST;
};
class BST{
Node* root;
public:
BST(){
root=NULL;
}
BST(int e){
root=new Node(e);
}
~BST(){
root=NULL;
}
Node* min(Node* p);
Node* max(Node* p);
Node* parent(Node* p);
void insert(int e);
void insertrec(int e);
void insertrec(int e, Node* &n);
Node* search(int e);
Node* searchrec(int e);
Node* searchrec(int e,Node* &n);
void preorder();
void preorder(Node* n);
void postorder();
void postorder(Node* n);
void inorder();
void inorder(Node* n);
void BreadthFirst();
Node* successor(int e);
Node* predecessor(int e);
int height(Node* p);
int nodeno();
int nodeno(Node* p);
int leafno();
int leafno(Node* p);
bool equality(BST& other);
bool equality(Node* p,Node* q);
bool mirror(BST& other);
bool mirror(Node* p,Node* q);
void deletem(int e);
void deletec(int e);
};
Node* BST::min(Node* p){
if(p==NULL) throw "Empty";
while(p->left!=NULL)p=p->left;
return p;
}
Node* BST::max(Node* p){
if(p==NULL) throw "Empty";
while(p->right!=NULL)p=p->right;
return p;
}
Node* BST::parent(Node* p){
Node* current= root;
Node* prev=NULL;
int e=p->ele;
while(current!=NULL){
if(current->ele==e) break;
else if(e<current->ele){
prev=current;
current=current->left;
}
else{
prev=current;
current=current->right;
}
}
if (current==NULL)throw "Invalid Value";
return prev;
}
void BST::insert(int e){
Node* newnode= new Node(e);
Node* current= root;
Node* prev= NULL;
while(current!=NULL){
prev=current;
if(e<current->ele)current=current->left;
else current=current->right;
}
if(prev==NULL)root=newnode;
else if(e<prev->ele)prev->left=newnode;
else prev->right=newnode;
}
void BST::insertrec(int e){
return insertrec(e,root);
}
void BST::insertrec(int e, Node* &n){
if (n==NULL){
n= new Node(e);
return;
}
if(e<n->ele) insertrec(e,n->left);
else if(e>n->ele) insertrec(e,n->right);
}
Node* BST::search(int e){
Node* current= root;
while(current!=NULL){
if(current->ele==e) break;
else if(e<current->ele)current=current->left;
else current=current->right;
}
if (current==NULL)throw "Invalid Value";
return current;
}
Node* BST::searchrec(int e){
return searchrec(e,root);
}
Node* BST::searchrec(int e,Node* &n){
if (n==NULL){
return n;
}
if (n->ele==e)return n;
if(e<n->ele) return searchrec(e,n->left);
else if(e>n->ele) return searchrec(e,n->right);
}
void BST::preorder(){
cout<<"Preorder: ";
preorder(root);
cout<<endl;
}
void BST::preorder(Node* n){
if(n==NULL){
return;
}
cout<<n->ele<<" ";
preorder(n->left);
preorder(n->right);
}
void BST::postorder(){
cout<<"Postorder: ";
postorder(root);
cout<<endl;
}
void BST::postorder(Node* n){
if(n==NULL){
return;
}
postorder(n->left);
postorder(n->right);
cout<<n->ele<<" ";
}
void BST::inorder(){
cout<<"Inorder: ";
inorder(root);
cout<<endl;
}
void BST::inorder(Node* n){
if(n==NULL){
return;
}
inorder(n->left);
cout<<n->ele<<" ";
inorder(n->right);
}
void BST::BreadthFirst(){
queue<Node*> q;
Node* p=root;
if(p!=0) q.push(p);
while(!q.empty()){
p=q.front();
q.pop();
cout<<p->ele<<" ";
if(p->left!=NULL)q.push(p->left);
if(p->right!=NULL)q.push(p->right);
}
}
Node* BST::successor(int e){
Node* r=this->search(e);
if(r==NULL)throw"Not Found.";
if(r==this->max(root))throw "Invalid";
if (r->right!=NULL){
return this->min(r->right);
}
Node* y=this->parent(r);
while(y!=NULL and r==y->right){
Node* temp=y;
r=y;
y=this->parent(temp);
}
return y;
}
int BST::height(Node* p){
if(p==NULL)return -1;
else if(p->right==NULL and p->left==NULL)return 0;
int left=height(p->left);
int right=height(p->right);
int num=left>right?left:right;
return (num+1);
}
int BST::nodeno(){
if(root==NULL)return 0;
else if(root->left==NULL and root->right==NULL)return 1;
return (1+nodeno(root->left)+nodeno(root->right));
}
int BST::nodeno(Node* p){
if(p==NULL)return 0;
else if(p->left==NULL and p->right==NULL)return 1;
return (1+nodeno(p->left)+nodeno(p->right));
}
int BST::leafno(){
if(root==NULL)return 0;
else if(root->left==NULL and root->right==NULL)return 1;
return (leafno(root->left)+leafno(root->right));
}
int BST::leafno(Node* p){
if(p==NULL)return 0;
else if(p->left==NULL and p->right==NULL)return 1;
return (leafno(p->left)+leafno(p->right));
}
bool BST::equality(BST& other){
return equality(root,other.root);
}
bool BST::equality(Node* p, Node* q){
if(p==NULL and q==NULL)return 1;
else if((p==NULL and q!=NULL) or (p!=NULL and q==NULL))return 0;
else if(p->ele!=q->ele)return 0;
return (equality(p->left,q->left) and equality(p->right,q->right));
}
bool BST::mirror(BST& other){
return mirror(root,other.root);
}
bool BST::mirror(Node* p, Node* q){
if(p==NULL and q==NULL)return 1;
else if((p==NULL and q!=NULL) or (p!=NULL and q==NULL))return 0;
else if(p->ele!=q->ele)return 0;
return (equality(p->left,q->right) and equality(p->right,q->left));
}
void BST::deletem(int e){
Node* p=this->search(e);
if(p->left==NULL and p->right==NULL){
if(p==root){
root=NULL;
return;
}
Node* parent=this->parent(p);
if(parent->left==p)parent->left=NULL;
else parent->right=NULL;
return;
}
else if((p->left==NULL and p->right!=NULL) or (p->left!=NULL and p->right==NULL)){
if(p==root){
if(p->right!=NULL) root=p->right;
else root=p->left;
return;
}
Node* parent=this->parent(p);
if(parent->left==p){
if(p->right!=NULL) parent->left=p->right;
else parent->left=p->left;
}
else{
if(p->right!=NULL) parent->right=p->right;
else parent->right=p->left;
}
return;
}
else{
Node* r=p->right;
Node* l=p->left;
while(l->right!=NULL)l=l->right;
l->right=r;
if(p==root){
root=p->left;
return;
}
Node* parent=this->parent(p);
if(parent->left==p){
parent->left=p->left;
}
else{
parent->right=p->left;
}
return;
}
}
void BST::deletec(int e){
Node* p=this->search(e);
if(p->left==NULL and p->right==NULL){
if(p==root){
root=NULL;
return;
}
Node* parent=this->parent(p);
if(parent->left==p)parent->left=NULL;
else parent->right=NULL;
return;
}
else if((p->left==NULL and p->right!=NULL) or (p->left!=NULL and p->right==NULL)){
if(p==root){
if(p->right!=NULL) root=p->right;
else root=p->left;
return;
}
Node* parent=this->parent(p);
if(parent->left==p){
if(p->right!=NULL) parent->left=p->right;
else parent->left=p->left;
}
else{
if(p->right!=NULL) parent->right=p->right;
else parent->right=p->left;
}
return;
}
else{
Node* r=p->right;
Node* temp=p;
while(r->left!=NULL){
temp=r;
r=r->left;
}
p->ele=r->ele;
if(r->ele<temp->ele)temp->left=r->right;
else temp->right=r->right;
delete r;
}
}
int main(){
string ans="Y";
cout<<"1. Add an element."<<endl;
cout<<"2. Delete an element by copying."<<endl;
cout<<"3. Delete an element by merging."<<endl;
cout<<"4. Search an element."<<endl;
cout<<"5. Display the list(postorder)."<<endl;
cout<<"6. Display the list(preorder)."<<endl;
cout<<"7. Display the list(inorder)."<<endl;
BST A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
try{
switch(choice){
case 1:
n=0;
cout<<" Enter element to add: ";cin>>n;
A.insert(n);
break;
case 2:
n=0;
cout<<" Enter element to delete: ";cin>>n;
A.deletec(n);
cout<<"Element deleted."<<endl;
break;
case 3:
n=0;
cout<<" Enter element to delete: ";cin>>n;
A.deletem(n);
cout<<"Element deleted."<<endl;
break;
case 4:
n=0;
cout<<" Enter element to search: ";cin>>n;
if(A.search(n)) cout<<"Found."<<endl;
else cout<<"Not Found."<<endl;
break;
case 5:
A.postorder();
break;
case 6:
A.preorder();
break;
case 7:
A.inorder();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
}
catch(...){
cout<<"Invalid Input."<<endl;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
Output-
Q7. Code to implement AVL Tree as ADT.
#include <iostream>
#include <algorithm>
using namespace std;
class Node{
public:
int key;
Node *left;
Node *right;
int h; // height
Node(int key)
{
this->key = key;
left = NULL;
right = NULL;
h = 0;
}
friend class avl_tree;
};
class avl_tree{
Node *root;
void display(Node *);
Node *insert(Node *, int);
public:
avl_tree()
{
root = NULL;
}
// perform rotation
temp1->right = temp;
temp->left = temp2;
// update height
temp->h = 1 + max(height(temp->left), height(temp->right));
temp1->h = 1 + max(height(temp1->left), height(temp1->right));
// perform rotation
temp1->left = temp;
temp->right = temp2;
// update height
temp->h = 1 + max(height(temp->left), height(temp->right));
temp1->h = 1 + max(height(temp1->left), height(temp1->right));
int main(){
avl_tree t;
t.insert(2);
t.insert(5);
t.insert(8);
t.insert(4);
t.insert(1);
t.display();
cout<<'\n'<<t.search(20)->left->key;
return 0;
}
Output-
Q8. Code to add Long Integers using linked lists.
#include <iostream>
#include <sstream>
#include <cstring>
using namespace std;
class Node{
int ele;
Node* next;
Node* prev;
friend class LongInt;
public :
Node(int a=0,Node* p=NULL,Node* n=NULL ){
ele=a;
next=n;
prev=p;
}
};
class LongInt{
Node* header;
Node* trailer;
protected:
void addfront(int);
void addlast(int);
public:
LongInt(string txt="0"){
header=new Node;
trailer=new Node;
header->next=trailer;
trailer->prev=header;
header->prev=NULL;
trailer->next=NULL;
if(txt=="0")return;
int s=txt.length();
int c=0;
string zero="";
if(s%3!=0){
for(int i=0;i<(3-s%3);i++)
zero+="0";
txt=zero+txt;
}
s=txt.length();
int count=s/3;
int elem;
while(c<count){
string st;
st=txt.substr(c*3,3);
stringstream ss;
ss<<st;
ss>>elem;
this->addlast(elem);
c++;
}
}
~LongInt(){
Node* currentnode=header->next;
while(currentnode!=trailer){
Node* tempnode=currentnode;
currentnode=currentnode->next;
delete tempnode;
}
}
bool empty();
void addint(LongInt& other);
void display();
};
bool LongInt::empty(){
if(header->next==trailer)return 1;
else return 0;
}
void LongInt::addfront(int x){
Node* newnode=new Node;
newnode->ele=x;
newnode->next=header->next;
newnode->prev=header;
header->next->prev=newnode;
header->next=newnode;
}
void LongInt::addlast(int x){
Node* newnode=new Node;
newnode->ele=x;
newnode->next=trailer;
newnode->prev=trailer->prev;
trailer->prev->next=newnode;
trailer->prev=newnode;
}
void LongInt::addint(LongInt& other){
cout<<"Sum is: ";
if(empty())other.display();
else if(other.empty())this->display();
LongInt temp;
Node* temp1=trailer->prev;
Node* temp2=other.trailer->prev;
int carry=0;
int sum=0;
while(temp1!=header and temp2!=other.header){
sum=temp1->ele+temp2->ele+carry;
carry=sum/1000;
sum=sum%1000;
temp.addfront(sum);
temp1=temp1->prev;
temp2=temp2->prev;
}
while(temp1!=header){
sum=temp1->ele+carry;
carry=sum/1000;
sum=sum%1000;
temp.addfront(sum);
temp1=temp1->prev;
}
while(temp2!=other.header){
sum=temp2->ele+carry;
carry=sum/1000;
sum=sum%1000;
temp.addfront(sum);
temp2=temp2->prev;
}
if(carry!=0)temp.addfront(carry);
temp.display();
}
void LongInt::display(){
if(empty()) cout<<"Empty";
else{
Node* newnode=header->next;
while (newnode->next!=trailer){
cout<<newnode->ele<<",";
newnode=newnode->next;
}
cout<<newnode->ele;
}
cout<<endl;
}
int main(){
string st;
cout<<"Enter a number: ";cin>>st;
LongInt A(st);
cout<<"Enter number to add: ";cin>>st;
LongInt B(st);
LongInt C;
A.addint(B);
}
Output-
Q9. Code to add two polynomials with the help of linked lists.
#include <iostream>
using namespace std;
class Node{
int pow;
int coef;
Node* next;
friend class Poly;
public :
Node(int a=0,int b=0){
coef=a;
pow=b;
next=NULL;
}
};
class Poly{
Node* head;
Node* tail;
protected:
bool empty();
void addfront(int,int);
void addlast(int,int);
void delfront();
void dellast();
public:
Poly(bool f=0){
head=NULL;
tail=NULL;
if(f!=0){
char ans='y';
bool Poly::empty(){
if(head==NULL) return 1;
else return 0;
}
void Poly::addfront(int a,int b){
Node* newnode= new Node;
newnode->next=head;
newnode->coef=a;
newnode->pow=b;
head=newnode;
if(tail==NULL)tail=newnode;
}
void Poly::addlast(int a,int b){
if(this->empty()){
addfront(a,b);
return;
}
Node* newnode= new Node;
newnode->next=NULL;
newnode->coef=a;
newnode->pow=b;
tail->next=newnode;
tail=newnode;
}
void Poly::delfront(){
if(head==NULL){
cout<<"Empty List";
return;
}
Node* newnode= new Node;
newnode=head;
head=newnode->next;
delete newnode;
if(head==NULL)tail=NULL;
}
void Poly::dellast(){
if(head==NULL){
cout<<"Empty List";
return;
}
else if(head->next==NULL){
this->delfront();
return;
}
else{
Node* prevnode=new Node;
Node* tempnode=new Node;
tempnode=head;
prevnode=NULL;
while(tempnode->next!=NULL){
prevnode=tempnode;
tempnode=tempnode->next;
}
tail=prevnode;
prevnode->next=NULL;
delete tempnode;
}
}
void Poly::add(Poly& other){
Poly templist;
Node* temp1=head;
Node* temp2=other.head;
while(temp1!=NULL and temp2!=NULL){
if(temp1->pow < temp2->pow){
templist.addlast(temp2->coef,temp2->pow);
temp2=temp2->next;
}
else if(temp2->pow < temp1->pow){
templist.addlast(temp1->coef,temp1->pow);
temp1=temp1->next;
}
else{
templist.addlast(temp1->coef+temp2->coef,temp1->pow);
temp1=temp1->next;
temp2=temp2->next;
}
}
while(temp1!=NULL){
templist.addlast(temp1->coef,temp1->pow);
temp1=temp1->next;
}
while(temp2!=NULL){
templist.addlast(temp2->coef,temp2->pow);
temp2=temp2->next;
}
cout<<"After addition ";
templist.display();
}
void Poly::display(){
if(head==NULL)cout<<"Empty List";
else{
cout<<"The polynomial is: ";
Node* tempnode=head;
while(tempnode->next!=NULL){
cout<<tempnode->coef<<"*x^"<<tempnode->pow<<" + ";
tempnode=tempnode->next;
}
cout<<tempnode->coef<<"*x^"<<tempnode->pow;
}
cout<<endl;
}
int main(){
cout<<"Enter First Polynomial-"<<endl;
Poly A(1);
A.display();
cout<<"Enter Second Polynomial-"<<endl;
Poly B(1);
B.display();
A.add(B);
return 0;
}
Output-
Q10. Code to find the next greater elements in an array using stack.
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
vector<int> nextgreat(int arr[],int size){
vector<int> res(size);
stack<int> Index;
Index.push(0);
for(int i=1;i<size;i++){
while (!Index.empty() and arr[i]>=arr[Index.top()]){
res[Index.top()]=arr[i];
Index.pop();
}
Index.push(i);
}
while(!Index.empty()){
res[Index.top()]=-1;
Index.pop();
}
return res;
}
int main(){
int arr[7]={5,9,6,7,1,20,15};
cout<<"Input: "<<endl;
for(int i=0;i<7;i++){
cout<<arr[i]<<" ";
//cout<<"Enter "<<i+1<<"th element: ";cin>>arr[i];
}
cout<<endl;
vector<int> result=nextgreat(arr,7);
cout<<"Output: "<<endl;
for(int i=0;i<7;i++){
cout<<result[i]<<" ";
}
}
Output-
Q11. Write a code to implement heap.
#include<iostream>
using namespace std;
class max_heap{
int *aheap;
int capacity;
int heap_size;
public:
max_heap(int size){
capacity=size;
aheap=new int[size+1];
heap_size=0;
}
bool empty(){
if(heap_size==0)
return true;
else
return false;
}
void insert(int x){
if(heap_size==capacity)
throw "Heap full";
else if(empty()){
heap_size++;
aheap[heap_size]=x;
}
else{
heap_size++;
aheap[heap_size]=x;
int temp;
int size1=heap_size;
int size=heap_size/2;
while(x>aheap[size] && size!=0){
temp=aheap[size];
aheap[size]=x;
aheap[size1]=temp;
size1=size;
size=size/2;
}
}
}
void heapify(int i){
if(empty())
throw "Heap is empty";
int temp;
while(aheap[i]<aheap[i*2] || aheap[i]<aheap[i*2 +1]){
if(aheap[i*2]>aheap[i*2 +1]){
temp=aheap[i];
aheap[i]=aheap[i*2];
aheap[i*2]=temp;
i=i*2;
}
else if(aheap[i*2]<aheap[i*2 +1]){
temp=aheap[i];
aheap[i]=aheap[i*2 +1];
aheap[i*2 +1]=temp;
i=i*2 +1;
}
if(i>=heap_size)
break;
}
}
int maxheap(){
return aheap[1];
}
void extract_max(){
aheap[1]=aheap[heap_size];
heap_size--;
heapify(1);
}
void display(){
cout<<"Heap formed:\n";
for(int i=1;i<=heap_size;i++){
cout<<aheap[i]<<" ";
}
}
};
int main(){
int size;
cout<<"Enter size of heap: ";
cin>>size;cout<<endl;
max_heap a(size);
int choice=0;
cout<<"******Heap Operations******"<<endl;
cout<<"1. Insert a element to heap "<<endl;
cout<<"2. Display Max element from heap"<<endl;
cout<<"3. Remove a Maximum element from Heap"<<endl;
cout<<"4. Display heap"<<endl;
cout<<"5. Exit"<<endl;
while(true){
cout<<"Enter your choice: ";
cin>>choice;
if(choice==1){
int element;
cout<<"Enter the element to be added: ";
cin>>element;
a.insert(element);
cout<<"Element added successfully"<<endl;
}
else if(choice==2){
int max;
max=a.maxheap();
cout<<"max element:"<<max<<endl;
}
else if(choice==3){
a.extract_max();
cout<<"Max element removed\n";
}
else if(choice==4){
a.display();
cout<<endl;
}
else if(choice==5){
cout<<"Exiting";
break;
}
cout<<endl;
}
}
Output-
Q12. Implement Queue with Circular Linked List.
#include <iostream>
using namespace std;
class Node{
int ele;
Node* next;
friend class CLL;
public :
Node(int a=0){
ele=a;
next=NULL;
}
};
class CLL{
Node* cursor;
public:
CLL(){
cursor=NULL;
}
~CLL(){
while(!empty()){
remove();
}
}
bool empty()const;
int front()const;
int back()const;
void advance();
void add(int e);
void remove();
void removecursor();
void display()const;
};
class CLLQueue:private CLL{
int n=0;
CLL Queue;
public:
void enqueue(int e){
Queue.add(e);
Queue.advance();
n++;
}
void dequeue(){
Queue.remove();
n--;
}
int front(){
return Queue.front();
}
int size(){
return n;
}
bool empty(){
return (n==0);
}
void display(){
Queue.display();
}
};
bool CLL::empty()const{
return cursor==NULL;
}
int CLL::front()const{
if(empty())throw "Empty";
return cursor->next->ele;
}
int CLL::back()const{
if(empty())throw "Empty";
return cursor->ele;
}
void CLL::advance(){
if(empty())throw "Empty";
cursor=cursor->next;
}
void CLL::add(int e){
Node* newnode=new Node;
newnode->ele=e;
if(empty()){
newnode->next=newnode;
cursor=newnode;
}
else{
newnode->next=cursor->next;
cursor->next=newnode;
}
}
void CLL::remove(){
Node* tempnode=cursor->next;
if(tempnode==cursor)cursor=NULL;
else cursor->next=tempnode->next;
delete tempnode;
}
void CLL::removecursor(){
Node* tempnode=cursor->next;
if(tempnode==cursor)cursor=NULL;
while(tempnode->next!=cursor)tempnode=tempnode->next;
tempnode->next=cursor->next;
delete cursor;
cursor=tempnode;
}
void CLL::display()const{
if(empty()){
cout<<"Empty list.";
return;
}
cout<<"The Queue is: ";
Node* tempnode=cursor->next;
do{
cout<<tempnode->ele<<" ";
tempnode=tempnode->next;
}
while(tempnode!=cursor->next);
cout<<endl;
}
int main(){
string ans="Y";
cout<<"1. Add an element at last."<<endl;
cout<<"2. Delete an element from front."<<endl;
cout<<"3. Show front."<<endl;
cout<<"4. Size of queue."<<endl;
cout<<"5. Display the list."<<endl;
CLLQueue A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
try{
switch(choice){
case 1:
n=0;
cout<<" Enter element to add: ";cin>>n;
A.enqueue(n);
break;
case 2:
A.dequeue();
cout<<"First element deleted."<<endl;
break;
case 3:
cout<<"First element is "<<A.front()<<endl;
break;
case 4:
cout<<"Size of queue is "<<A.size()<<endl;
break;
case 5:
A.display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
}
catch(...){
cout<<"Invalid Input."<<endl;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
}
Q13. Write a code to implement Deque with Doubly Linked List.
#include <iostream>
using namespace std;
class Node{
int ele;
Node* next;
Node* prev;
friend class DLL;
public :
Node(int a=0,Node* p=NULL,Node* n=NULL ){
ele=a;
next=n;
prev=p;
}
};
class DLL{
Node* header;
Node* trailer;
protected:
void add(Node*,int&);
void remove(Node*);
public:
DLL(){
header=new Node;
trailer=new Node;
header->next=trailer;
trailer->prev=header;
header->prev=NULL;
trailer->next=NULL;
}
~DLL(){
Node* currentnode=header->next;
while(currentnode!=trailer){
Node* tempnode=currentnode;
currentnode=currentnode->next;
delete tempnode;
}
delete header;
delete trailer;
}
bool empty();
int& frontele();
int& lastele();
void addfront(int);
void addlast(int);
void delfront();
void dellast();
void display();
int& findmiddle();
void reverse();
};
class Deque:private DLL{
int size;
DLL Queue;
public:
void insertfront(int e){
Queue.addfront(e);
size++;
}
void insertback(int e){
Queue.addlast(e);
size++;
}
void erasefront(){
Queue.delfront();
size--;
}
void eraseback(){
Queue.dellast();
size--;
}
int front(){
return Queue.frontele();
}
int back(){
return Queue.lastele();
}
bool empty(){
return (size==0);
}
void display(){
Queue.display();
}
};
void DLL::add(Node* nextnode,int& x){ //add before
Node* newnode=new Node(x,nextnode->prev,nextnode); //element,prev,next
nextnode->prev->next=newnode;
nextnode->prev=newnode;
}
void DLL::remove(Node* currentnode){
currentnode->next->prev=currentnode->prev;
currentnode->prev->next=currentnode->next;
delete currentnode;
}
bool DLL::empty(){
if(header->next==trailer)return 1;
else return 0;
}
int& DLL::frontele(){
if (empty())throw"Empty List";
return header->next->ele;
}
int& DLL::lastele(){
if (empty())throw"Empty List";
return trailer->prev->ele;
}
void DLL::addfront(int x){
Node* newnode=new Node;
newnode->ele=x;
newnode->next=header->next;
newnode->prev=header;
header->next->prev=newnode;
header->next=newnode;
}
void DLL::addlast(int x){
Node* newnode=new Node;
newnode->ele=x;
newnode->next=trailer;
newnode->prev=trailer->prev;
trailer->prev->next=newnode;
trailer->prev=newnode;
}
void DLL::delfront(){
Node* newnode=header->next;
header->next=newnode->next;
newnode->next->prev=newnode->prev;
delete newnode;
}
void DLL::dellast(){
Node* newnode=trailer->prev;
newnode->prev->next=newnode->next;
trailer->prev=newnode->prev;
delete newnode;
}
void DLL::display(){
if(empty()) cout<<"Queue List";
else{
Node* newnode=header->next;
cout<<"Queue is: ";
while (newnode!=trailer){
cout<<newnode->ele<<" ";
newnode=newnode->next;
}
}
cout<<endl;
}
int& DLL::findmiddle(){
Node* newnode=header->next;
Node* new2=trailer->prev;
while(newnode!=new2 and newnode->next!=new2){
newnode=newnode->next;
new2=new2->prev;
}
return newnode->ele;
}
void DLL::reverse(){
if (empty()) return;
Node* first=header->next;
Node* last=trailer->prev;
while(first!=last and first->next!=last){
int temp=first->ele;
first->ele=last->ele;
last->ele=temp;
first=first->next;
last=last->prev;
}
}
int main(){
string ans="Y";
cout<<"1. Add an element at front."<<endl;
cout<<"2. Add an element at last."<<endl;
cout<<"3. Delete an element from front."<<endl;
cout<<"4. Delete an element from last."<<endl;
cout<<"5. Show front."<<endl;
cout<<"6. Show back."<<endl;
cout<<"7. Display the list."<<endl;
Deque A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
try{
switch(choice){
case 1:
n=0;
cout<<" Enter element to add at front: ";cin>>n;
A.insertfront(n);
break;
case 2:
n=0;
cout<<" Enter element to add at back: ";cin>>n;
A.insertback(n);
break;
case 3:
A.erasefront();
cout<<"First element deleted."<<endl;
break;
case 4:
A.eraseback();
cout<<"Last element deleted."<<endl;
break;
case 5:
cout<<"First element is "<<A.front()<<endl;
break;
case 6:
cout<<"Last element is "<<A.back()<<endl;
break;
case 7:
A.display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
}
catch(...){
cout<<"Invalid Input."<<endl;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
}
Q14. Write a code to implement Sorted Singly List.
#include <iostream>
using namespace std;
class Node{
public :
int ele;
Node* next;
friend class SLL2;
Node(int a=0){
ele=a;
next=NULL;
}
};
class SLL2{
protected:
Node* head;
Node* tail;
public:
SLL2(){
head=NULL;
tail=NULL;
}
SLL2(SLL2& a);
~SLL2();
bool empty();
void addfront(int a);
void addlast(int a);
void delindex(int a);
void delfront();
void dellast();
bool search(int a);
void display();
};
class SortedSLL:protected SLL2{
public:
void insert(int x);
void delindex(int a){
SLL2::delindex(a);
}
void delfront(){
SLL2::delfront();
}
void dellast(){
SLL2::dellast();
}
bool search(int a){
SLL2::search(a);
}
void display(){
SLL2::display();
}
};
SLL2::SLL2(SLL2& a){
if(a.head==NULL){
head=NULL;
tail=NULL;
return;
}
head=new Node(a.head->ele);
Node* tempnode=a.head;
Node* currentnode=head;
while(tempnode->next!=NULL){
currentnode->next=new Node(tempnode->next->ele);
currentnode=currentnode->next;
tempnode=tempnode->next;
}
tail=new Node(a.tail->ele);
}
SLL2::~SLL2(){
Node* currentnode=head;
while(currentnode!=NULL){
Node* tempnode=currentnode;
currentnode=currentnode->next;
delete tempnode;
}
}
bool SLL2::empty(){
if(head==NULL) return 1;
else return 0;
}
void SLL2::addfront(int a){
Node* newnode= new Node;
newnode->next=head;
newnode->ele=a;
head=newnode;
if(tail==NULL)tail=newnode;
}
void SLL2::addlast(int a){
Node* newnode= new Node;
newnode->next=NULL;
newnode->ele=a;
if(this->empty()){
head=newnode;
tail=newnode;
return;
}
tail->next=newnode;
tail=newnode;
}
void SLL2::delindex(int a){
if (head==NULL){
cout<<"Empty List, can't delete";
return;
}
else if (a==1){
this->delfront();
return;
}
else{
Node* tempnode = head;
int count=1;
while(tempnode!=NULL and count<a-1){
tempnode=tempnode->next;
count++;
}
if(tempnode==NULL)
{
cout<<"Invalid Position";
return;
}
else{
Node* nextnode=tempnode->next;
tempnode->next=nextnode->next;
delete nextnode;
if(tempnode->next==NULL)tail=tempnode;
}
}
}
void SLL2::delfront(){
if(head==NULL){
cout<<"Empty List";
return;
}
Node* newnode= new Node;
newnode=head;
head=newnode->next;
delete newnode;
if(head==NULL)tail=NULL;
}
void SLL2::dellast(){
if(head==NULL){
cout<<"Empty List";
return;
}
else if(head->next==NULL){
this->delfront();
return;
}
else{
Node* prevnode=new Node;
Node* tempnode=new Node;
tempnode=head;
prevnode=NULL;
while(tempnode->next!=NULL){
prevnode=tempnode;
tempnode=tempnode->next;
}
tail=prevnode;
prevnode->next=NULL;
delete tempnode;
}
}
bool SLL2::search(int a){
if(head==NULL)throw "Empty List";
else{
Node* tempnode=head;
while(tempnode!=NULL and tempnode->ele!=a){
tempnode=tempnode->next;
}
if(tempnode==NULL) return 0;
else if(tempnode->ele!=a) return 1;
}
}
void SLL2::display(){
if(head==NULL)cout<<"Empty List";
else{
cout<<"The list is: ";
Node* tempnode=head;
while(tempnode!=NULL){
cout<<tempnode->ele<<" ";
tempnode=tempnode->next;
}
}
cout<<endl;
}
void SortedSLL::insert(int x){
if (empty())addfront(x);
else if(x<head->ele)addfront(x);
else if(x>tail->ele)addlast(x);
else{
Node* newnode=new Node(x);
Node* prevnode=head;
Node* tempnode=head->next;
while(tempnode->ele<x){
prevnode=tempnode;
tempnode=tempnode->next;
}
prevnode->next=newnode;
newnode->next=tempnode;
}
}
int main()
{
string ans="Y";
cout<<"1. Insert an element."<<endl;
cout<<"2. Delete an element from front."<<endl;
cout<<"3. Delete an element from last."<<endl;
cout<<"4. Delete an element at given index."<<endl;
cout<<"5. Search an element."<<endl;
cout<<"6. Display the list."<<endl;
SortedSLL A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
switch(choice){
case 1:
n=0;
cout<<" Enter element to add: ";cin>>n;
A.insert(n);
break;
case 2:
A.delfront();
cout<<"First element deleted."<<endl;
break;
case 3:
A.dellast();
cout<<"Last element deleted."<<endl;
break;
case 4:
ind=0;
cout<<" Enter index: ";cin>>ind;
A.delindex(ind);
cout<<" Element "<<ind<<" deleted."<<endl;
break;
case 5:
n=0;
cout<<" Enter element to search: ";cin>>n;
if(A.search(n)) cout<<"Found."<<endl;
else cout<<"Not Found."<<endl;
break;
case 6:
A.display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
}
Q15. Write a code to implement Stack with Array.
#include <iostream>
using namespace std;
class Stack{
int* stack;
int n;
int top;
public:
Stack(int N=20){
n=N;
top=-1;
stack=new int[n];
}
~Stack(){
delete stack;
}
void push(int val);
void pop();
void topele();
void size();
void empty();
void display();
};
void Stack::push(int val){
if(top>=n-1)
cout<<" Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
cout<<" Element added"<<endl;
}
}
void Stack::pop(){
if(top<=-1)
cout<<" Stack Underflow"<<endl;
else {
cout<<" The popped element is "<< stack[top] <<endl;
top--;
}
}
void Stack::topele(){
if(top<=-1)
cout<<" Stack is Empty"<<endl;
else {
cout<<" The top element is "<< stack[top] <<endl;
}
}
void Stack::size(){
if(top<=-1)
cout<<" Stack is Empty"<<endl;
else {
cout<<" The size is "<<top+1<<endl;
}
}
void Stack::empty(){
if(top<=-1)
cout<<" Stack is Empty"<<endl;
else {
cout<<" Stack is Unempty"<<endl;
}
}
void Stack::display() {
if(top>=0) {
cout<<" Stack elements are: ";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
}
else
cout<<" Stack is empty"<<endl;
}
int main() {
Stack S;
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Top element in stack"<<endl;
cout<<"4) Size of stack"<<endl;
cout<<"5) If stack empty"<<endl;
cout<<"6) Display stack"<<endl;
cout<<"7) Exit"<<endl;
do {
cout<<"Enter choice: ";
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed: ";
cin>>val;
S.push(val);
break;
}
case 2: {
S.pop();
break;
}
case 3: {
S.topele();
break;
}
case 4: {
S.size();
break;
}
case 5: {
S.empty();
break;
}
case 6: {
S.display();
break;
}
case 7: {
cout<<" Exit"<<endl;
break;
}
default: {
cout<<" Invalid Choice"<<endl;
}
}
}
while(ch!=7);
return 0;
}
Q16. Write a code to check if parenthesis is valid in a string.
#include <iostream>
#include <stack>
#include <cstring>
using namespace std;
int infunc(char S){
if(S=='(')return 10;
else if(S==')')return 11;
else if(S=='{')return 20;
else if(S=='}')return 21;
else if(S=='[')return 30;
else if(S==']')return 31;
else if(S==',')return 1;
else return 0;
}
bool valid(string S){
int size=S.length();
stack<char> Stack;
char buffer='0';
for(int i=0;i<size;i++){
if(infunc(S[i])==0){
buffer+=S[i];
}
else if(infunc(S[i])==1 and infunc(S[i-1])==0){
Stack.push(buffer);
buffer='0';
}
else if(infunc(S[i])==10 or infunc(S[i])==20 or infunc(S[i])==30)
Stack.push(S[i]);
else if(infunc(S[i])==11 or infunc(S[i])==21 or infunc(S[i])==31){
while(infunc(Stack.top())!=infunc(S[i])-1){
if(infunc(Stack.top())!=0)return 0;
Stack.pop();
}
Stack.pop();
}
}
if(Stack.size()==0)return 1;
else return 0;
}
int main(){
string equation="[A,BB,cc,{abc,bd,c},sw]";
cout<<"Valid: "<<valid(equation);
}
Q17. Write a code to simulate solving of a Tower of Hanoi.
#include <iostream>
using namespace std;
Output-