You are on page 1of 22

Data Structures

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>

using namespace std;

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);

case 1: cout<<"Enter the element you want to push: ";


cin>>x;
ts.push1(x);
break;
case 2: cout<<"Enter the element you want to push: ";
cin>>x;
ts.push2(x);
break;
case 3: x=ts.pop1();
if(x!= INT_MIN) cout<<"The popped element is: "<<x;
break;
case 4: x=ts.pop2();
if(x!= INT_MIN) cout<<"The popped element is: "<<x;
break;
default: cout<<"Wrong choice...."<<endl;
break;
}
}while(ch);
}

Implement Queue using two Stacks

#include<bits/stdc++.h>

using namespace std;

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);
}

Implement Sorted Linked List


#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
}*p = NULL, *head = NULL, *q = NULL, *np = NULL;
int c = 0;
void create(int x)
{
np = new node;
np->data = x;
np->next = NULL;
if (c == 0)
{
head = np;
p = head;
p->next = head;
c++;
}
else if (c == 1)
{
p = head;
q = p;
if (np->data < p->data)
{
np->next = p;
head = np;
p->next = np;
}
else if (np->data > p->data)
{
p->next = np;
np->next = head;
}
c++;
}
else
{
p = head;
q = p;
if (np->data < p->data)
{
np->next = p;
head = np;
do
{
p = p->next;
}
while (p->next != q);
p->next=head;
}
else if (np->data > p->data)
{
while (p->next !=head && q->data < np->data)
{
q = p;
p = p->next;
if (p->next == head && (p->data < np->data))
{
p->next = np;
np->next = head;
}
else if (np->data < p->data)
{
q->next = np;
np->next = p;
break;
}
}
}
}
}
void traverse(int i)
{
node *t = head;
int c = 0;
while (c < i)
{
cout<<t->data<<"\t";
t = t->next;
c++;
}
}
int main()
{
int i = 0, n, x;
cout<<"enter the no of nodes\n";
cin>>n;
while (i < n)
{
cout<<"\nenter value of node\n";
cin>>x;
create(x);
i++;
}
cout<<"\n\nlinear display of nodes currently present in circularly linked
list....\n\n";
traverse(n);
getch();
}

Implement Priority Queue using Linked List


#include <stdio.h>
#include <stdlib.h>

typedef struct node {


int data;
int priority;

struct node* next;

} Node;

Node* newNode(int d, int p)


{
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = d;
temp->priority = p;
temp->next = NULL;

return temp;
}

int peek(Node** head)


{
return (*head)->data;
}

void pop(Node** head)


{
Node* temp = *head;
(*head) = (*head)->next;
free(temp);
}

void push(Node** head, int d, int p)


{
Node* start = (*head);
Node* temp = newNode(d, p);

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 isEmpty(Node** head)


{
return (*head) == NULL;
}

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;
};

void swap ( int* a, int* b )


{ int t = *a; *a = *b; *b = t; }

Node *lastNode(Node *root)


{
while (root && root->next)
root = root->next;
return root;
}

Node* partition(Node *l, Node *h)


{
int x = h->data;
Node *i = l->prev;

for (Node *j = l; j != h; j = j->next)


{
if (j->data <= x)
{
i = (i == NULL)? l : i->next;

swap(&(i->data), &(j->data));
}
}
i = (i == NULL)? l : i->next;
swap(&(i->data), &(h->data));
return i;
}

void _quickSort(Node* l, Node *h)


{
if (h != NULL && l != h && l != h->next)
{
Node *p = partition(l, h);
_quickSort(l, p->prev);
_quickSort(p->next, h);
}
}

void quickSort(Node *head)


{
Node *h = lastNode(head);

_quickSort(head, h);
}

void printList(Node *head)


{
while (head)
{
cout << head->data << " ";
head = head->next;
}
cout << endl;
}

void push(Node** head_ref, int new_data)


{
Node* new_node = new Node;
new_node->data = new_data;

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);

cout << "Linked List before sorting \n";


printList(a);

quickSort(a);

cout << "Linked List after sorting \n";


printList(a);

return 0;
}

Evaluate infix Arithmetic expression


#include<bits/stdc++.h>
#include<math.h>

using namespace std;

