Professional Documents
Culture Documents
JSPM’s
MCA DEPARTMENT
Prepared By:
Shantanu D.Mhala
MCA I Sem I
Shantanu D. Mhala
Data Structure And Algoritham
JayawantShikshanPrasarak Mandal
Group of Institute
Institute Name: -JSCOE MCA Dept
Certificate
This is to certify that,
Mr./Mrs.
_ Shantanu Dineshpant Mhala________________________________________
Has successfully completed the term work in
Subject:Data Structure And Algorithm
As per the syllabus of Pune University during the Academic year 20-
2020 -2021 MCA I SEM.I
Date:07/05/2021
INDEX
University Exam No
Shantanu D. Mhala
Data Structure And Algoritham
SUBMISSION REPORT
Practical Subject Teacher
Date Title Remark
No Signature
1 W
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Shantanu D. Mhala
Data Structure And Algoritham
22
23
24
25
26
27
28
29
Shantanu D. Mhala
Data Structure And Algoritham
<html>
<head>
<script type="text/javascript" src="index.js"></script>
</head>
<body></body>
</html>
Save as (index.js)
class Node
{
constructor(data, next = null)
{
this.data = data;
this.next = next;
}
}
class LinkedList
{
constructor()
DSA (Practical Questions and Answers)Data Structure and
Algorithm Page 2
{
this.head = null;
this.size = 0;
}
// insert Node at the First
Shantanu D. Mhala
Data Structure And Algoritham
insertFirst(data)
{
var node = new Node(data);
if(this.head === null){
this.head = node;
}
else{
node.next = this.head;
this.head = node;
}
this.size++;
}
//Display List
printList()
{
var current = this.head;
while(current)
{
console.log(current.data);
current = current.next;
}
}
//Search data
searchData(data)
{
var current = this.head;
var count = 0;
while(current)
{
if(current.data === data)
{
console.log("Data found at "+count+" index, Data: "+data);
Shantanu D. Mhala
Data Structure And Algoritham
}
current = current.next;
count++;
}
}
}
var ll = new LinkedList();
ll.insertFirst(12);
ll.insertFirst(23);Data Structure and Algorithm Page 3
ll.insertFirst(43);
ll.insertFirst(10);
console.log(ll);
console.log("Linked list data:\n");
ll.printList();
ll.searchData(43);
OUTPUT:
Shantanu D. Mhala
Data Structure And Algoritham
Shantanu D. Mhala
Data Structure And Algoritham
insertLast(data)
{
var node = new Node(data);
// List is currently empty
if (this.head === null)
{
this.head = node;
this.tail = node;
}
else{
node.next = null;
node.prev = this.tail;
this.tail.next = node;
this.tail = node;
}
this.size++;
}
// delete node from specific position (index)
deleteNode(index)
{
if (this.head === null)
{
console.log("Doubly Linked List is empty...");
return;
}
this.size--;
let curr = this.head;
if(index <= 0)
{
this.head = this.head.next;
if(this.size !== 0)
{
Shantanu D. Mhala
Data Structure And Algoritham
this.head.prev = null;
}
}else if(index>=this.size-1)
{
this.tail = this.tail.prev;
this.tail.next = null;
}else
{
let count = 0;
while(count<index)
{Data Structure and Algorithm Page
5
curr = curr.next;
count++;
}
curr.next = curr.next.next;
curr.next.prev = curr;
}
}
//Display List
printList() {
var current = this.head;
while(current) {
console.log(current.data);
current = current.next;
}
}
}
var dll = new DoublyLinkedList();
dll.insertLast(12);
dll.insertLast(23);
dll.insertLast(43);
Shantanu D. Mhala
Data Structure And Algoritham
dll.insertLast(10);
console.log(dll);
console.log("Doubly Linked list data:
\n");
dll.printList();
console.log("position 3 deleted");
dll.deleteNode(3);
dll.printList();
console.log("position 0 deleted");
dll.deleteNode(0);
dll.printList();
console.log("position 1 deleted");
dll.deleteNode(1);
dll.printList();
console.log("position 0 deleted");
dll.deleteNode(0);
dll.printList();
OUTPUT:
Shantanu D. Mhala
Data Structure And Algoritham
Shantanu D. Mhala
Data Structure And Algoritham
Shantanu D. Mhala
Data Structure And Algoritham
insertLast(data)
{
var node = new Node(data);
// List is currently empty
if (this.head === null)
{
this.head = node;
this.tail = node;
}
else{
node.next = null;
node.prev = this.tail;
this.tail.next = node;
this.tail = node;
}
this.size++;
}
// Sort the Doubly Linked List
sortList()
{
if (this.head === null)
{
console.log("Doubly Linked List is already empty...");
return;
}
let curr = null,index = null;
let temp;
for(curr = this.head; curr.next !== null; curr = curr.next)
{
for(index = curr.next; index !== null; index = index.next)
{
if(curr.data > index.data)
Shantanu D. Mhala
Data Structure And Algoritham
{
temp = curr.data;
curr.data = index.data;
index.data = temp;
}
}
}
}
//Display List
printList()
{
var current = this.head;
while(current)
{Data Structure and Algorithm Page 8
console.log(current.data);
current = current.next;
}
}
}
var dll = new DoublyLinkedList();
dll.insertLast(12);
dll.insertLast(53);
dll.insertLast(43);
dll.insertLast(10);
console.log(dll);
console.log("Doubly Linked list data:\n");
dll.printList();
dll.sortList();
console.log("Linked List Sorted...");
dll.printList();
OUTPUT:
Shantanu D. Mhala
Data Structure And Algoritham
Shantanu D. Mhala
Data Structure And Algoritham
Shantanu D. Mhala
Data Structure And Algoritham
Shantanu D. Mhala
Data Structure And Algoritham
current = current.next;
}
}
}
var dll = new DoublyLinkedList();
dll.insertFirst(12);
dll.insertFirst(53);
dll.insertFirst(43);
dll.insertFirst(10);
console.log(dll);
console.log("Singly Linked list data:\n");
dll.printList();
dll.countNodes()
Output :
Shantanu D. Mhala
Data Structure And Algoritham
Shantanu D. Mhala
Data Structure And Algoritham
return this.item.pop();
}
peek()
{ // return the top most value from the stack
return this.item[this.item.length - 1];
}
isEmpty()
{
// return true if stack is empty...
return this.item.length == 0;
}
}
var s1 = new Stack();
console.log("isEmpty: "+s1.isEmpty());
s1.push(10);
s1.push(20);
s1.push(30);
console.log("Print stack: "+s1.item);
console.log("pop: "+s1.pop());
console.log("Peek(top) value: "+s1.peek());
console.log("\nPrint stack: "+s1.item);
console.log("isEmpty: "+s1.isEmpty());
console.log("pop: "+s1.pop());
console.log("Print stack: "+s1.item);Data Structure and
Algorithm Page 12
console.log("pop: "+s1.pop());
console.log("Print stack: "+s1.item);
console.log("isEmpty: "+s1.isEmpty());
Shantanu D. Mhala
Data Structure And Algoritham
OUTPUT:
Shantanu D. Mhala
Data Structure And Algoritham
Shantanu D. Mhala
Data Structure And Algoritham
// insert value
enqueue(element)
{
if(this.length>=this.size)
console.log("full");
this.rear++;
this.data[(this.rear)%this.size] =element;// data[0]=10
this.length++;
}
getfront()
{
if(this.length == 0)
{
console.log("no element in circular queue");
}
return this.data[this.front%this.size]//data[0]
}
// Delete value
dequeue()
{
if(this.length == 0)
console.log("no element");
const value = this.getfront();
this.data[this.front%this.size]=null;
this.front++;
this.length--;
console.log("dequeue: "+value);
}
// Display
printQueue()
{
var str = "";
Shantanu D. Mhala
Data Structure And Algoritham
Output :
Shantanu D. Mhala
Data Structure And Algoritham
Shantanu D. Mhala
Data Structure And Algoritham
}
var s1 = new Stack();
OUTPUT:
Shantanu D. Mhala
Data Structure And Algoritham
Shantanu D. Mhala
Data Structure And Algoritham
*/
else {
let last = stack.pop();
/** If the popped element from the stack, which is the last
opening brace doesn’t
match the corresponding closing brace in the map, then return
false
*/
if (str[i] !== map[last]) {return false};
}
}
/**
By the completion of the for loop after checking all the brackets
of the str, at the end, if the
stack is not empty then fail
*/
if (stack.length !== 0) {return false};
return true;
}
console.log(isMatchingBrackets("(){}")); // returns true
console.log(isMatchingBrackets("(){[]()}")); // returns false
Output :
Shantanu D. Mhala
Data Structure And Algoritham
Shantanu D. Mhala
Data Structure And Algoritham
constructor()
{
this.head = null;
this.size = 0;
}
/**
* Push function
* itereate through the list and then add the node to the last
node in the list
*/
pushToStack(item)
{Data Structure and Algorithm Page 18
var node = new StackNode();
node.item = item;
node.next = null;
if(this.size < 1 && this.head === null){
this.head = node;
this.head.next = null;
this.size = 1;
}else{
var current = this.head;
while(current.next !== null)
{
current = current.next;
}
current.next = node;
this.size += 1;
}
}
/**
Shantanu D. Mhala
Data Structure And Algoritham
* Pop Function
* Iterate through the list and grab the last item and remove it
from list
*/
popFromStack()
{
var current = this.head;
if(this.size === 0)
{
return;
}
if(this.size === 1)
{
this.head = null;
this.size = 0;
return current;
}
var prev = current; // 543
while(current.next !== null)
{
prev = current;
current = current.next;
}
prev.next = null;
this.size -= 1;Data Structure and Algorithm Page 19
return current;
}
// Function to get top item of the stack
stackTop()
{
Shantanu D. Mhala
Data Structure And Algoritham
Shantanu D. Mhala
Data Structure And Algoritham
Algorithm Page 20
console.log("Popped item is: " + poped.item);
stack.printStack();
OUTPUT:
Shantanu D. Mhala
Data Structure And Algoritham
Shantanu D. Mhala
Data Structure And Algoritham
{
if(this.items.length == 0)
return "Underflow";
return this.items.shift();
}
// front function
front()
{
if(this.items.length == 0)
return "No elements in Queue";
return this.items[0];
}
// Rear function
Rear()
{
if(this.items.length == 0)
return "No elements in Queue";
return this.items[this.items.length
- 1];
}
// printQueue function
printQueue()
{
var str = "";
Shantanu D. Mhala
Data Structure And Algoritham
}
}
var queue = new Queue();
console.log(queue.dequeue());
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.enqueue(40);
queue.enqueue(50);
queue.enqueue(60);Data Structure and Algorithm Page 22
console.log(queue.printQueue());
console.log("queue.front: "+queue.front());
console.log("queue.dequeue: "+queue.dequeue());
console.log("queue.front: "+queue.front());
console.log("queue.dequeue: "+queue.dequeue())
console.log(queue.printQueue());
Output :
Shantanu D. Mhala
Data Structure And Algoritham
// functions to be implemented
// add vertex to the graph
addVertex(v)
{
// initialize the adjacent list with a
// null array
this.AdjList.set(v, []);
}
// add edge to the graph
addEdge(v, w)
{
Shantanu D. Mhala
Data Structure And Algoritham
Shantanu D. Mhala
Data Structure And Algoritham
}
}
// Main DFS method
dfs(startingNode)
{
var visited = {};
this.DFSUtil(startingNode, visited);
}
// Recursive function which process and explore
// all the adjacent vertex of the vertex with which it is called
DFSUtil(vert, visited)
{
visited[vert] = true;
console.log(vert);
for (var
i in get_neighbours)
{
var get_elem
= get_neighbours[i];
if (!visited[get_elem])
this.DFSUtil(get_elem, visited);
}
}}
// Using the above implemented graph class
var
g
Shantanu D. Mhala
Data Structure And Algoritham
= new Graph(6);
var vertices =[ 'A', 'B', 'C', 'D', 'E', 'F' ];
// adding vertices
for (var i= i = 0; i< vertices.length;i++)
{
g.addVertex(vertices[i]); }
// adding edges
g.addEdge('A', 'B');
g.addEdge('A', 'D');
g.addEdge('A', 'E');
g.addEdge('B', 'C');
g.addEdge('D', 'E');
g.addEdge('E', 'F');
g.addEdge('E', 'C');
g.addEdge('C', 'F');
Shantanu D. Mhala
Data Structure And Algoritham
Output :
Shantanu D. Mhala
Data Structure And Algoritham
// Insert data
setItem(key,value){
let index = this.hash(key);
if(!this.buckets[index]){
this.buckets[index] = [];
}
this.buckets[index].push([key,value])
return index
}
// Search data
getItem(key){
let index = this.hash(key);
if(!this.buckets[index])return null
Shantanu D. Mhala
Data Structure And Algoritham
Output :
Shantanu D. Mhala
Data Structure And Algoritham
Shantanu D. Mhala
Data Structure And Algoritham
}
// Update maximum water value
res += Math.min(left, right) - arr[i];6
}
return res;
}
let arr = [ 0, 1, 0, 2, 1, 0,
1, 3, 2, 1, 2, 1 ];
let n = arr.length;
console.log(maxWater(arr,n));
Output :
Shantanu D. Mhala
Data Structure And Algoritham
var max = 0;
Shantanu D. Mhala
Data Structure And Algoritham
Shantanu D. Mhala
Data Structure And Algoritham
Shantanu D. Mhala
Data Structure And Algoritham
Output :
Shantanu D. Mhala
Data Structure And Algoritham
console.log(" ");
}
}
// Driver program to test printPowerSet
var set = [ 'a', 'b', 'c' ];
printPowerSet(set, 3);
Shantanu D. Mhala
Data Structure And Algoritham
{
this.data
= data;
this.left
= null;
this.right
= null;
}
}
// Binary Search tree class
class BinarySearchTree {
constructor()
{
// root of
a binary seach tree
this.root
Shantanu D. Mhala
Data Structure And Algoritham
= null;
// function to be implemented
// insert(data)
// remove(data)
// Helper function
// findMinNode()
// getRootNode()
// inorder(node)
// preorder(node)
// postorder(node)
// search(node, data)
// helper method which creates
a new node to
// be inserted and calls insertNode
insert(data) {
// Creating
a node and initailising
// with data
var newNode
= new Node(data);
Shantanu D. Mhala
Data Structure And Algoritham
= newNode;
else
// Method to insert
a node in
a tree
// it moves over the tree to find the location
// to insert
a node with
a given data
insertNode(node, newNode) {
// if the data is less than the node
// data move left of the tree
if(newNode.data
< node.data)
{
// if left is null insert node here
if(node.left === null)
node.left
= newNode;
else
Shantanu D. Mhala
Data Structure And Algoritham
this.insertNode(node.left, newNode);
{
// if right is null insert node here
if(node.right === null)
node.right
= newNode;
else
}
}
// helper method that calls the
// removeNode with
a given data
remove(data) {
// root is re
-initialized with
// root of
a modified tree.
this.root
Shantanu D. Mhala
Data Structure And Algoritham
= this.removeNode(this.root, data);
}
{
node.left
= this.removeNode(node.left, key);
return node;
Shantanu D. Mhala
Data Structure And Algoritham
> node.data)
{
node.right
= this.removeNode(node.right, key);
return node;
{
// deleting node with no children
if(node.left === null && node.right === null)
{
node
= null;
return node;
{
node
= node.right;
Shantanu D. Mhala
Data Structure And Algoritham
return node;
{12
node
= node.left;
return node;
node.right
=
this.removeNode(node.right, aux.data);
return node;
}
}
// finds the minimum node in tree
// searching starts from given node
Shantanu D. Mhala
Data Structure And Algoritham
findMinNode(node) {
// if left of
a node is null
// then it must be minimum node
if(node.left === null)
return node;
else
return this.findMinNode(node.left); }
// Performs inorder traversal of
a tree
inorder(node) {
if(node !== null)
{
this.inorder(node.left);
console.log(node.data);
this.inorder(node.right);
}
}
preorder(node) {
if(node !== null)
{
console.log(node.data);
this.preorder(node.left);
this.preorder(node.right);
}
}13
Shantanu D. Mhala
Data Structure And Algoritham
{
this.postorder(node.left);
this.postorder(node.right);
console.log(node.data);
}
}
// returns root of the tree
getRootNode() {
return this.root; }
// search for
a node with given data
search(node, data) {
// if trees is empty return null
if(node === null)
return null;
Shantanu D. Mhala
Data Structure And Algoritham
else if(data
> node.data)
return this.search(node.right, data);
// 15
// / \
// 10 25
// / \ / \
// 7 13 22 27
Shantanu D. Mhala
Data Structure And Algoritham
// / \ /
// 5 9 17
// 15
// / \
// 10 25
// / \ / \
// 7 13 22 27
// \ /
// 9 17
var root = BST.getRootNode();
console.log("Printing Inorder Traversal after removing 5");
// prints 7 9 10 13 15 17 22 25 27
BST.inorder(root);
// 15
// / \
// 10 25
// / \ / \
Shantanu D. Mhala
Data Structure And Algoritham
// 9 13 22 27
Shantanu D. Mhala
Data Structure And Algoritham
}
};
Shantanu D. Mhala
Data Structure And Algoritham
//top row
for(var j=0; j<n; j++){
dp[0][j] = 1;
}
Shantanu D. Mhala
Data Structure And Algoritham
return dp[m-1][n-1];
}
var res=[];
res=uniquePaths(3,3)
console.log(res)
Output :
Shantanu D. Mhala