0% found this document useful (0 votes)
13 views36 pages

Dsa Lab Mannual

DSA

Uploaded by

rasalshweta221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views36 pages

Dsa Lab Mannual

DSA

Uploaded by

rasalshweta221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

DSA LAB MANNUAL

Submitted By
Name: Shweta Dattatray Rasal
Roll No.:44

Submitted to

SAVITRIBAI PHULE PUNE UNIVERSITY

MASTER OF COMPUTER APPLICATION

SIDDHANT INSTITUTE OF COMPUTER APPLICATION


TALEGAON-CHAKAN ROAD, TALUKA-MAVAL, SUDUMBRE, PUNE-412109

ACADEMIC YEAR 2023-2024


Acknowledgement

A successful practical work is a result of an organized and well coordinated


teamwork. I, therefore, feel obliged to extend my gratitude towards all those who
made a valuable contribution throughout semester.

I am extremely thankful to Director Prof. Nitin Shrirao for inspiring me to


gather professional knowledge and material without which it would have been
impossible tocomplete this work. I take this opportunity to express my deep sense
of gratitude and thanks to our subject in-charge Prof. Dnyaneshwar Jadhav &
head of department Prof. Reshma Mavkar for their inspiration and guide during
semester.

Student Name & Sign


Certificate

This is to certify that Shweta Dattatray Rasal a bonafide student of Siddhant


Institute of Computer Application, Sudumbare, Pune, Maharashtra has
successfully completed the practical work as prescribed by Savitribai Phule Pune
University inthe partial fulfillment of the requirement of the Master of Computer
Application(MCA) Program, Semester I, A.Y.2023-24.

Prof. Dnyaneshwar Jadhav Prof.Reshma Mavkar Prof. Nitin Shrirao

Subject In-charge Head of Department Director

Internal Examiner: External Examiner:

Date:
Place:
Pune
1. Write a function to get Nth node in a Linked List?

Algorithm:
1. Initialize count = 0
2. Loop through the link list
a. if count is equal to the passed index then return current
node
b. Increment count
c. change current to point to next of the current.

<script>

class Node {
constructor(d) {
this.data = d;
this.next = null;
}
}

var head;
function GetNth(index)
{
var current = head;
var count = 0;
while (current != null) {
if (count == index)
return current.data;
count++;
current = current.next;
}

assert (false);
return 0;
}
function push(new_data) {

var new_Node = new Node(new_data);

new_Node.next = head;

head = new_Node;
}

push(1);
push(4);
push(1);
push(12);
push(1);

document.write("Element at index 3 is "


+ GetNth(3));

</script>

Output
Element at index 3 is 4
2. Write a program to Delete a Linked List node at a given position?

<script>

var head;
class Node
{
constructor(val)
{
this.data = val;
this.next = null;
}
}

function push(new_data)
{

var new_node = new Node(new_data);

new_node.next = head;

head = new_node;
}
function deleteNode(position)
{

if (head == null)
return;
var temp = head;

if (position == 0)
{

head = temp.next;
return;
}
for(i = 0; temp != null && i < position - 1; i++)
temp = temp.next;

if (temp == null || temp.next == null)


return;

var next = temp.next.next;

temp.next = next;
}

function printList()
{
var tnode = head;
while (tnode != null)
{
document.write(tnode.data + " ");
tnode = tnode.next;
}
}

push(7);
push(1);
push(3);
push(2);
push(8);

document.write("Created Linked list is: <br/>");


printList();
deleteNode(4);

document.write("<br/>Linked List after " +


"Deletion at position 4: <br/>");
printList();

</script>
Output:
--Created Linked List--
10-> 20-> 30-> 40-> 50-> 60->NULL

Linked List after deletion at position 0


Element deleted is : 10

Updated Linked List is :


20-> 30-> 40-> 50-> 60->NULL

Linked List after deletion at position 2


Element deleted is : 40

Updated Linked List is :


20-> 30-> 50-> 60->NULL
3. Write a program to Insertion Operation in Doubly Linked List?

