Professional Documents
Culture Documents
and
Algorithms
Practical File
Group Members
Anuj Gupta – 2018UIT2504
Tejas Gupta – 2018UIT2508
Siddharth Saini – 2018UIT2502
Nitin Porwal – 2018UIT2519
Rishabh Jain – 2018UIT2527
Hardik Aggarwal – 2018UIT2532
Implement two stacks in an array.
#include<iostream>
#include<stdlib.h>
class twoStacks
{
int *arr;
int size;
int top1, top2;
public:
twoStacks(int n)
{
size = n;
arr = new int[n];
top1 = -1;
top2 = size;
}
void push1(int x)
{
if (top1 < top2 - 1)
{
top1++;
arr[top1] = x;
}
else
{
cout << "Stack Overflow";
return;
}
}
void push2(int x)
{
if (top1 < top2 - 1)
{
top2--;
arr[top2] = x;
}
else
{
cout << "Stack Overflow";
return;
}
}
int pop1()
{
if (top1 >= 0 )
{
int x = arr[top1];
top1--;
return x;
}
else
{
cout << "Stack UnderFlow";
return INT_MIN;
}
}
int pop2()
{
if (top2 < size)
{
int x = arr[top2];
top2++;
return x;
}
else
{
cout << "Stack UnderFlow";
return INT_MIN;
}
}
};
int main()
{
int n;
cout<<endl<<"Enter the size of array : ";
cin>>n;
twoStacks ts(n);
int ch;
do
{
cout<<endl<<"Enter 1 to push in 1st stack";
cout<<endl<<"Enter 2 to push in 2nd stack";
cout<<endl<<"Enter 3 to pop from 1st stack";
cout<<endl<<"Enter 4 to pop from 2nd stack";
cout<<endl<<"Enter 0 to exit";
cout<<endl<<"Enter the choice: ";
cin>>ch;
int x;
switch(ch)
{
case 0: exit(0);
#include<bits/stdc++.h>
int main()
{
stack<int> prim,sec;
int ch;
do
{
cout<<endl<<endl<<"Enter 1 to push";
cout<<endl<<"Enter 2 to pop";
cout<<endl<<"Enter 3 to display";
cout<<endl<<"Enter 0 to exit";
cout<<endl<<"Enter your choice : ";
cin>>ch;
if(ch==0)
{
exit(0);
}
else if(ch==1)
{
cout<<"Enter the element you want to insert : ";
int n;
cin>>n;
prim.push(n);
}
else if(ch==2)
{
if(prim.empty()) cout<<"\nUnderflow";
else
{
while(!prim.empty())
{
int p=prim.top();
prim.pop();
sec.push(p);
}
cout<<endl<<"The popped element is "<<sec.top();
sec.pop();
while(!sec.empty())
{
int p=sec.top();
sec.pop();
prim.push(p);
}
}
}
else if(ch==3)
{
if(prim.empty()) cout<<"\nUnderflow";
else
{
while(!prim.empty())
{
int p=prim.top();
prim.pop();
sec.push(p);
}
while(!sec.empty())
{
int p=sec.top();
sec.pop();
prim.push(p);
cout<<endl<<"\t"<<p;
}
}
}
else
{
cout<<endl<<"Wrong choice....";
}
}while(ch);
}
} Node;
return temp;
}
if ((*head)->priority > p) {
temp->next = *head;
(*head) = temp;
}
else
{
while (start->next != NULL && start->next-
>priority < p) start = start->next;
temp->next = start->next;
start->next = temp;
}
}
int main()
{
Node* pq = newNode(4, 1);
push(&pq, 5, 2);
push(&pq, 6, 3);
push(&pq, 7, 0);
while (!isEmpty(&pq))
{
printf("%d ", peek(&pq));
pop(&pq);
}
return 0;
}
Implement Quicksort using Doubly Linked List
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node *prev;
};
swap(&(i->data), &(j->data));
}
}
i = (i == NULL)? l : i->next;
swap(&(i->data), &(h->data));
return i;
}
_quickSort(head, h);
}
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL) (*head_ref)->prev = new_node ;
(*head_ref) = new_node;
}
int main()
{
Node *a = NULL;
push(&a, 5);
push(&a, 20);
push(&a, 4);
push(&a, 3);
push(&a, 30);
quickSort(a);
return 0;
}
int prec(char a)
{
if(a=='%') return 3;
else if(a=='*' || a=='/') return 2;
else if(a=='+' || a=='-') return 1;
else return -1;
}
for(int i=0;i<exp.size();i++)
{
if(exp[i]>='0' && exp[i]<='9')
{
post += exp[i];
}
else if(exp[i]=='(')
{
stack += exp[i];
}
else if(exp[i]==')')
{
while(stack[stack.size()-1]!='(')
{
char a = stack[stack.size()-1];
stack.pop_back();
post += a;
}
stack.pop_back();
}
else
{
while(prec(exp[i])<prec(stack[stack.size()-1]))
{
char a = stack[stack.size()-1];
stack.pop_back();
post+=a;
}
stack += exp[i];
}
}
while(stack[stack.size()-1]!='N')
{
char a = stack[stack.size()-1];
stack.pop_back();
post += a;
}
return post;
}
int main()
{
string exp,post;
post = intopost(exp);
cout<<endl<<"The postfix expression : "<<post<<endl;
int x=evaluate(post);
cout<<endl<<"The Answer is : "<<x<<endl;
return 0;
}
struct node
{
int data;
struct node *l, *r;
};
void main()
{
int ch,item;
struct node *root=NULL;
do
{
printf("\nEnter 1 to insert");
printf("\nEnter 2 to display");
printf("\nEnter 3 to search");
printf("\nEnter 4 to delete");
printf("\nEnter 0 to exit");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch (ch)
{
case 0: break;
case 2: display(root);
break;
p->data=item;
p->l=NULL;
p->r=NULL;
return p;
}
return root;
}
else
{
if(root->l==NULL)
{
struct node *temp;
temp=root->r;
free(root);
return temp;
}
else if(root->r==NULL)
{
struct node *temp;
temp=root->l;
free(root);
return temp;
}
else
{
struct node *temp, *par;
temp=root->r;
par=root->r;
while(temp->l!=NULL)
{
par=temp;
temp=temp->l;
}
if(par==temp)
{
par->l=root->l;
return par;
}
else
{
par->l=temp->r;
temp->r=root->r;
temp->l=root->l;
return temp;
}
}
}
}
else
{
display(root->l);
printf("\n%d",root->data);
display(root->r);
}
}
#include<bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node *right;
node *left;
int height;
node(int x)
{
data=x;
right=NULL;
left=NULL;
height=1;
}
};
int main()
{
int ch;
node *root;
root=NULL;
do
{
cout<<endl;
cout<<"Enter 1 to insert"<<endl;
cout<<"Enter 2 to display"<<endl;
cout<<"Enter 3 to delete"<<endl;
cout<<"Enter 0 to exit"<<endl;
cout<<"Enter you choice : ";
cin>>ch;
switch(ch)
{
case 0: exit(0);
case 1: int x;
cout<<endl<<"Enter the element you want to insert : ";
cin>>x;
root=insert(root,x);
break;
case 2: cout<<endl;
display(root);
break;
node* newnode(int x)
{
node *p;
p=new node(x);
return p;
}
l->right=root;
root->left=r;
root->height = 1 + max(getheight(root->left),getheight(root->right));
l->height = 1 + max(getheight(l->left),getheight(l->right));
return l;
}
r->left=root;
root->right=l;
root->height = 1 + max(getheight(root->left),getheight(root->right));
r->height = 1 + max(getheight(r->left),getheight(r->right));
return r;
}
root->height = 1 + max(getheight(root->left),getheight(root->right));
int bf;
bf = getheight(root->left) - getheight(root->right);
cout<<bf;
if(bf>1)
{
if(x<root->left->data) root=rightrotate(root);
else
{
root->left=leftrotate(root->left);
root=rightrotate(root);
}
}
if(bf<-1)
{
if(x>root->right->data) root=leftrotate(root);
else
{
root->right=rightrotate(root->right);
root=leftrotate(root);
}
}
return root;
}
}
else if(root->right==NULL)
{
temp=root->left;
delete root;
}
else
{
node *par;
temp=root->right;
par=root->right;
while(temp->left!=NULL)
{
par=temp;
temp=temp->left;
}
if(par==temp)
{
temp->left=root->left;
temp->height = 1 + max(getheight(root-
>left),getheight(root->right));
}
else
{
par->left=temp->right;
par->height--;
temp->right=root->right;
temp->left=root->left;
root->height = 1 + max(getheight(root-
>left),getheight(root->right));
}
}
return temp;
}
root->height = 1 + max(getheight(root->left),getheight(root->right));
int bf;
bf = getheight(root->left) - getheight(root->right);
if(bf>1)
{
if(x<root->left->data) root=rightrotate(root);
else
{
root->left=leftrotate(root->left);
root=rightrotate(root);
}
}
if(bf<-1)
{
if(x>root->right->data) root=leftrotate(root);
else
{
root->right=rightrotate(root->right);
root=leftrotate(root);
}
}
return root;
}
}