Lab Report
Lab Report
Algorithm: -
Firstly, we create a stack with c capacity.
Initializing the array to –1.
For push operation we just increase the top by 1 and put the value in top
position like arr[top]=x;
For pop operation we just decrease the top position by 1 and then return
the arr[top].
Actually, stack follows the last in the first out rule so the last element is first
which out.
Code:-
#include <bits/stdc++.h>
using namespace std;
struct MyStack{
int *arr;
int cap;
int top;
MyStack(int c){
cap=c;
arr=new int [cap];
top=-1;
}
int pop(){
if(top==-1){cout<<"Stack is Empty"<<endl;return INT_MIN;}
int res=arr[top];
top--;
return res;
}
int peek(){
if(top==-1){cout<<"Stack is Empty"<<endl;return INT_MIN;}
return arr[top];
}
int size(){
return (top+1);
}
bool isEmpty(){
return top==-1;
}
};
int main()
{
MyStack s(5);
s.push(80);
s.push(90);
s.push(3);
cout<<s.pop()<<endl;
cout<<s.size()<<endl;
cout<<s.peek()<<endl;
cout<<s.isEmpty()<<endl;
return 0;
}
Output:-
Date-06/09/2021
Algorithm:-
As we know queues is follows first in first out or last in last out rule.
A queue maintains two pointers front and rear.
For enqueue operation -
If the queue is full, produce overflow error and exit.
If the queue is not full, increment rear pointer to point the next empty space,
Add data element to the queue location, where the rear is pointing.
For dequeue operation-
If the queue is full, produce overflow error and exit.
if queue is not full ,access the data front is pointing,increament the
front pointer to next available data element.
Code:-
#include <bits/stdc++.h>
using namespace std;
class Queue {
public:
int front, rear, size;
unsigned capacity;
int* array;
};
queue->rear = capacity - 1;
queue->array = new int[(
queue->capacity * sizeof(int))];
return queue;
}
int main()
{
Queue* queue = createQueue(1000);
enqueue(queue, 11);
enqueue(queue, 25);
enqueue(queue, 36);
enqueue(queue, 49);
return 0;
}
Output:-
Date-13/09/2021
Code:-
#include<iostream>
#include<stack>
#include<locale>
using namespace std;
while(stk.top() != '#') {
postfix += stk.top();
stk.pop();
}
return postfix;
}
int main() {
string infix = "3^4/(4*6)+1";
cout << "Postfix Form Is: " << infixtopostfix(infix) << endl;
}
Output:-
Date-13/09/2021
P4:- implementation of Postfix expression evaluation using stack.
Algorithm:-
Read the element from left to right and push into the stack.
Then if the current expression is operator than pop the two operands from
the stack and then evaluate it.
Then push back the result of evaluation.
Repeat it till the end of the expression.
Code:-
#include <iostream>
#include <stack>
using namespace std;
int y = stack.top();
stack.pop();
if (exp[i] == '+') {
stack.push(y + x);
}
else if (exp[i] == '-') {
stack.push(y - x);
}
else if (exp[i] == '*') {
stack.push(y * x);
}
else if (exp[i] == '/') {
stack.push(y / x);
}
}
}
return stack.top();
}
int main()
{
string exp = "56+67*";//1. 46+2/5*7+"=32
//2. "21 + 3*"+9
return 0;
}
Output:-
Date-20/09/2021
Code:-
#include<stdio.h>
#include<stdlib.h>
struct node {
int i;
struct node *link;
};
struct node* head=NULL;
void print(){
struct node* t;
t=head;
printf("\n single linked list");
while (t)
{
printf("%d\n", t->i);
t=t->link;
}
printf("Null");
void add_at_bigging(){
struct node* new=(struct node*)malloc(sizeof(struct node));
new->i=5;
new->link=NULL;
new->link=head;
head=new;
}
void add_at_end(){
struct node* new=(struct node*)malloc(sizeof(struct node));
new->i=6;
new->link=NULL;
struct node* t=head;
while(t->link){
t=t->link;
}
t->link=new;
new->link=NULL;
}
void add_at_intermediant(int pos){
int counter=1;
struct node* new=(struct node*)malloc(sizeof(struct node));
new->i=97;
new->link=NULL;
struct node* t=head;
while ( t->link != NULL && counter!=pos )
{
t=t->link;
counter++;
}
new->link=t->link;
t->link=new;
}
int main(){
struct node* n1=(struct node*)malloc(sizeof(struct node));
struct node* n2=(struct node*)malloc(sizeof(struct node));
struct node* n3=(struct node*)malloc(sizeof(struct node));
struct node* n4=(struct node*)malloc(sizeof(struct node));
n1->i=1;n1->link=n2;
n2->i=2;n2->link=n3;
n3->i=3;n3->link=n4;
n4->i=4;n4->link=NULL;
head=n1;
print();
add_at_bigging();
print();
add_at_end();
print();
add_at_intermediant(2);
print();
add_at_intermediant(5);
print();
return 0;
Output:-
Date-27/09/2021
P6:- To represent the polynomial as a linked list and write a function
to polynomial addition.
Algorithm:-
loop around all values of linked list .
if the value of a node’s exponent. is greater copy this node to result node
and head towards the next node.
if the values of both node’s exponent is same add the coefficients and then
copy the added value with node to the result.
Print the resultant node.
Code:-
#include<bits/stdc++.h>
using namespace std;
struct Node{
int coeff;
int pow;
struct Node *next;
};
void create_node(int x, int y, struct Node **temp){
struct Node *r, *z;
z = *temp;
if(z == NULL){
r =(struct Node*)malloc(sizeof(struct Node));
r->coeff = x;
r->pow = y;
*temp = r;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
} else {
r->coeff = x;
r->pow = y;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
}
void polyadd(struct Node *p1, struct Node *p2, struct Node *result){
while(p1->next && p2->next){
if(p1->pow > p2->pow){
result->pow = p1->pow;
result->coeff = p1->coeff;
p1 = p1->next;
}
else if(p1->pow < p2->pow){
result->pow = p2->pow;
result->coeff = p2->coeff;
p2 = p2->next;
} else {
result->pow = p1->pow;
result->coeff = p1->coeff+p2->coeff;
p1 = p1->next;
p2 = p2->next;
}
result->next = (struct Node *)malloc(sizeof(struct Node));
result = result->next;
result->next = NULL;
}
while(p1->next || p2->next){
if(p1->next){
result->pow = p1->pow;
result->coeff = p1->coeff;
p1 = p1->next;
}
if(p2->next){
result->pow = p2->pow;
result->coeff = p2->coeff;
p2 = p2->next;
}
result->next = (struct Node *)malloc(sizeof(struct Node));
result = result->next;
result->next = NULL;
}
}
void printpoly(struct Node *node){
while(node->next != NULL){
printf("%dx^%d", node->coeff, node->pow);
node = node->next;
if(node->next != NULL)
printf(" + ");
}
}
int main(){
struct Node *p1 = NULL, *p2 = NULL, *result = NULL;
create_node(11,2,&p1);
create_node(29,3,&p1);
create_node(52,0,&p1);
create_node(2,1,&p2);
create_node(5,3,&p2);
printf("polynomial 1: ");
printpoly(p1);
printf("\npolynomial 2: ");
printpoly(p2);
result = (struct Node *)malloc(sizeof(struct Node));
polyadd(p1, p2, result);
printf("\npolynomial after adding p1 and p2 : ");
printpoly(result);
return 0;
}
Output:-
Date-11/10/2021
Code:-
#include"bits/stdc++.h"
using namespace std;
struct node{
int data;
struct node*left;
struct node* right;
node(int val)
{
data=val;
left =NULL;
right =NULL;
}
};
void preorder(node*root){
if(root==NULL){
return;
}
cout<<root->data<<" ";
cout<<"\n";
preorder(root->left);
preorder(root->right);
}
void inorder(node*root){
if(root==NULL){
return;
}
inorder(root->left);
cout<<root->data<<" ";
cout<<"\n";
inorder(root->right);
}
void postorder(node*root){
if(root==NULL){
return;
}
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
cout<<"\n";
}
int main()
{
struct node* root = new node(4);
root->left=new node(2);
root->right=new node(6);
root->left->left=new node(1);
root->left->right=new node(3);
root->right->left=new node(5);
root->right->right=new node(7);
cout<<"preorder traversal"<<"\n";
preorder(root);
cout<<"inorder traversal"<<"\n";
inorder(root);
cout<<"postorder traversal"<<"\n";
postorder(root);
return 0;
}
Output:-
Date-18/10/2021
Algorithm:-
o The last link's next points to the first link of the list in both cases of
singly as well as doubly linked list.
o The first link's previous points to the last of the list in case of doubly
linked list.
Code:-
#include <bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node* next;
Node(int d){
data=d;
next=NULL;
}
};
int main()
{
Node *head=new Node(342);
head->next=new Node(93);
head->next->next=new Node(90);
head->next->next->next=new Node(85);
head->next->next->next->next=head;
printlist(head);
return 0;
}
Output:-
Date-25/10/2021
Algorithm:-
o Insert-
For inserting we call recursive function.
For this return new node if root is null.
Otherwise call recursive function for left and right sub tree.
o Search-
For search operation we call bool recursive function.
If root is null then return false.
If root->key is same as enter key then return true.
Otherwise call recursive function for right and left sub tree.
o Delete-
For delete operation we defined a recursive delete function and a
successor function that used to find closed value.
Similar to insert function but if left leaf node is null then point a
temp node to right and delete root and same goes for right node
is null.
If both node is not empty then find successor then swap with
that and delete the node through recursive delete function
Code:-
#include<iostream>
using namespace std;
struct node{
int key;
node *left, *right;
node(int x){
key=x;
left=right=NULL;
}
};
}
node *getsuccer(node *curr){
curr=curr->right;
while(curr!=NULL && curr->left!=NULL)
curr=curr->left;
return curr;
}
int main(){
node *root=new node(10);
root->left=new node(5);
root->right=new node(15);
root->right->right=new node(18);
root->right->left=new node(12);
cout<<"binary tree is : \n "<<endl;
inorder(root);
insert(root,20);
deletenode(root,15);
cout<<"afer deleting : \n"<< endl;
inorder(root);
return 0;
}
Output:-
Date-01/11/2021
Code:-
#include <iostream>
#include<queue>
using namespace std;
int main(){
priority_queue <int> pq;
pq.push(10);
pq.push(15);
pq.push(5);
cout<<pq.size()<<" ";
cout<<pq.top()<<" ";
while(pq.empty()==false){
cout<<pq.top()<<" ";
pq.pop();
}
return 0;
}
Output:-
Date-08/11/2021
Algorithm:-
o Chaining-
hashIndex = key % noOfBucket
Now, we will use this hash function and calculate the
hashindex of every inserted value to the hashmap.
Insert element and calculate the hashIndex of given key
value and then insert the new node to the end of the list.
To delete a node, we will calculate the hash index and in the
bucket corresponding to hash Index we will search for the
element in the bucket and remove it.
o Linear probing-
Calculate the hash key. key = data % size;
If hashTable[key] is empty, store the value directly.
hashTable[key] = data.
If the hash index already has some value, check for
next index.
key = (key+1) % size;
If the next index is available hashTable[key], store the
value. Otherwise try for next index.
Do the above process till we find the space.
Code:-
o Chaining-
#include<bits/stdc++.h>
using namespace std;
struct myhash{
int bucket;
list<int>*table;
myhash(int b){
bucket=b;
table=new list<int>[b];
}
void insert(int key){
int i=key%bucket;
table[i].push_back(key);
}
void remove(int key){
int i=key%bucket;
table[i].remove(key);
}
bool search(int key){
int i=key%bucket;
for(auto x : table[i])
if(x==key) return 1;
return 0;
}
void displayHash(){
for (int i = 0; i <bucket; i++){
cout << i;
for (auto x:table[i])
cout << " --> " << x;
cout << endl;
}
}
};
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6, };
int n = 5;
myhash h (7);
for (int i = 0; i < n; i++)
h.insert(a[i]);
h.remove (12);
h.search(67);
h.displayHash();
return 0;
}
o Linear probing-
#include<bits/stdc++.h>
using namespace std;
#define TABLE_SIZE 10
class linearprobing {
int* hashTable;
int curr_size;
public:
bool isFull()
{
return (curr_size == TABLE_SIZE);
}
int hash1(int key)
{
return (key % TABLE_SIZE);
}
linearprobing()
{
hashTable = new int[TABLE_SIZE];
curr_size = 0;
for (int i = 0; i < TABLE_SIZE; i++)
hashTable[i] = -1;
}
int i = 1;
while (1) {
int newIndex = (index + i) % TABLE_SIZE;
if (hashTable[newIndex] == -1) {
hashTable[newIndex] = key;
break;
}
i++;
}
}
else
hashTable[index] = key;
curr_size++;
}
void search(int key)
{
int index1 = hash1(key);
int i = 0;
while (hashTable[(index1+ i) % TABLE_SIZE] != key) {
if (hashTable[(index1 + i) % TABLE_SIZE] == -1) {
cout << key << " does not exist" << endl;
return;
}
i++;
}
cout << key << " found" << endl;
}
void displayHash()
{
for (int i = 0; i < TABLE_SIZE; i++) {
if (hashTable[i] != -1)
cout << i << " --> "
<< hashTable[i] << endl;
else
cout << i << endl;
}
}
};
int main()
{
int a[] = { 12, 34, 56, 78, 89, 90 };
int n = sizeof(a) / sizeof(a[0]);
linearprobing h;
for (int i = 0; i < n; i++) {
h.insertHash(a[i]);
}
h.search(37);
h.search(34);
h.displayHash();
return 0;
}
Output:-
o Chaining-
o Linear probing-
Date-15/11/2021
o Quick sort-
Code:-
o Merge sort-
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a[]={10,5,30,15,7};
int l=0,r=4;
mergeSort(a,l,r);
for(int x: a)
cout<<x<<" ";
}
o Quick sort-
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[]={54,45,74,85,11,25,21,12};
int n=sizeof(arr)/sizeof(arr[0]);
qSort(arr,0,n-1);
for(int x: arr)
cout<<x<<" ";
}
Output:-
o Merge sort-
o Quick sort-
Date-15/11/2021
o Binary searching-
#include<iostream>
using namespace std;
int main()
{
int x, n,a[100],key;
cout<<"enter size of array"<<endl;
cin>>n;
cout<<"enter array elements"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"enter key"<<endl;
cin>>key;
cout<<endl;
x=linearsearch(a,n,key);
if(x!=-1)
{
cout<<"key found";
}
else{
cout<<"key doesent found";
}
return 0;
}
Output:-
o Linear searching-
o Binary searching-
Date-22/11/2021
Algorithm:-
Code :-
o DFS-
#include<bits/stdc++.h>
using namespace std;
for(int u:adj[s]){
if(visited[u]==false)
DFSRec(adj,u,visited);
}
}
DFSRec(adj,s,visited);
}
int main()
{
int V=5;
vector<int> adj[V];
addEdge(adj,0,1);
addEdge(adj,0,2);
addEdge(adj,2,3);
addEdge(adj,1,3);
addEdge(adj,1,4);
addEdge(adj,3,4);
return 0;
}
o BFS-
BFS-#include<bits/stdc++.h>
using namespace std;
queue<int> q;
visited[s] = true;
q.push(s);
while(q.empty()==false)
{
int u = q.front();
q.pop();
cout << u << " ";
for(int v:adj[u]){
if(visited[v]==false){
visited[v]=true;
q.push(v);
}
}
}
}
int main()
{
int V=5;
vector<int> adj[V];
addEdge(adj,0,1);
addEdge(adj,0,2);
addEdge(adj,1,2);
addEdge(adj,2,3);
addEdge(adj,1,3);
addEdge(adj,3,4);
addEdge(adj,2,4);
return 0;
}
Output:-
o BFS-
o DFS-
thank
you