<script>
var head;
class Node {

constructor(d) {
this.data = d;
this.next = null;
this.prev = null;
}
}

function push(new_data) {

var new_Node = new Node(new_data);

new_Node.next = head;
new_Node.prev = null;

if (head != null)
head.prev = new_Node;

head = new_Node;
}

function InsertBefore(next_node , new_data) {


if (next_node == null) {
document.write("The given next node can not be NULL");
return;
}
var new_node = new Node(new_data);
new_node.prev = next_node.prev;
next_node.prev = new_node;
new_node.next = next_node;
if (new_node.prev != null)
new_node.prev.next = new_node;
else
head = new_node;
}
function InsertAfter(prev_Node , new_data) {

if (prev_Node == null) {
document.write("The given previous node cannot be NULL ");
return;
}

var new_node = new Node(new_data);

new_node.next = prev_Node.next;

prev_Node.next = new_node;
new_node.prev = prev_Node;

if (new_node.next != null)
new_node.next.prev = new_node;
}
function append(new_data) {

var new_node = new Node(new_data);

var last = head;


new_node.next = null;

if (head == null) {
new_node.prev = null;
head = new_node;
return;
}

while (last.next != null)


last = last.next;
last.next = new_node;

new_node.prev = last;
}

function printlist(node) {
var last = null;
document.write("<br/>Traversal in forward Direction<br/>");
while (node != null) {
document.write(node.data + " ");
last = node;
node = node.next;
}
document.write();
document.write("<br/>Traversal in reverse direction<br/>");
while (last != null) {
document.write(last.data + " ");
last = last.prev;
}
}
append(6);
push(7);
push(1);
append(4);

InsertAfter(head.next, 8);

InsertBefore(head.next.next, 5);

document.write("Created DLL is:<br/> ");


printlist(head);
</script>
Output:
Created DLL is:
Traversal in forward Direction
175864
Traversal in reverse direction
4 68571
4. Write a JavaScript program to delete a node from doubly Linked List?

Algorithm
 Let the node to be deleted be del.
 If node to be deleted is head node, then change the head pointer to
next current head.
if headnode == del then
headnode = del.nextNode
 Set prev of next to del, if next to del exists.
if del.nextNode != none
del.nextNode.previousNode = del.previousNode
 Set next of previous to del, if previous to del exists.
if del.previousNode != none
del.previousNode.nextNode = del.next

<script>
var head;
class Node {
constructor(val) {
this.data = val;
this.prev = null;
this.next = null;
}
}

function push(new_data) {
new_Node = new Node(new_data);
new_Node.next = head;
new_Node.prev = null;

if (head != null)
head.prev = new_Node;
head = new_Node;
}
function printlist( node) {
last = null;

while (node != null) {


document.write(node.data + " ");
last = node;
node = node.next;
}

document.write("<br/>");
}
function deleteNode( del) {

if (head == null || del == null) {


return;
}

if (head == del) {
head = del.next;
}

if (del.next != null) {
del.next.prev = del.prev;
}
if (del.prev != null) {
del.prev.next = del.next;
}

return;
}
push(2);

push(4);

push(8);

push(10);

document.write("Created DLL is: ");


printlist(head);

deleteNode(head);
deleteNode(head.next);
deleteNode(head.next);

document.write("Modified Linked list: ");


printlist(head);

</script>

Output:
Original Linked list 10 8 4 2
Modified Linked list 8
5. Write a program to Stack Implementation Linked List?

<script>

var root;

class StackNode {

constructor(data) {
this.data = data;
this.next = null;
}
}

function isEmpty() {
if (root == null) {
return true;
} else
return false;
}

function push(data) {
var newNode = new StackNode(data);

if (root == null) {
root = newNode;
} else {
var temp = root;
root = newNode;
newNode.next = temp;
}
document.write(data + " pushed to stack<br/>");
}

function pop() {
var popped = Number.MIN_VALUE;
if (root == null) {
document.write("Stack is Empty");
} else {
popped = root.data;
root = root.next;
}
return popped;
}

