Jayawant Institute of Management & Studies Laboratory Workbo o K
Jayawant Institute of Management & Studies Laboratory Workbo o K
Laboratory Workbook
Subject: Data Structure Programming Lab
Prepared by,
Jancy Jose
Roll No: 61291425
SPPU Exam Seat No: 26954
Program Index
Sr. No.
Program
3
Reverse a string using stack.
12 Implementation of Hashing.
<script>
class Node {
constructor(data,next=null)
this.data=data;
this.next=next;
class Linkedlist
constructor()
this.head=null;
this.size=0;
insertFirst(data)
this.head=new Node(data,this.head)
this.size++;
insertLast(data)
this.head=node;
else
let temp=this.head;
while(temp.next)
temp=temp.next;
temp.next=node;
this.size++;
insertAtIndex(data,index)
return;
if(index===0)
{
this.head=new Node(data,this.head)
return;
let temp1,temp2;
temp1=this.head;
let count=0;
while(count<index)
temp2=temp1;
count++;
temp1=temp1.next;
node.next=temp1;
temp2.next=node;
this.size++;
getIndex(index)
var b=false;
let temp=this.head;
let count=0;
while(temp)
if(count==index)
document.write("</br>");
b=true;
count++;
temp=temp.next;
if(b==false)
document.write("null");
removeat(index)
let temp1=this.head;
let temp2;
let count=0;
return;
}
if(index===0)
this.head=temp1.next;
else
while(count<index)
count++;
temp2=temp1;
temp1=temp1.next;
temp2.next=temp1.next;
document.write("</br>");
this.size--;
clearlist()
{
this.head=null;
this.size=0;
printdatalist()
let temp=this.head;
while(temp)
document.write(temp.data);
document.write("</br>");
temp=temp.next
document.write("</br>")
ll.insertFirst(100)
ll.insertAtIndex(200,1)
ll.insertAtIndex(300,2)
ll.insertAtIndex(400,3)
ll.insertLast(500)
ll.printdatalist()
ll.removeat(0)
ll.printdatalist()
ll.getIndex(3)
document.write("Size:"+ll.size);
document.write("</br>")
document.write("</br>")
function doubleLinkedList() {
this.element = element;
this.next = null;
this.prev = null;
let length = 0;
this.append = function(element) {
current = head,
previous;
if(!head){
head = node;
tail = node;
}else{
node.prev = tail;
tail.next = node;
tail = node;
length++;
current = head,
previous,
index = 0;
if(!head){
head = node;
tail = node;
}else{
node.next = current;
current.prev = node;
head = node;
}
current = tail;
current.next = node;
node.prev = current;
tail = node;
}else{
previous = current;
current = current.next;
node.next = current;
previous.next = node;
current.prev = node;
node.prev = previous;
length++;
return true;
}else{
return false;
}
this.removeAt = function(position){
head = current.next;
tail = null;
}else{
head.prev = null;
current = tail;
tail = current.prev;
tail.next = null;
}else{
previous = current;
current = current.next;
previous.next = current.next;
current.next.prev = previous;
length--;
return current.element;
}else{
return null;
this.indexOf = function(elm){
index = -1;
while(current){
return ++index;
index++;
current = current.next;
return -1;
};
};
return this.removeAt(this.indexOf(elm));
};
this.deleteHead = function(){
this.removeAt(0);
this.deleteTail = function(){
this.removeAt(length-1);
this.toString = function(){
string = '';
while(current){
current = current.next;
return string;
};
this.disp = function(){
current = head;
while(current){
arr.push(current.element);
current = current.next;
}return arr;
};
this.isEmpty = function(){
};
this.size = function(){
return length;
this.getHead = function() {
return head;
this.getTail = function() {
return tail;
}}
dll.append('Megha');
dll.append('Prasanna');
dll.append('Ashlesha');
document.write(dll.disp());
document.write("</br>");
dll.insert(1, 'Shashank');
document.write(dll.disp());
document.write("</br>");
dll.deleteHead();
document.write(dll.disp());
document.write("</br>");
dll.deleteTail();
document.write(dll.disp());
document.write("</br>");
</script>
Output:
Program 2:- STACK implementation using Array with PUSH, POP operations
Source code
<script>
class Stack {
constructor() {
this.items = [];
push(element) {
this.items.push(element);
pop() {
return this.items.pop();
peek() {
isEmpty() {
size() {
return this.items.length;
clear() {
this.items = [];
toArray() {
return this.items;
disp(){
for(let i=this.items.length-1;i>=0;i--)
document.write(this.items[i]);
document.write("</br>");
toString() {
return this.items.toString();
document.write("<br>");
stack.push(12);
stack.push(13);
stack.disp();
stack.push(14);
stack.disp();
document.write("<br>");
document.write("<br>");
stack.push(15);
document.write("<br>");
stack.disp();
stack.pop();
stack.disp();
stack.pop();
stack.disp();
</script>
Output:
<script>
class Stack
constructor(){
this.data = [];
this. length = 0;
push(item){
this.length++;
return this.data.push(item);
pop (){
if (length <= 0) {
return null;
} else {
length--;
return data.pop();
peek() {
if (length <= 0) {
return null;
} else {
return data[length - 1];
disp()
for(let i=this.data.length-1;i>=0;i--)
document.write(this.data[i]);
isEmpty(){
return !length;
function reverseString(str) {
strArr.forEach((element) => {
stack.push(element);
});
stack.disp();
while (!stack.isEmpty()) {
result += stack.pop();
return result;
document.write("Original String:"+str+"</br>");
document.write(reverseString(str));
</script>
Output:
Original String:Megha
Reverse String: ahgeM
Program 4:- Check for balanced parentheses by using Stacks
Source code
<script>
isMatchingBrackets = function(str) {
let map = {
'(': ')',
'[': ']',
'{': '}'
stack.push(str[i]);
else {
return false
};
if (stack.length !== 0) {
return false
};
return true;
document.write(isMatchingBrackets("(){}"));
document.write("</br>");
document.write(isMatchingBrackets("({(()))}}"));
document.write("</br>");
</script>
Output:
true
false
Program 5:- Implement Stack using Linked List
Source code
<script>
function stackUsingLL(){
this.element = elm;
this.next = null;
let length = 0;
this.push = function(elm){
current;
current = head;
node.next = current;
head = node;
length++;
this.pop = function(){
let current = head;
if(current){
current = current.next;
head = current;
length--;
return elm;
return null;
this.peek = function(){
if(head){
return head.element;
return null;
this.toArray = function(){
while(current){
arr.push(current.element);
current = current.next;
return arr;
this.isEmpty = function(){
this.size = function(){
return length;
this.clear = function(){
head = null;
length = 0;
stack.push(1);
stack.push(2);
stack.push(3);
document.write(stack.peek());
document.write("<br>");
document.write(stack.isEmpty());
document.write("<br>");
document.write(stack.size());
document.write("<br>");
document.write(stack.pop());
document.write("<br>");
document.write(stack.toArray());
document.write("<br>");
document.write(stack.size());
document.write("<br>");
stack.clear();
document.write(stack.isEmpty());
document.write("<br>");
</script>
Output:
3
false
3
3
2,1
2
true
Program 6:- Demonstration of Linear Queue.
Source code
<script>
class Queue {
constructor() {
this.items = [];
enqueue(element) {
return this.items.push(element);
dequeue() {
if(this.items.length > 0) {
return this.items.shift();
peek() {
isEmpty(){
return this.items.length == 0;
size(){
return this.items.length;
clear(){
this.items = [];
return str
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(4);
queue.enqueue(8);
document.write(queue.items);
document.write("</br>");
queue.dequeue();
document.write(queue.items);
document.write("</br>");
document.write(queue.peek());
document.write("</br>");
document.write(queue.isEmpty());
document.write("</br>");
document.write(queue.size());
document.write("</br>");
var c=queue.clear();
document.write(c);
document.write("</br>");
</script>
Output:
1,2,4,8
2,4,8
8
false
3
Queue is Cleared
Program 7:- Demonstration of Circular Queue.
Source code
<script>
class CircularQueue {
constructor(size) {
this.element = [];
this.size = size
this.length = 0
this.front = 0
this.back = -1
print() {
document.write(this.element);
isEmpty() {
return (this.length == 0)
enqueue(element) {
this.back++
this.length++
dequeue() {
if (this.isEmpty()) throw (new Error("No elements in the queue"))
this.front++
this.length--
document.write(value);
getFront() {
clear() {
this.length = 0
this.back = 0
this.front = -1
cq.enqueue(100);
cq.enqueue(5);
cq.enqueue(20);
cq.enqueue(35);
cq.enqueue(45);
cq.print();
document.write("</br>");
cq.dequeue();
document.write("</br>");
cq.print();
</script>
Output:
Program 8:- Demonstration of Priority Queue
Source code
<script>
class QElement {
constructor(element, priority)
this.element = element;
this.priority = priority;
class PriorityQueue {
constructor()
this.items = [];
enqueue(element, priority)
this.items.splice(i, 0, qElement);
contain = true;
break;
}
if (!contain) {
this.items.push(qElement);
dequeue()
if (this.isEmpty())
return "Underflow";
return this.items.shift();
front()
if (this.isEmpty())
return this.items[0];
rear()
if (this.isEmpty())
isEmpty()
return this.items.length == 0;
printPQueue()
return str;
document.write(priorityQueue.isEmpty());
document.write("</br>");
document.write(priorityQueue.front());
document.write("</br>");
priorityQueue.enqueue("Megha", 2);
priorityQueue.enqueue("Ketaki", 1);
priorityQueue.enqueue("Rasika", 1);
priorityQueue.enqueue("Shahida", 2);
priorityQueue.enqueue("Shivani", 3);
document.write(priorityQueue.printPQueue());
document.write("</br>");
document.write(priorityQueue.front().element);
document.write("</br>");
document.write(priorityQueue.rear().element);
document.write("</br>");
document.write(priorityQueue.dequeue().element);
document.write("</br>");
priorityQueue.enqueue("Shashank", 2);
document.write(priorityQueue.printPQueue());
document.write("</br>");
</script>
Output:
true
No elements in Queue
Ketaki Rasika Megha Shahida Shivani
Ketaki
Shivani
Ketaki
Rasika Megha Shahida Shashank Shivani
Program 9:- Reverse stack using queue
Source code
<script>
class Stack {
constructor(){
this.elements = [];
push(element){
this.elements.push(element);
pop(){
isEmpty(){
return this.elements.length == 0;
print(){
return this.elements;
}
}
class Queue {
constructor(){
this.elements = [];
enqueue(element){
this.elements.push(element)
dequeue() {
if(!this.isEmpty()) {
return this.elements.shift();
} else {
isEmpty() {
return this.elements.length == 0;
function reverse(stack){
const queue = new Queue();
while(!stack.isEmpty()){
queue.enqueue(stack.pop());
while(!queue.isEmpty()){
stack.push(queue.dequeue());
stack.push('Megha');
stack.push('is');
stack.push('Student');
document.write("</br>");
reverse(stack);
document.write("</br>");
</script>
Output:
Source code
<script>
class Node
constructor(data)
this.data = data;
this.left = null;
this.right = null;
class BinarySearchTree
constructor()
this.root = null;
insert(data)
this.root = newNode;
else
this.insertNode(this.root, newNode);
insertNode(node, newNode)
node.left = newNode;
else
this.insertNode(node.left, newNode);
else
node.right = newNode;
else
this.insertNode(node.right,newNode);
remove(data)
removeNode(node, key)
return null;
return node;
return node;
else
node = null;
return node;
}
if(node.left === null)
node = node.right;
return node;
node = node.left;
return node;
node.data = aux.data;
return node;
inorder(node)
this.inorder(node.left);
document.write(node.data);
document.write(" ");
this.inorder(node.right);
preorder(node)
document.write(node.data);
document.write(" ");
this.preorder(node.left);
this.preorder(node.right);
postorder(node)
this.postorder(node.left);
this.postorder(node.right);
document.write(node.data);
document.write(" ");
}
}
findMinNode(node)
return node;
else
return this.findMinNode(node.left);
getRootNode()
return this.root;
search(node, data)
return null;
else
return node;
}
var BST = new BinarySearchTree();
BST.insert(15);
BST.insert(25);
BST.insert(10);
BST.insert(7);
BST.insert(22);
BST.insert(17);
BST.insert(13);
BST.insert(5);
BST.insert(9);
BST.insert(27);
BST.inorder(root);
BST.remove(5);
document.write("</br>");
BST.inorder(root);
BST.remove(7);
var root = BST.getRootNode();
document.write("</br>");
BST.inorder(root);
BST.remove(15);
document.write("</br>");
document.write("inorder traversal");
document.write("</br>");
BST.inorder(root);
document.write("</br>");
document.write("postorder traversal");
document.write("</br>");
BST.postorder(root);
document.write("</br>");
document.write("preorder traversal");
document.write("</br>");
BST.preorder(root);
document.write("</br>");
</script>
Output:
Program 11:- Graph implementation and graph traversals.
Source code
<script>
class Graph {
constructor() {
console.log('Di-graph');
addVertex(vertex) {
if (!this.AdjList.has(vertex)) {
this.AdjList.set(vertex, []);
} else {
addEdge(vertex, node) {
if (this.AdjList.has(vertex)) {
if (this.AdjList.has(node)){
if(!arr.includes(node)){
arr.push(node);
}else{
throw `Can't add '${node}', it already exists`;
}else {
} else {
print() {
document.write(this.AdjList);
document.write("</br>")
document.write(key, value);
document.write("</br>")
createVisitedObject(){
arr[key] = false;
}
return arr;
bfs(startingNode){
document.write('\nBFS')
document.write("</br>")
let q = [];
visited[startingNode] = true;
q.push(startingNode);
while(q.length){
document.write(current);
if(!visited[elem]){
visited[elem] = true;
q.unshift(elem)
}
}
dfs(startingNode){
document.write('\nDFS')
document.write("</br>");
this.dfsHelper(startingNode, visited);
dfsHelper(startingNode, visited){
visited[startingNode] = true;
document.write(startingNode);
if(!visited[elem]){
this.dfsHelper(elem, visited);
}
}
doesPathExist(firstNode, secondNode){
let q = [];
visited[firstNode] = true;
q.push(firstNode);
while(q.length){
path.push(node);
if(elements.includes(secondNode)){
document.write(path.join('->'))
return true;
}else{
if(!visited[elem]){
visited[elem] = true;
q.unshift(elem);
}
}
return false;
document.write("</br>");
}else{
g.addVertex(arr[i]);
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');
g.print();
g.bfs('A');
document.write("</br>");
g.dfs('A');
document.write("</br>");
</script>
Output:
Program 12:- Implementation of Hashing.
Source code
<script>
class HashTable {
constructor() {
this.values = {};
this.length = 0;
this.size = 0;
calculateHash(key) {
add(key, value) {
if(!this.values.hasOwnProperty(hash))
this.values[hash] = {};
if (!this.values[hash].hasOwnProperty(key)) {
this.length++;
this.values[hash][key] = value;
search(key) {
const hash = this.calculateHash(key);
return this.values[hash][key];
} else {
return null;
}}}
ht.add("Canada", "300");
ht.add("Germany", "100");
ht.add("Italy", "50");
document.write(ht.search("Canada"));
</script>
Output:
Program 13:- Implementation of Brute Force technique Linear Search.
Source code
<script>
var b=false;
document.write((i+1)+" position");
b=true;}
if(b==false){
arr=[1,3,5,8,9]
key=3
linearSearch(arr,key)
</script>
Output:
Program 14:- Implementation of Brute Force technique Rain Terraces.
Source code
<script>
function trapWaterUsingBrute(bars) {
len = bars.length;
trappedWater = 0;
Imax = 0; rmax = 0;
bars[i]);
return trappedWater;
document.write(bars);
</script>
Output:
0,1,0,2,1,0,1,3,2,1,2,1
Water trapped:6
Program 15:- Implementation of Brute Force technique Travelling Salesman Problem.
Source code
<script>
function generatePermutations(Arr){
var A = Arr.slice();
function swap(a,b){
A[a] = A[b];
A[b] = tmp;
if (n == 1){
permutations.push(A.slice());
} else {
generate(n-1, A);
swap(n % 2 == 0 ? i : 0 ,n-1);
generate(A.length, A);
document.write(" ")
return permutations;
}function generateCityRoutes(cities){
var pems = generatePermutations(cities.slice(1));
pems[i].unshift(cities[0]);
pems[i].push(cities[0]);
return pems;
document.write("</br>");
document.write(generatePermutations([1,2,3]));
document.write("</br>");
document.write(generateCityRoutes([0,2,3]));
document.write("</br>");
</script>
Output:
<script>
function createAdjMatrix(V, G) {
adjMatrix.push([]);
adjMatrix[G[i][0]][G[i][1]] = G[i][2];
adjMatrix[G[i][1]][G[i][0]] = G[i][2];
return adjMatrix;
function prims(V, G) {
var vertex = 0;
visited.push(vertex);
if (adjMatrix[vertex][r] !== 0) {
edges.push([vertex,r,adjMatrix[vertex][r]]);
minEdge = edges[e];
edges.splice(edges.indexOf(minEdge), 1);
MST.push(minEdge+"</br>")
vertex = minEdge[1];
minEdge = [null,null,Infinity];
document.write(MST);
var a = 0, b = 1, c = 2, d = 3, e = 4, f = 5;
var graph = [
[a,b,2],
[a,c,3],
[b,d,3],
[b,c,5],
[b,e,4],
[c,e,4],
[d,e,2],
[d,f,3],
[e,f,5]
];
prims(6, graph);
</script>
Output:
<script>
const n = points.length;
let totalDist = 0;
dist.push([distance, i, j]);
function find(id) {
graph[id] = find(graph[id]);
return graph[id];
}
for(let [d, u, v] of dist) {
graph[rootV] = rootU
totalDist += d;
document.write(totalDist)
};
kruskal(points)
</script>
Output:
<script>
if (arr[mid]===x)
document.write("</br>");
return true;
if(arr[mid] > x)
else
let x = 5;
if (recursiveFunction(arr, x, 0, arr.length-1))
document.write();
x = 6;
if (recursiveFunction(arr, x, 0, arr.length-1))
document.write();
</script>
Output:
<script>
if (n == 1)
document.write("Move disk 1 from rod " + from_rod + " to rod " + to_rod+"<br/>");
return; }
document.write("Move disk " + n + " from rod " + from_rod +" to rod " + to_rod+"<br/>");
var n = 3;
</script>
Output:
<script>
let X, Y;
if (i == 0 || j == 0)
return count;
count = Math.max(count,
Math.max(lcs(i, j - 1, 0),
lcs(i - 1, j, 0)));
return count;
let n, m;
X = "abcdxyez";
Y = "xyzabcyued";
n = X.length;
m = Y.length;
function ValidateEmail(inputText)
if(mailformat.test(inputText))
return true;
else
return false;
var s="meghaughade1999@gmail.com";
ValidateEmail(s);
</script>
Output:
Program 21:- Practical based on backtracking- N Queen’s problems.
Source code
<script>
var iterations = 0
var print_board = function (columns) {
col++
document.write('</br>')
col = 0
row++
previous--
}
return false
columns = columns || []
columns.push(column)
iterations++
if (!has_conflict(columns) &&
return columns
columns.pop(column)
return null
print_board(place_next_queen(28, 28))
</script>
Output:
Q###########################
##Q#########################
####Q#######################
#Q##########################
###Q########################
########Q###################
##########Q#################
############Q###############
##############Q#############
################Q###########
######################Q#####
########################Q###
#####################Q######
###########################Q
#########################Q##
#######################Q####
##########################Q#
######Q#####################
###########Q################
###############Q############
#################Q##########
#######Q####################
#########Q##################
#############Q##############
###################Q########
#####Q######################
####################Q#######
##################Q#########
iterations: 84175966
Prpgram 22:-Write JavaScript code for Maximum Sub Array.
<script>
function maxSubarraySum(A,n) {
max_sum =0
for(i = 0;i<n;i++) {
sum=0
for(j =i ;j<n;j++) {
max_sum = sum }
return max_sum
</script>
OutPut:
Max Sum:19
Program 23:- Write JavaScript code for Recursive Staircase.
<script>
function climbStairs(N) {
if (N<2)
return 1
else
</script>
OutPut:
***