Dsa Lab Mannual
Dsa Lab Mannual
Submitted By
Name: Shweta Dattatray Rasal
Roll No.:44
Submitted to
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) {
new_Node.next = head;
head = new_Node;
}
push(1);
push(4);
push(1);
push(12);
push(1);
</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)
{
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;
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);
</script>
Output:
--Created Linked List--
10-> 20-> 30-> 40-> 50-> 60->NULL
<script>
var head;
class Node {
constructor(d) {
this.data = d;
this.next = null;
this.prev = null;
}
}
function push(new_data) {
new_Node.next = head;
new_Node.prev = null;
if (head != null)
head.prev = new_Node;
head = new_Node;
}
if (prev_Node == null) {
document.write("The given previous node cannot be NULL ");
return;
}
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) {
if (head == null) {
new_node.prev = null;
head = new_node;
return;
}
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);
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;
document.write("<br/>");
}
function deleteNode( del) {
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);
deleteNode(head);
deleteNode(head.next);
deleteNode(head.next);
</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);
</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;
}
}
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;
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;
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;
if (root == null)
return;
BToDLL(root.right);
root.right = head;
if (head != null)
head.left = root;
head = root;
BToDLL(root.left);
}
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) {
new_node.next = this.head;
this.head = new_node;
}
convertList2Binary(node) {
var q = [];
if (this.head == null) {
node = null;
return null;
}
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;
}
}
function printPostorder(node) {
if (node == null)
return;
printPostorder(node.left);
printPostorder(node.right);
function printInorder(node) {
if (node == null)
return;
printInorder(node.left);
printInorder(node.right);
}
function printPreorder(node) {
if (node == null)
return;
document.write(node.key + " ");
printPreorder(node.left);
printPreorder(node.right);
</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;
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;
}
return root;
}
function inorder()
{
inorderRec(root);
}
function inorderRec(root)
{
if (root != null) {
inorderRec(root.left);
document.write(root.key + " ");
inorderRec(root.right);
}
}
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);
}
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);
g.DFS(2);
</script>
Output:
Following is Depth First Traversal (starting from vertex 2)
2 0 1 3