function peek() {
if (root == null) {
document.write("Stack is empty");
return Number.MIN_VALUE;
} else {
return root.data;
}
}

push(10);
push(20);
push(30);

document.write(pop() + " popped from stack<br/>");

document.write("Top element is " + peek());

</script>
Output:
10 pushed to stack
20 pushed to stack
30 pushed to stack
30 popped from stack
Top element is 20
Elements present in stack : 20 10
6. Write a Program for linked- list implementation of queue?

<script>
class QNode
{
constructor(key)
{
this.key = key;
this.next = null;
}
}

let front = null, rear = null;

function enqueue(key)
{
let temp = new QNode(key);

if (rear == null) {
front = rear = temp;
return;
}

rear.next = temp;
rear = temp;
}

function dequeue()
{

if (front == null)
return;

let temp = front;


front = front.next;

if (front == null)
rear = null;
}

enqueue(10);
enqueue(20);
dequeue();
dequeue();
enqueue(30);
enqueue(40);
enqueue(50);
dequeue();
document.write("Queue Front : " + front.key+"<br>");
document.write("Queue Rear : " + rear.key+"<br>");

</script>

Output:
Queue Front : 40
Queue Rear : 50
7. Write a Program for Priority Queue Implementation of enqueue dequeue
front rear and isEmpty function?

