You are on page 1of 49

PROGRAM 1: STACK USING ARRAY

CODING:
#include<iostream.h>
#include<conio.h>
struct stack
{
int i, n, top,ch,a[];
public:
stack()
{
top=0;
}
void size();
void choice();
void push();
void pop();
void display();
};
void stack::size()
{
cout<<"\nEnter Stack size:";
cin>>n;
}
void stack::choice()
{
do
{
cout<<"\n1.Push";
cout<<"\n2.Pop";
cout<<"\n3.Display";
cout<<"\n4.Exit";
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
}
}
while(ch!=4);
}
void stack::push()
{
if(top>=n)
{
cout<<"\nStack is Full";
}
else
{
top = top+1;
cout<<"\nEnter element";
cin>>a[top];
}
}
void stack::pop()
{
if(top==0)
{
cout<<"\nStack is Empty";
}
else
{
top = top-1;
}
}
void stack::display()
{
if(top==0)
{
cout<<"\nStack is empty";
}
else
{
cout<<"\nElements in stack are ";
for(i=1;i<=top;i++)
{
cout<<"\n"<<a[i];
}
}
}
void main()
{
clrscr();
stack s;
cout<<"\nSTACK OPERATION";
s.size();
s.choice();
getch();
}

OUTPUT:
PROGRAM 2: QUEUE USING ARRAY
CODING:
#include<iostream.h>
int queue[100], n = 100, front = -1, rear = -1;
void insert()
{
int value;
if(rear == n-1)
cout<<"Queue overflow"<< endl;
else
{
if(front==-1)
front=0;
cout<<"Insert element in queue"<< endl;
cin>>value;
rear++;
queue[rear]=value;
}
}
void del()
{
if(front==-1||front>rear)
{
cout<<"Queue underflow";
return;
}
else
{
cout<<"Element deleted from queue is "<<queue[front]<<endl;
front++;
}
}
void display()
{
if(front==-1)
cout<<"Queue is empty"<<endl;
else
{
cout<<"Queue Elements are ";
for(int i=front; i<=rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}
}
int main()
{
int ch;
cout<<"1.Insert"<<endl;
cout<<"2.Delete"<<endl;
cout<<"3.Display"<<endl;
cout<<"4.Exit"<<endl;
do
{
cout<<"Enter your choice "<<endl;
cin>>ch;
switch(ch)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
cout<<"Exit"<<endl;
break;
default:
cout<<"Invalid choice"<<endl;
}
}
while(ch!=4);
return 0;
}