int prec(char a)
{
if(a=='%') return 3;
else if(a=='*' || a=='/') return 2;
else if(a=='+' || a=='-') return 1;
else return -1;
}

string intopost(string exp)


{
string post;
string stack;
stack += 'N';

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 evaluate(string post)


{
vector<int> stack;
int a,b,x;
for(int i=0;i<post.size();i++)
{
if(post[i]>='0' && post[i]<='9')
{
stack.push_back(post[i]-'0');
}
else
{
a = stack[stack.size()-1];
stack.pop_back();
b = stack[stack.size()-1];
stack.pop_back();
switch(post[i])
{
case '+': x = a+b;
break;
case '-': x = b-a;
break;
case '*': x = b*a;
break;
case '/': x = b/a;
break;
case '%': x = b%a;
break;
}
stack.push_back(x);
}
}
return stack[0];
}

int main()
{
string exp,post;

cout<<"\nEnter the Expression(no spaces) : ";


cin>>exp;

post = intopost(exp);
cout<<endl<<"The postfix expression : "<<post<<endl;

int x=evaluate(post);
cout<<endl<<"The Answer is : "<<x<<endl;
return 0;
}

Implement Binary Search Tree and display output in tree format.


#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *l, *r;
};

struct node* insert(struct node *,int);


struct node* create(int item);
void search(struct node *,int);
struct node* delete(struct node *,int);
void display(struct node *);

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 1: printf("\nEnter the value : ");


scanf("%d",&item);
root=insert(root,item);
break;

case 2: display(root);
break;

case 3: printf("\nEnter the value : ");


scanf("%d",&item);
search(root,item);
break;

case 4: printf("\nEnter the value : ");


scanf("%d",&item);
root=delete(root,item);
break;

default: printf("\nWrong choice....");


break;
}
} while (ch);
}

struct node* create(int item)


{
struct node *p;
p=(struct node *)malloc(sizeof(struct node));

p->data=item;
p->l=NULL;
p->r=NULL;

return p;
}

struct node* insert(struct node *root,int item)


{
if(root==NULL) root=create(item);

else if(item<root->data) root->l=insert(root->l,item);


else root->r=insert(root->r,item);

return root;
}

void search(struct node *temp,int item)


{
while(temp!=NULL)
{
if(temp->data==item) { printf("\nElement found"); return; }

else if (item>temp->data) temp=temp->r;

else if (item<temp->data) temp=temp->l;


}

printf("\nElement not found");


return;
}

struct node* delete(struct node *root,int item)


{
if(root==NULL) return root;

else if(item<root->data) root->l=delete(root->l,item);

else if(item>root->data) root->r=delete(root->r,item);

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;
}
}
}
}

void display(struct node *root)


{
if(root==NULL) return;

else
{
display(root->l);
printf("\n%d",root->data);
display(root->r);
}
}

Implement AVL Trees

#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;
}
};

node* newnode(int x);


node* insert(node *root,int x);
void display(node *root);
node* del(node *root,int x);

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;

case 3: cout<<endl<<"Enter the element you want to delete : ";


cin>>x;
root=del(root,x);
break;
default: cout<<"Wrong Choice...."<<endl;
break;
}
}while(ch);
}

node* newnode(int x)
{
node *p;
p=new node(x);
return p;
}

int max(int a,int b)


{
if(a>b) return a;
else return b;
}
int getheight(node *root)
{
if(root==NULL) return 0;
else return root->height;
}

node* rightrotate(node *root)


{
node *l = root->left;
node *r = l->right;

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;
}

node* leftrotate(node *root)


{
node *r = root->right;
node *l = r->left;

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;
}

node* insert(node *root,int x)


{
if(root==NULL)
{
root=newnode(x);
return root;
}
else
{
if(x<root->data) root->left=insert(root->left,x);
else root->right=insert(root->right,x);

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;
}
}

node* del(node *root,int x)


{
if(root==NULL) return root;
else
{
if(x<root->data) root->left=del(root->left,x);
else if(x>root->data) root->right=del(root->right,x);
else
{
node *temp;
if(root->left==NULL)
{
temp=root->right;
delete 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;
}
}

void display(node *root)


{
if(root==NULL) return;
cout<<"\t"<<root->data;
display(root->left);
display(root->right);
}

You might also like