class QElement {
constructor(element, priority)
{
this.element = element;
this.priority = priority;
}
}
class PriorityQueue {
constructor()
{
this.items = [];
}
enqueue(element, priority)
{
var qElement = new QElement(element, priority);
var contain = false;

for (var i = 0; i < this.items.length; i++) {


if (this.items[i].priority > qElement.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 "No elements in Queue";
return this.items[0];
}
rear()
{

if (this.isEmpty())
return "No elements in Queue";
return this.items[this.items.length - 1];
}
isEmpty()
{
return this.items.length == 0;
}
printPQueue()
{
var str = "";
for (var i = 0; i < this.items.length; i++)
str += this.items[i].element + " ";
return str;
}
}
var priorityQueue = new PriorityQueue();
document.write("priorityQueue isEmpty : "+priorityQueue.isEmpty());
document.write("<br>");
document.write("priorityQueue front: "+priorityQueue.front());
document.write("<br>");
priorityQueue.enqueue("Sumit", 2);
priorityQueue.enqueue("Gourav", 1);
priorityQueue.enqueue("Piyush", 1);
priorityQueue.enqueue("Sunny", 2);
priorityQueue.enqueue("Sheru", 3);

document.write("priorityQueue printPQueue:
"+priorityQueue.printPQueue());
document.write("<br>");
document.write("priorityQueue front: "+priorityQueue.front().element);
document.write("<br>");
document.write("priorityQueue rear: "+priorityQueue.rear().element);
document.write("<br>");
document.write(priorityQueue.dequeue().element);
document.write("<br>");
priorityQueue.enqueue("Sunil", 2);
document.write("priorityQueue printPQueue:
"+priorityQueue.printPQueue());
Output:
priorityQueue isEmpty : true
priorityQueue front: No elements in Queue
priorityQueue printPQueue: Gourav Piyush Sumit Sunny Sheru
priorityQueue front: Gourav
priorityQueue rear: Sheru
Gourav
priorityQueue printPQueue: Piyush Sumit Sunny Sunil Sheru
8. Write a JavaScript Program to convert a Binary Tree to Doubly Linked
List?

<script>
class Node {
constructor(data)
{
this.data = data;
this.left = this.right = null;
}
}

var root;

var head;

function BToDLL( root)


{

if (root == null)
return;
BToDLL(root.right);
root.right = head;
if (head != null)
head.left = root;
head = root;
BToDLL(root.left);
}

function printList( head)


{
document.write("Extracted Double Linked List is :<br/> ");
while (head != null) {
document.write(head.data + " ");
head = head.right;
}
}

/* Constructing below tree


5
/ \
3 6
/ \ \
1 4 8
/ \ / \
0 2 7 9 */

root = new Node(5);


root.left = new Node(3);
root.right = new Node(6);
root.left.right = new Node(4);
root.left.left = new Node(1);
root.right.right = new Node(8);
root.left.left.right = new Node(2);
root.left.left.left = new Node(0);
root.right.right.left = new Node(7);
root.right.right.right = new Node(9);

BToDLL(root);
printList(head);
</script>

Output :
Extracted Double Linked list is:
0 1 2 3 4 5 6 7 8 9
9. Write a Program for Complete Binary Tree from its Linked List
Representation?
<script>

class ListNode {
constructor(d) {
this.data = d;
this.next = null;
}
}

class BinaryTreeNode {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}

class BinaryTree {
constructor() {
this.head = null;
this.root = null;
}

push(new_data) {

var new_node = new ListNode(new_data);

new_node.next = this.head;

this.head = new_node;
}

convertList2Binary(node) {

var q = [];

if (this.head == null) {
node = null;
return null;
}

node = new BinaryTreeNode(this.head.data);


q.push(node);
this.head = this.head.next;

while (this.head != null) {

var parent = q.shift();

var leftChild = null,


rightChild = null;

leftChild = new BinaryTreeNode(this.head.data);


q.push(leftChild);
this.head = this.head.next;

if (this.head != null) {
rightChild = new BinaryTreeNode(this.head.data);
q.push(rightChild);
this.head = this.head.next;
}

parent.left = leftChild;
parent.right = rightChild;
}

return node;
}
inorderTraversal(node) {
if (node != null) {
this.inorderTraversal(node.left);
document.write(node.data + " ");
this.inorderTraversal(node.right);
}
}
}
var tree = new BinaryTree();
tree.push(36);
tree.push(30);
tree.push(25);
tree.push(15);
tree.push(12);

tree.push(10);
var node = tree.convertList2Binary(tree.root);

document.write(
"Inorder Traversal of the" + " constructed Binary Tree is:<br>"
);
tree.inorderTraversal(node);

</script>

Output
Inorder Traversal of the constructed Binary Tree is:
25 12 30 10 36 15
10. Write a Program for Tree Traversals Ignored, Preoreder and PostOrder?
<script>
class Node {
constructor(val) {
this.key = val;
this.left = null;
this.right = null;
}
}

var root = null;

function printPostorder(node) {
if (node == null)
return;

printPostorder(node.left);

printPostorder(node.right);

document.write(node.key + " ");


}

function printInorder(node) {
if (node == null)
return;
printInorder(node.left);

document.write(node.key + " ");

printInorder(node.right);
}

function printPreorder(node) {
if (node == null)
return;
document.write(node.key + " ");

printPreorder(node.left);

printPreorder(node.right);

root = new Node(1);


root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);

document.write("Preorder traversal of binary tree is <br/>");


printPreorder(root);

document.write("<br/>Inorder traversal of binary tree is <br/>");


printInorder(root);

document.write("<br/>Postorder traversal of binary tree is <br/>");


printPostorder(root);

</script>

Output:
Preorder traversal of binary tree is
1 2 4 5 3
Inorder traversal of binary tree is
4 2 5 1 3
Postorder traversal of binary tree is
4 5 2 3 1
11. Write a Program for demonstrate delete operation insertion operation
in Binary Search Tree?

<script>
class Node
{
constructor(item)
{
this.key = item;
this.left = this.right = null;
}
}
let root=null;

function deleteKey(key)
{
root = deleteRec(root, key);
}

function deleteRec(root,key)
{

if (root == null)
return root;

if (key < root.key)


root.left = deleteRec(root.left, key);
else if (key > root.key)
root.right = deleteRec(root.right, key);

else {
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;

root.key = minValue(root.right);
root.right = deleteRec(root.right, root.key);
}

return root;
}

function minValue(root)
{
let minv = root.key;
while (root.left != null)
{
minv = root.left.key;
root = root.left;
}
return minv;
}

function insert(key)
{
root = insertRec(root, key);
}
function insertRec(root,key)
{
if (root == null) {
root = new Node(key);
return root;
}

if (key < root.key)


root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);

return root;
}

function inorder()
{
inorderRec(root);
}

function inorderRec(root)
{
if (root != null) {
inorderRec(root.left);
document.write(root.key + " ");
inorderRec(root.right);
}
}

/* Let us create following BST


50
/ \
30 70
/ \ /\
20 40 60 80 */
insert(50);
insert(30);
insert(20);
insert(40);
insert(70);
insert(60);
insert(80);

document.write(
"Inorder traversal of the given tree<br>");
inorder();

document.write("<br>Delete 20<br>");
deleteKey(20);
document.write(
"Inorder traversal of the modified tree<br>");
inorder();

document.write("<br>Delete 30<br>");
deleteKey(30);
document.write(
"Inorder traversal of the modified tree<br>");
inorder();

document.write("<br>Delete 50<br>");
deleteKey(50);
document.write(
"Inorder traversal of the modified tree<br>");
inorder();

</script>

Output:
Inorder traversal of the given tree
20 30 40 50 60 70 80
Delete 20
Inorder traversal of the modified tree
30 40 50 60 70 80
Delete 30
Inorder traversal of the modified tree
40 50 60 70 80
Delete 50
Inorder traversal of the modified tree
40 60 70 80
12. Write a program to detect cycle in an undirected using BFS?
Algorithm:
1. Create a recursive function that takes the edge and color array (this
can be also kept as a global variable)
2. Mark the current node as GREY.
3. Traverse all the adjacent nodes and if any node is marked GREY then
return true as a loop is bound to exist.
4. If any adjacent vertex is WHITE then call the recursive function for
that node. If the function returns true. Return true.
5. If no adjacent node is grey or has not returned true then mark the
current Node as BLACK and return false.

<script>
var WHITE = 0, GRAY = 1, BLACK = 2;
class Graph
{
constructor(ver)
{
this.V = ver;
this.adjList = Array.from(
Array(this.V), () => Array(this.V));
}
}
function addEdge(g, u, v)
{
g.adjList[u].push(v);
}

function DFSUtil(g, u, color)


{

color[u] = GRAY;

for(var iN of g.adjList[u])
{
if (color[iN] == GRAY)
return true;
if (color[iN] == WHITE &&
DFSUtil(g, iN, color) == true)
return true;
}
color[u] = BLACK;
return false;
}
function isCyclic(g)
{
var color = Array(g.V);
for(var i = 0; i < g.V; i++)
{
color[i] = WHITE;
}
for(var i = 0; i < g.V; i++)
{
if (color[i] == WHITE)
{
if (DFSUtil(g, i, color) == true)
return true;
}
}
return false;
}
var g = new Graph(4);
addEdge(g, 0, 1);
addEdge(g, 0, 2);
addEdge(g, 1, 2);
addEdge(g, 2, 0);
addEdge(g, 2, 3);
addEdge(g, 3, 3);

if (isCyclic(g))
document.write("Graph contains cycle");
else
document.write("Graph doesn't contain cycle");

</script>

Output:
Graph contains cycle
13. Write a JavaScript program to print DFS traversal from a given graph?
<script>

class Graph
{

constructor(v)
{
this.V = v;
this.adj = new Array(v);
for(let i = 0; i < v; i++)
this.adj[i] = [];
}
addEdge(v, w)
{

this.adj[v].push(w);
}

DFSUtil(v, visited)
{

visited[v] = true;
document.write(v + " ");

for(let i of this.adj[v].values())
{
let n = i
if (!visited[n])
this.DFSUtil(n, visited);
}
}

DFS(v)
{
let visited = new Array(this.V);
for(let i = 0; i < this.V; i++)
visited[i] = false;
this.DFSUtil(v, visited);
}
}

g = new Graph(4);

g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

document.write("Following is Depth First Traversal " +


"(starting from vertex 2)<br>");

g.DFS(2);

</script>

Output:
Following is Depth First Traversal (starting from vertex 2)
2 0 1 3

You might also like