OUTPUT:
PROGRAM 3: DOUBLY LINKED LIST AND CIRCULAR LINKED
LIST
DOUBLY LINKED LIST
CODING
#include<iostream.h>
#include<malloc.h>
#include<conio.h>
struct Node
{
int data;
struct Node *prev;
struct Node *next;
};
struct Node* head = NULL;
void insert(int newdata)
{
struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));
newnode->data = newdata;
newnode->prev = NULL;
newnode->next = head;
if(head != NULL)
head->prev = newnode;
head =newnode;
}
void display()
{
struct Node* ptr;
ptr = head;
while(ptr != NULL)
{
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
int main()
{
clrscr();
insert(25);
insert(14);
insert(6);
insert(13);
insert(37);
cout<<"The doubly linked list is: ";
display();
getch();
return 0;
}

OUTPUT:
CIRCULAR LINKED LIST
CODING
#include<iostream.h>
#include<conio.h>
#include<malloc.h>
struct Node {
int data;
struct Node *next;
};
struct Node* head = NULL;
void insert(int newdata) {
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
struct Node *ptr = head;
newnode->data = newdata;
newnode->next = head;
if (head!= NULL) {
while (ptr->next != head)
ptr = ptr->next;
ptr->next = newnode;
}
else
newnode->next = newnode;
head = newnode;
}
void display()
{
struct Node* ptr;
ptr = head;
do
{
cout<<ptr->data <<" ";
ptr = ptr->next;
}
while(ptr != head);
}
int main()
{
clrscr();
insert(32);
insert(18);
insert(74);
insert(27);
insert(19);
cout<<"The circular linked list is: ";
display();
getch();
return 0;
}

OUTPUT:
PROGRAM 5: TREE TRAVERSAL METHODS
CODING:
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
struct st
{
int data;
struct st*left;
struct st*right;
};
struct st*root = NULL, *temp;
void insertElements();
void preorder(struct st*);
void inorder(struct st*);
void postorder(struct st*);
int main()
{
int ch;
while(1)
{
cout<<"\n1.Insert \n2.Preorder \n3.Inorder \n4.Postorder \n5.Exit";
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
insertElements();
break;
case 2:
preorder(root);
break;
case 3:
inorder(root);
break;
case 4:
postorder(root);
break;
case 5:
exit(1);
break;
default:
cout<<"Invalid Operation";
}
}
}
void insertElements()
{
struct st*nc, *pnode;
int v;
cout<<"\nEnter value:";
cin>>v;
temp = new st;
temp->data = v;
temp->left = NULL;
temp->right = NULL;
if(root==NULL)
{
root = temp;
}
else
{
nc= root;
while(nc!=NULL)
{
pnode=nc;
if(v<nc->data)
nc = nc->left;
else
nc = nc->right;
}
if(v < pnode->data)
{
pnode->left = temp;
}
else
pnode->right = temp;
}
}
void preorder(struct st*temp)
{
if(temp!=NULL)
{
cout<<" "<<temp->data;
preorder(temp->left);
preorder(temp->right);
}
}
void inorder(struct st*temp)
{
if(temp!=NULL)
{
inorder(temp->left);
cout<<" "<<temp->data;
inorder(temp->right);
}
}
void postorder(struct st*temp)
{
if(temp!=NULL)
{
postorder(temp->left);
postorder(temp->right);
cout<<" "<<temp->data;
}
}

OUTPUT:
PROGRAM 6: AVL TREE OPERATIONS
CODING:
#include<iostream.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define pow2(n) (1 << (n))
struct avl
{
int d;
struct avl *l;
struct avl *r;
} *r;
class avl_tree {
public:
int height (avl *);
int difference (avl *);
avl *rr_rotat (avl *);
avl *ll_rotat (avl *);
avl *lr_rotat (avl*);
avl *rl_rotat (avl *);
avl * balance (avl *);
avl * insert (avl*, int);
void show (avl*, int);
void inorder (avl *);
void preorder (avl *);
void postorder(avl*);
avl_tree ()
{
r = NULL;
}
};
int avl_tree::height (avl *t)
{
int h = 0;
if(t!=NULL)
{
int l_height = height(t->l);
int r_height = height(t->r);
int b_factor=l_height*r_height;
return b_factor;
}
return h;
}
int avl_tree::difference (avl *t)
{
int l_height = height(t->l);
int r_height = height(t->r);
int b_factor = l_height - r_height;
return b_factor;
}
avl *avl_tree::rr_rotat (avl *parent)
{
avl *t;
t = parent->r;
parent->r = t->l;
t->l = parent;
cout<<"Right-Right Rotation";
return t;
}
avl *avl_tree::ll_rotat (avl *parent)
{
avl *t;
t = parent->l;
parent->l = t->r;
t->r = parent;
cout<<"Left-Left Rotation";
return t;
}
avl *avl_tree::lr_rotat(avl *parent)
{
avl *t;
t = parent->l;
parent->l = rr_rotat(t);
cout<<"Left-Right Rotation";
return ll_rotat(parent);
}
avl *avl_tree::rl_rotat(avl *parent)
{
avl *t;
t = parent->r;
parent->r = ll_rotat(t);
cout<<"Right-Left Rotation";
return rr_rotat(parent);
}
avl *avl_tree::balance(avl *t)
{
int bal_factor = difference(t);
if (bal_factor > 1)
{
if (difference(t->l) > 0)
t = ll_rotat(t);
else
t = lr_rotat(t);
}
else if (bal_factor < -1)
{
if (difference(t->r) > 0)
t = rl_rotat(t);
else
t = rr_rotat(t);
}
return t;
}
avl *avl_tree::insert (avl *r, int v)
{
if (r == NULL)
{
r = new avl;
r->d = v;
r->l = NULL;
r->r = NULL;
return r;
}
else if (v< r->d)
{
r->l = insert(r->l, v);
r = balance(r);
}
else if (v >= r->d)
{
r->r = insert(r->r, v);
r = balance(r);
}
return r;
}
void avl_tree::show(avl *p, int l)
{
int i;
if (p!= NULL)
{
show(p->r, l+ 1);
cout<<" ";
if (p == r)
cout << "Root -> ";
for (i = 0; i < l&& p!= r; i++)
cout << " ";
cout << p->d;
show(p->l, l + 1);
}
}
void avl_tree::inorder(avl *t)
{
if (t == NULL)
return;
inorder(t->l);
cout << t->d << " ";
inorder(t->r);
}
void avl_tree::preorder(avl *t)
{
if (t == NULL)
return;
cout << t->d << " ";
preorder(t->l);
preorder(t->r);
}
void avl_tree::postorder (avl *t)
{
if (t == NULL)
return;
postorder (t ->l);
postorder (t ->r);
cout << t->d << " ";
}
int main ()
{
int c, i;
avl_tree avl;
while (1)
{
cout << "1. Insert Element into the tree" << endl;
cout << "2. show Balanced AVL Tree" << endl;
cout << "3. InOrder traversal" << endl;
cout << "4. PreOrder traversal" << endl;
cout << "5. PostOrder traversal" << endl;
cout << "6. Exit" << endl;
cout << "Enter your Choice: ";
cin >> c;
switch (c)
{
case 1:
cout << "Enter value to be inserted: ";
cin >> i;
r = avl.insert(r, i);
break;
case 2:
if (r == NULL)
{
cout << "Tree is Empty" << endl;
continue;
}
cout << "Balanced AVL Tree:" << endl;
avl. show (r, 1);
cout<<endl;
break;
case 3:
cout << "Inorder Traversal:" << endl;
avl. inorder(r);
cout << endl;
break;
case 4:
cout << "Preorder Traversal:" << endl;
avl. preorder (r);
cout << endl;
break;
case 5:
cout << "Postorder Traversal:" << endl;
avl. postorder(r);
cout << endl;
break;
case 6:
exit (1);
break;
default:
cout << "Wrong Choice" << endl;
}
}
return 0;
}

OUTPUT
PROGRAM 8: HASH TABLE USING LINKED LIST
CODING:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
const int TABLE_SIZE = 5;
class HashNode
{
public:
int key;
int value;
HashNode(int key, int value)
{
this->key = key;
this->value = value;
}
};
class DeletedNode:public HashNode
{
private:
static DeletedNode *entry;
DeletedNode():HashNode(-1, -1)
{}
public:
static DeletedNode *getNode()
{
if (entry == NULL)
entry = new DeletedNode();
return entry;
}
};
DeletedNode *DeletedNode::entry = NULL;
class HashMap
{
private:
HashNode **htable;
public:
HashMap()
{
htable = new HashNode* [TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
{
htable[i] = NULL;
}
}

~HashMap()
{
for (int i = 0; i < TABLE_SIZE; i++)
{
if (htable[i] != NULL && htable[i] != DeletedNode::getNode())
delete htable[i];
}
delete[] htable;
}
int HashFunc(int key)
{
return key % TABLE_SIZE;
}
void Insert(int key, int value)
{
int hash_val = HashFunc(key);
int init = -1;
int deletedindex = -1;
while (hash_val != init && (htable[hash_val]
== DeletedNode::getNode() || htable[hash_val]
!= NULL && htable[hash_val]->key != key))
{
if (init == -1)
init = hash_val;
if (htable[hash_val] == DeletedNode::getNode())
deletedindex = hash_val;
hash_val = HashFunc(hash_val + 1);
}
if (htable[hash_val] == NULL || hash_val == init)
{
if(deletedindex != -1)
htable[deletedindex] = new HashNode(key, value);
else
htable[hash_val] = new HashNode(key, value);
}
if(init != hash_val)
{
if (htable[hash_val] != DeletedNode::getNode())
{
if (htable[hash_val] != NULL)
{
if (htable[hash_val]->key == key)
htable[hash_val]->value = value;
}
}
else
htable[hash_val] = new HashNode(key, value);
}
}
int Search(int key)
{
int hash_val = HashFunc(key);
int init = -1;
while (hash_val != init && (htable[hash_val]
== DeletedNode::getNode() || htable[hash_val]
!= NULL && htable[hash_val]->key != key))
{
if (init == -1)
init = hash_val;
hash_val = HashFunc(hash_val + 1);
}
if (htable[hash_val] == NULL || hash_val == init)
return -1;
else
return htable[hash_val]->value;
}
void Remove(int key)
{
int hash_val = HashFunc(key);
int init = -1;
while (hash_val != init && (htable[hash_val]
== DeletedNode::getNode() || htable[hash_val]
!= NULL && htable[hash_val]->key != key))
{
if (init == -1)
init = hash_val;
hash_val = HashFunc(hash_val + 1);
}
if (hash_val != init && htable[hash_val] != NULL)
{
delete htable[hash_val];
htable[hash_val] = DeletedNode::getNode();
}
}
};
int main()
{
HashMap hash;
int key, value;
int choice;
while(1)
{
cout<<"\n----------------------"<<endl;
cout<<"Operations on Hash Table"<<endl;
cout<<"\n----------------------"<<endl;
cout<<"1.Insert element into the table"<<endl;
cout<<"2.Search element from the key"<<endl;
cout<<"3.Delete element at a key"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter element to be inserted: ";
cin>>value;
cout<<"Enter key at which element to be inserted: ";
cin>>key;
hash.Insert(key, value);
break;
case 2:
cout<<"Enter key of the element to be searched: ";
cin>>key;
if(hash.Search(key) == -1)
{
cout<<"No element found at key "<<key<<endl;
continue;
}
else
{
cout<<"Element at key "<<key<<" : ";
cout<<hash.Search(key)<<endl;
}
break;
case 3:
cout<<"Enter key of the element to be deleted: ";
cin>>key;
hash.Remove(key);
break;
case 4:
exit(1);
default:
cout<<"\nEnter correct option\n";
}
}
return 0;
}
OUTPUT
PROGRAM 9: HEAP OPERATIONS
CODING:
#include<iostream.h>
#include<conio.h>
void min_heap(int *a, int m, int n){
int j, t;
t= a[m];
j = 2 * m;
while (j <= n) {
if (j < n && a[j+1] < a[j])
j = j + 1;
if (t < a[j])
break;
else if (t >= a[j]) {
a[j/2] = a[j];
j = 2 * j;
}
}
a[j/2] = t;
return;
}
void build_minheap(int *a, int n) {
int k;
for(k = n/2; k >= 1; k--) {
min_heap(a,k,n);
}
}
int main() {
int n, i;
cout<<"enter no of elements of array\n";
cin>>n;
int a[30];
for (i = 1; i <= n; i++) {
cout<<"enter element"<<" "<<(i)<<endl;
cin>>a[i];
}
build_minheap(a, n);
cout<<"Min Heap\n";
for (i = 1; i <= n; i++) {
cout<<a[i]<<endl;
}
getch();
return 0;
}
OUTPUT
PROGRAM 10: PRIORITY QUEUES
CODING:
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>

struct node
{
int priority;
int info;
struct node *link;
};

class Priority_Queue
{
private:
node *front;
public:
Priority_Queue()
{
front = NULL;
}

void insert(int item, int priority)


{
node *tmp, *q;
tmp = new node;
tmp->info = item;
tmp->priority = priority;
if (front == NULL || priority < front->priority)
{
tmp->link = front;
front = tmp;
}
else
{
q = front;
while (q->link != NULL && q->link->priority <= priority)
q=q->link;
tmp->link = q->link;
q->link = tmp;
}
}

void del()
{
node *tmp;
if(front == NULL)
cout<<"Queue Underflow\n";
else
{
tmp = front;
cout<<"Deleted item is: "<<tmp->info<<endl;
front = front->link;
free(tmp);
}
}
void display()
{
node *ptr;
ptr = front;
if (front == NULL)
cout<<"Queue is empty\n";
else
{ cout<<"Queue is :\n";
cout<<"Priority Item\n";
while(ptr != NULL)
{
cout<<ptr->priority<<" "<<ptr->info<<endl;
ptr = ptr->link;
}
}
}
};
int main()
{ clrscr();
int choice, item, priority;
Priority_Queue pq;
do
{
cout<<"1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.Display\n";
cout<<"4.Quit\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Input the item value to be added in the queue : ";
cin>>item;
cout<<"Enter its priority : ";
cin>>priority;
pq.insert(item, priority);
break;
case 2:
pq.del();
break;
case 3:
pq.display();
break;
case 4:
break;
default :
cout<<"Wrong choice\n";
}
}
while(choice != 4);
getch();
return 0;
}
OUTPUT
PROGRAM 11: DYNAMIC EQUIVALENCE
CODING
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
class Disjset
{
int *rank, *parent, n;
public:
Disjset (int n)
{
rank=new int[n];
parent=new int[n];
this->n=n;
MakeSet ();
}
void MakeSet ()
{
for (int i=0; i<n; i++)
{
parent[i]=i;
}
}
int find (int x)
{
if(parent[x]!=x)
{
parent[x]=find(parent[x]);
}
return parent[x];
}
void union1(int x, int y)
{
int xset=find(x);
int yset=find(y);
if(xset==yset)
return;
if(rank[xset]<rank[yset])
{
parent[xset]=yset;
}
else if(rank[xset]>rank[yset])
{
parent[yset]=xset;
}
else
{
parent[yset]=xset;
rank[xset]=rank[xset]+1;
}
}
};
int main ()
{
clrscr ();
Disjset obj (5);
obj. union1(0,2);
obj. union1(4,2);
obj. union1(3,1);
if (obj. find (4) ==obj. find (0))
cout<<"yes \n";
else
cout<<"no \n";
if (obj. find (1) ==obj. find (0))
cout<<"yes \n";
else
cout<<"no \n";
getch ();
return 0;
}
OUTPUT
PROGRAM 12: RECURSIVE DECOMPOSITION
CODING:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
int main(int argc, char **argv)
{
clrscr();
void lu(float[][10], float[][10], float[][10], int n);
void output(float[][10], int);
float a[10][10], l[10][10], u[10][10];
int n = 0, i = 0, j = 0;
cout << "Enter size of 2d array(Square matrix) : ";
cin >> n;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << "Enter values no:" << i << ", " << j << ": ";
cin >> a[i][j];
}
}
lu(a, l, u, n);
cout << "\nL Decomposition\n\n";
output(l, n);
cout << "\nU Decomposition\n\n";
output(u, n);
getch();
return 0;
}
void lu(float a[][10], float l[][10], float u[][10], int n)
{
int i = 0, j = 0, k = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (j < i)
l[j][i] = 0;
else
{
l[j][i] = a[j][i];
for (k = 0; k < i; k++)
{
l[j][i] = l[j][i] - l[j][k] * u[k][i];
}
}
}
for (j = 0; j < n; j++)
{
if (j < i)
u[i][j] = 0;
else if (j == i)
u[i][j] = 1;
else
{
u[i][j] = a[i][j] / l[i][i];
for (k = 0; k < i; k++)
{
u[i][j] = u[i][j] - ((l[i][k] * u[k][j]) / l[i][i]);
}
}
}
}
}
void output (float x [] [10], int n)
{
int i = 0, j = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%f ", x[i][j]);
}
cout << "\n";
}
}
OUTPUT

You might also like