0% found this document useful (0 votes)
28 views43 pages

Yogesh DSA Practical

1) The document describes creating a singly linked list and performing operations like inserting a node at the front (unshift) and deleting a node from the front (shift). Code is provided to implement these operations. 2) Operations for inserting a node at the end (push) and deleting a node from the end (pop) of a singly linked list are described. Code is provided to implement these operations. 3) Creating a doubly linked list and performing operations like inserting a node from the front (unshift) and deleting a node from the front (shift) are discussed. Code is given to implement these operations.

Uploaded by

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

Yogesh DSA Practical

1) The document describes creating a singly linked list and performing operations like inserting a node at the front (unshift) and deleting a node from the front (shift). Code is provided to implement these operations. 2) Operations for inserting a node at the end (push) and deleting a node from the end (pop) of a singly linked list are described. Code is provided to implement these operations. 3) Creating a doubly linked list and performing operations like inserting a node from the front (unshift) and deleting a node from the front (shift) are discussed. Code is given to implement these operations.

Uploaded by

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

1) Write a program to perform operation on Singly LinkedList.

A)Insert t Node AT Front(unshift)

B)Delete Node From Front(Shift)

Code:
class Node
{
constructor(data)
{ this.data =
data; this.next
= null;
} } class
SinglyLinkedList
{ constructor()
{ this.length = 0;
this.head = null;
this.tail = null;
}
unshift(data)
{
const newNode = new Node(data);
if (!this.length)
{
this.tail = newNode;
this.head = newNode;
}
else
{
newNode.next = this.head;
this.head = newNode;
}
this.length += 1;
return newNode;
}
shift() {
if (!this.length)
{
return undefined

}
if(this.length==1)
{
this.head=null
this.head=null
this.length=0
}
else
{
1
const nodeToRemove = this.head;
this.head = this.head.next;
this.length -= 1;
console.log(nodeToRemove)
}

if (!this.length)
{
this.tail = null;
}

}}
const SLL =new SinglyLinkedList()
SLL.unshift(10)
SLL.unshift(20)
SLL.unshift(30)
console.log(SLL) SLL.shift()
console.log(SLL)
OUTPUT-

3
2) Write a program to perform operation on Singly LinkedList.

A) Insert Node At End(Push)

B) Delete Node From End(pop)

Code:
class Node
{ constructor(data)
{ this.data = data;
this.next = null;
} } class SLL
{ constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
push(data) {
const newNode = new Node(data);
if (this.length > 0) { this.tail.next
= newNode; this.tail = newNode;
this.length++; return newNode;
}
this.head = newNode;
this.tail = newNode;
this.length = +1; return
newNode;
}
pop() {
const newNode = new Node();
if (!this.head) {
console.log("list is empty");
}
if (this.length == 1) {
let temp = this.tail;
this.head = null;
this.tail = null;
this.length--;
return newNode;
}
}}
const n1 = new SLL();
n1.push(10);
n1.push(20);
n1.push(30); n1.pop();
console.log(n1);
OUTPUT-

5
3) Create Doubly Linked List program and perform following
operation.
A) Insert node from front(Unshift)
B) Delete node from front(Shift) Code:
class Node
{ constructor(value)
{ this.value = value;
this.next = null; this.prev
= null;
} } class Doubly1 {
constructor(value) {
const newNode = new Node(value);
this.head = newNode; this.tail =
newNode; this.length = 0;
}
unshift(value) {
const newNode = new Node(value);
if (this.length === 0)
{ this.head = newNode;
this.tail = newNode;
} else {
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
}
this.length++;
return this;
}
shift() {
const temp = this.head;
if (this.length === 0) {
return undefined;
}
if (this.length === 1) {
this.next = null; this.tail
= null;
} else {
const temp = this.head.next;
this.head.next = null; this.prev
= null;
this.head = temp;
}
this.length--;
return this;
}
displayForward() { let
current = this.head; while
(current != null)
{ console.log(current.dat
a);
current = current.next;
}
}
displayBackward() {
let current = this.tail;
while (current)
{ console.log(current.dat
a);
current = current.prevoius;
}
} } const d1 = new
Doubly1(); d1.unshift(10);
d1.unshift(20);
d1.unshift(30);
d1.unshift(40);
//console.log(d1);
//d1.shift();
console.log(d1);
console.log("Forward");
d1.displayForward();
console.log("Backward");
d1.displayBackward();

7
OUTPUT-
4) Create Doubly Linked List program and perform following
operation.
A) Insert node from
End(PUSH)
B) Delete node from End(POP)

Code:

class Node
{ constructor(data)
{ this.data = data;
this.next = null;
this.previous = null;
}}
class DoublyLinkedList {
constructor(data)
{ this.head = null;
this.tail = null;
}
insertAtBegining(data) { const
newNode = new Node(data);
if (this.head === null)
{ this.head = newNode;
this.tail = newNode;
} else {
newNode.next = this.head;
this.head.prevoius = newNode;
this.head = newNode;
}
}
InsertATEnd(data) { const
newNode = new Node(data);
if (this.head === null) {
this.head = newNode;
this.tail - newNode;
} else {
newNode.previous = this.tail;
this.tail.next = newNode; this.tail
= newNode;
9
}
}
displayForward() { let
current = this.head; while
(current != null)
{ console.log(current.dat
a);
current = current.next;
}
}
displayBackward() {
let current = this.tail;
while (current)
{ console.log(current.data
); current =
current.prevoius;
}
}}
const myList = new DoublyLinkedList();

myList.insertAtBegining(3);
myList.insertAtBegining(2);
myList.insertAtBegining(1);
//myList.InsertATEnd(5);
//myList.InsertATEnd(6);
console.log(myList);
console.log("Forward");
myList.displayForward();
console.log("Backward");
myList.displayBackward();

OUTPUT-
11
Roll No-21
Name – Yogesh Uttam Jadhav
Assignment No
Assignment No 05) Create Stack Using array and perform following operation.
1) PUSH 2) POP 3)PEEK Code:
class Stack
{ constructor()
{ this.array = Array();
this.top = -1;
this.length = 0;
}
push(value) {
this.top++;
this.array[this.top] = value;
this.length++; return this;
}
Pop() {
if (this.length === 0)
{ console.log("stack is empty");
}
return (this.array[this.top] = null);
this.top--;
this.length--;
}
peek() {
return this.array[this.top];
}}
const s = new Stack(); s.push(20);
s.push(21);
s.push(22);
s.push(23);
s.push(24);
s.push(25);
console.log(s);
console.log(s.peek());
s.Pop(); console.log(s);

OUTPUT
Roll No-22

Name –

Assignment No 6) Create Stack using Linked List and perform following operations
a)PUSH b)POP c)PEEK d) isEmptye e)Display

Code:

class Node
{ constructor(value)
{ this.value = value;
this.next = null;
} } class stackLL {
constructor(value) {
const newnode = new
Node(value);
this.top = newnode;
this.length = 0;
}
push(value) {
const newnode = new
Node(value);
if (this.length === 0)
{ this.top = newnode;
} else {
newnode.next = this.top;
this.top = newnode;
}
this.length++;
} pop() { const temp
= this.top; if (!
this.length === 0)
{ return undefined;
} else { this.top =
temp.next;
temp.next = null;
}
this.length--;
return temp;
} isempty() { if (this.top ===
null) { console.log("Stack is
empty"); return (this.top =
null);
} else {
console.log("Stack contains Elements");
}
return this;
} display() { let
temp = this.top;
while (temp)
{ console.log(te

13
Roll No-21
Name – Yogesh Uttam Jadhav
Assignment No

mp.value); temp
= temp.next;
} }
peek() {
return this.top.value;
}}
const n = new stackLL(); n.push(11);
n.push(22);
n.push(34); console.log(n.isempty());
//console.log(n.isempty());

OUTPUT

Roll No-22

Name –

Assignment No 07) Write a Program to Create Stack using Array to reverse Stack
Code:

class Stack
{ constructor()
{ this.items = [];
}
push(item) {
this.items.push(item);
} pop() { if
(this.isEmpty()) {
return "Stack is empty";
}
return this.items.pop();
} peek() { if
(this.isEmpty()) {
return "Stack is empty";
}
return this.items[this.items.length - 1];
}
isEmpty() {
return this.items.length === 0;
}
size() {
return this.items.length;
}
reverse() {
const reversedStack = new Stack();
while (!this.isEmpty()) {
reversedStack.push(this.pop());
}
return reversedStack;
}
}
// Example usage: const
stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
console.log("Original Stack: " + stack.items.join(", ")); const
reversedStack = stack.reverse();
console.log("Reversed Stack: " + reversedStack.items.join(", "));

OUTPUT

15
Roll No-21
Name – Yogesh Uttam Jadhav
Assignment No

Roll No-22

Name –
Assignment No 08) Write a Program to Check for balanced parentheses by using Stacks

Code:

function areParenthesesBalanced(inputString) {
const stack = []; const openingBrackets =
"({["; const closingBrackets = ")}]"; for (let
char of inputString) { if
(openingBrackets.includes(char)) {
stack.push(char);
} else if (closingBrackets.includes(char))
{ const topOfStack = stack.pop();
if (!topOfStack || !areBracketsMatching(topOfStack, char))
{ return false;
}
}
}
return stack.length === 0;
}
function areBracketsMatching(opening, closing) {
const bracketPairs = {
"(": ")",
"{": "}",
"[": "]",
};
return bracketPairs[opening] === closing;
}
//const testString1 = "{[()]}";
//const testString2 = "{[(])}"; const
testString1 = "(a+b)*[d-c]"; const
testString2 = "(a+b)+ (c-d";

console.log(
`"${testString1}" is balanced: ${areParenthesesBalanced(testString1)}`
);
console.log(
`"${testString2}" is balanced: ${areParenthesesBalanced(testString2)}`
);
OUTPUT:

17
Roll No-21
Name – Yogesh Uttam Jadhav
Assignment No

Roll No-21
Name – Yogesh Uttam Jadhav
Assignment No
Create Queue using Linked List and perform following operations

a)Enqueue b)Dequeue c)Peek

Code:

class Node
{ constructor(data)
{ this.data = data;
this.next = null;
} } class Queue {
constructor()
{ this.front =
null; this.rear =
null; this.length
= 0;
}
enqueue(data) {
const newNode = new Node(data);
if (!this.length) { this.front =
newNode; this.rear = newNode;
} else {
this.rear.next = newNode;
this.rear = newNode;
}
this.length++;
return this;
}

dequeue() { if
(!this.length) {
return undefined;
}
if (this.length == 1) { let
removedData = this.front;
this.front = -1;
this.rear = -1;
this.length--;
return removedData;
} else { let removedData
= this.front; this.front =
this.front.next;
removedData.next = null;

19
this.length--;
return removedData;
}
}

peek() { if (!
this.front) {
return null; // Queue is empty
} return
this.front.data;
}
// Check if the queue is empty
isEmpty() { return
this.length === 0;
}
// Get the size of the queue
getSize() { return
this.length;
}

traverse() { if (this.length ===


0) { console.log("Queue is
Empty");
} else { let current =
this.front; while (current)
{ console.log(current.dat
a);
current = current.next;
}
}
}}
const Q = new Queue();
Q.enqueue(10);
Q.enqueue(20); Q.traverse();
// console.log(Q)
Q.dequeue();
// console.log(Q.isEmpty() )
// console.log(Q.peek() )
Q.dequeue();
// console.log(Q.isEmpty() )
Q.traverse();
Q.enqueue(20);
Q.traverse();

OUTPUT:
21
Roll No-21
Name – Yogesh Uttam Jadhav

Assignment No 10) Create Circular Queue using Array and perform following
operations
a)Enqueue b)Dequeue c)Peek d)isEmpty E)Traverse f)GetSize

Code:

class cQueue
{ constructor(maxsize)
{ this.items = new
Array(maxsize); this.maxsize =
maxsize; this.length = 0;
this.front = -1; this.rear = -1;
}
isFull() {
return this.length == this.maxsize;
}
isEmpty() {
return this.length == 0;
}
enqueue(element) {
if (!this.isFull()) {
this.rear = (this.rear + 1) % this.maxsize;
this.items[this.rear] = element; this.length++;

if (this.front === -1)


{ this.front = 0;
}
} else {
return "Queue is Full";
}
}
dequeue() { if
(this.isEmpty()) { return
"Queue is Empty";
} else { let item =
this.items[this.front];
this.items[this.front] = null;
this.length--; if
(this.isEmpty()) { this.front =
-1; this.front = -1;
}
this.front++;
return item;
} }
peek() {
if (this.isEmpty()) {
return "Queue is empty";
} else { return
this.items[this.front];
}
}
printQueue() { return
this.items.toString();
} } let cq = new cQueue(5);
cq.enqueue(10);
cq.enqueue(20);
cq.enqueue(30);
console.log(cq);
console.log(cq.printQueue());
cq.dequeue(); cq.dequeue();
cq.dequeue();

// cq.enqueue(50)
console.log(cq.printQueue()); //
console.log(cq.peek())
//console.log(cq)
cq.enqueue(20);
console.log(cq.printQueue());
console.log(cq);
cq.enqueue(70);
console.log(cq.printQueue());
console.log(cq);

23
OUTPUT:
Roll No-21

Name – Yogesh Uttam Jadhav

Assignment No 11 ) Create Circular Queue using linked list and perform following
operations
a)Enqueue b)Dequeue c)Peek d)isEmpty E)Traverse f)GetSize

Code:
class Node
{ constructor(data)
{ this.data = data;
this.next = null;
} } class
CircularQueue
{ constructor()
{ this.front = null;
this.rear = null;
this.length = 0;
}
enqueue(data) {
const newNode = new Node(data);
if (!this.front)
{ this.front = newNode;
this.rear = newNode;
this.rear.next = this.front; // Make it circular
} else {
this.rear.next = newNode;
this.rear = newNode;
this.rear.next = this.front; // Make it circular
}
this.length++;
}
dequeue() { if
(!this.front) {
return null; // Queue is empty
}

const removedData = this.front.data;


if (this.front === this.rear)

25
{ this.front = null; this.rear =
null;
} else {
this.front = this.front.next;
this.rear.next = this.front; // Update the circular reference
}
this.length--;
return
removedData;
} peek() { if
(!this.front) {
return null; // Queue is empty
} return
this.front.data;
}

isEmpty() { return
this.length === 0;
}

getSize()
{ return
this.length;
} frontele() { if (this.length == 0)
{ console.log("Circular Queue is
Empty");
} else
{ console.log(this.front);
}
}

Rareele() { if (this.length == 0)
{ console.log("Circular Queue is
Empty");
} else
{ console.log(this.rear);
}
}
}

const cq = new CircularQueue();


cq.enqueue(10);
cq.enqueue(20);
console.log(cq); cq.frontele();
cq.Rareele();
OUTPUT:

27
12) Create Priority Queue using Array and perform following
operations

a)Enqueue b)Dequeue c)Peek d)isEmpty E)Traverse

Code:

class pqueue
{ constructor()
{ this.items = [];
}
enqueu(element, priority)
{ const item = { element,
priority };
let add = false; for (let i = 0; i <
this.items.length; i++) { if (item.priority
< this.items[i].priority)
{ this.items.splice(i, 0, item); add
= true; break;
} }
if (!add) {
this.items.push(item);
} } peek() { if
(!this.isEmpty())
{ return
this.items[0];
} else
{ return
null;
}
}
isEmpty() {
return this.items.length == 0;
}
dequeue() { if (!
this.isEmpty()) { return
this.items.shift();
} else
{ return
null;
} }
traverse() {
let str = "";
for (let i = 0;
i<
this.items.len
gth; i++) {
//
str+=this.ite
ms[i].elemen
t+" "
Roll No-21
Name – Yogesh Uttam Jadhav
Assignment No
str += `{ element: ${this.items[i].element} , priority: ${this.items[i].priority}}`;
}
return str;
}}
const pq = new pqueue();
pq.enqueu(10, 2); pq.enqueu(20,
1); pq.enqueu(35, 0);
console.log(pq.traverse());
pq.dequeue();
console.log(pq.traverse());

OUTPUT:

13) Create Priority Queue using Linked List and Print all Nodes in
Ascending
Order.
A)Enqueue b) Dequeue c)printAsc

Code:

29
class Node {
constructor(data, priority) {
this.data = data; this.priority
= priority; this.next = null;
} } class
PriorityQueue
{ constructor()
{ this.front = null;
}
enqueue(data, priority) { const newNode =
new Node(data, priority); if (!this.front ||
priority < this.front.priority) {
newNode.next = this.front;
this.front = newNode;
return;
}
let current = this.front;
while (current.next && priority >= current.next.priority)
{ current = current.next;
}
newNode.next = current.next;
current.next = newNode;
}
dequeue() { if
(!this.front) {
console.log("Priority queue is empty.");
return null;
}
const removedNode = this.front;
this.front = this.front.next;
return removedNode.data;
}
print() {
let current = this.front;
while (current) {
console.log(current.data + " (priority: " + current.priority + ")");
current = current.next;
}
}}
const pq = new PriorityQueue();
pq.enqueue("Task A", 2);
pq.enqueue("Task B", 1);
pq.enqueue("Task C", 3);
pq.enqueue("Task D", 2);
console.log("Priority Queue (in ascending order of priority):"); pq.print();
Roll No-21
Name – Yogesh Uttam Jadhav
Assignment No

OUTPUT:

14 )Create Priority Queue using Linked List and Print all Nodes in
Descending
Order
A)Enqueue b) Dequeue c)printdesc

class Node {

31
constructor(data, priority) {
this.data = data; this.priority
= priority; this.next = null;
} } class
PriorityQueue
{ constructor()
{ this.front = null;
}
enqueue(data, priority) { const newNode =
new Node(data, priority); if (!this.front ||
priority > this.front.priority) {
newNode.next = this.front;
this.front = newNode;
return;
}
let current = this.front;
while (current.next && priority <= current.next.priority)
{ current = current.next;
}
newNode.next = current.next;
current.next = newNode;
}
dequeue() { if
(!this.front) {
console.log("Priority queue is empty.");
return null;
}
const removedNode = this.front;
this.front = this.front.next;
return removedNode.data;
}
print() {
let current = this.front;
while (current) {
console.log(current.data + " (priority: " + current.priority + ")");
current = current.next;
}
}}
const pq = new PriorityQueue();
pq.enqueue("Task A", 2);
pq.enqueue("Task B", 1);
pq.enqueue("Task C", 3);
pq.enqueue("Task D", 2);
console.log("Priority Queue (in descending order of priority):"); pq.print();
console.log("\nDequeue elements from the Priority Queue:");
console.log(pq.dequeue()); // Dequeue highest priority element
console.log(pq.dequeue()); // Dequeue next highest priority element console.log("\
nUpdated Priority Queue (in descending order of priority):"); pq.print();
Roll No-21
Name – Yogesh Uttam Jadhav
Assignment No

OUTPUT:

33
Roll No-21
Name – Yogesh Uttam Jadhav
Assignment No

Code:
15) Create Binary Tree search tree and perform following operation
1)Insert

class Node
{ constructor(data)
{ this.data = data;
this.left = null;
this.right = null;
} } class BST {
constructor()
{ this.root =
null;
}
insert(data) {
const newNode = new Node(data);
if (!this.root)
{ this.root =
newNode;
return this;
}
let current = this.root;
function addNode() {
if (data == current.data) return undefined;
if (data < current.data) {
if (!current.left)
{ current.left =
newNode;
return this;
}
current = current.left;
addNode();
} else if (data > current.data) {
if (!current.right)
{ current.right = newNode;
return this;
}
current = current.right;
addNode();
}
}
addNode();
}
}
Roll No-21
Name – Yogesh Uttam Jadhav
Assignment No

Code:
const b = new BST(); b.insert(10);
b.insert(5);
b.insert(35);
b.insert(40); console.log(b);
console.log(b.search(b.root, 40));
let d = b.search(b.root, 5);

OUTPUT:

16) Create Binary Tree search tree and perform following operation
1)Insert 2) Search

35
class Node
{ constructor(data)
{ this.data = data;
this.left = null;
this.right = null;
} } class BST {
constructor()
{ this.root =
null;
}
insert(data) {
const newNode = new Node(data);
if (!this.root)
{ this.root =
newNode;
return this;
}
let current = this.root;
function addNode() {
if (data == current.data) return undefined;
if (data < current.data) {
if (!current.left)
{ current.left =
newNode;
return this;
}
current = current.left;
addNode();
} else if (data > current.data) {
if (!current.right)
{ current.right = newNode;
return this;
}
current = current.right;
addNode();
}
}
addNode();
}
search(root, val) {
if (root === null) {
return false;
} else if (root.data === val)
{ return true;
} else if (root.data > val)
{ return this.search(root.left,
val); } else {
return this.search(root.right, val);
}
}}
const b = new BST(); b.insert(10);
Roll No-21
Name – Yogesh Uttam Jadhav
Assignment No

Code:
b.insert(5);
b.insert(35);
b.insert(40); console.log(b);
console.log(b.search(b.root, 40));
let d = b.search(b.root, 5);
if (d) {
console.log("Element found");
} else {
console.log("Element not found");
}

OUTPUT:

17) Create Binary Tree search tree and perform following operation

37
1)Insert 2) Inorder 3) Preorder 4)Postorder

class Node
{ constructor(data)
{ this.data = data;
this.left = null;
this.right = null;
} } class BST {
constructor()
{ this.root =
null;
}
insert(data) {
const newNode = new Node(data);
if (!this.root)
{ this.root =
newNode;
return this;
}
let current = this.root;
function addNode() {
if (data == current.data) return undefined;
if (data < current.data) {
if (!current.left)
{ current.left =
newNode;
return this;
}
current = current.left;
addNode();
} else if (data > current.data) {
if (!current.right)
{ current.right = newNode;
return this;
}
current = current.right;
addNode();
}
}
addNode();
}
preOrder(root) {
if (root) {
console.warn(root.data);
this.preOrder(root.left);
this.preOrder(root.right);
}
}
Roll No-21
Name – Yogesh Uttam Jadhav
Assignment No

Code:
inOrder(root) { if (root)
{ this.inOrder(root.left);
console.warn(root.data);
this.inOrder(root.right);
}
}
postOrder(root) { if
(root)
{ this.inOrder(root.left);
this.inOrder(root.right);
console.warn(root.data);
}
}}
const b = new BST(); b.insert(10);
b.insert(5);
b.insert(35);
b.insert(40); console.log(b);
b.preOrder(b.root);
console.log("--------");
b.postOrder(b.root);
console.log("--------");
b.inOrder(b.root);

Output

39
Roll No-21
Name – Yogesh Uttam Jadhav
Assignment No

18) Create Binary Tree search tree and perform following operation.
a)Min b)Max c) Insert
Code:
class Node
{ constructor(data)
{ this.data = data;
this.left = null;
this.right = null;
} } class BST {
constructor()
{ this.root =
null;
}
insert(data) {
const newNode = new Node(data);
if (!this.root)
{ this.root =
newNode;
return this;
}
let current = this.root;
function addNode() {
if (data == current.data) return undefined;
if (data < current.data) {
if (!current.left)
{ current.left =
newNode;
return this;
}
current = current.left;
addNode();
} else if (data > current.data) {
if (!current.right)
{ current.right = newNode;
return this;
}
current = current.right;
addNode();
}
}
addNode();
}

41
minValue(root) {
if (!root.right)
{ return
root.data;
} else {
return this.minValue(root.left);
}
}
maxValue(root) {
if (!root.right)
{ return
root.data;
} else {
return this.maxValue(root.right);
}
}
search(root, val) {
if (root === null) {
return false;
} else if (root.data === val)
{ return true;
} else if (root.data > val)
{ return this.search(root.left,
val);
} else {
return this.search(root.right, val);
}
}}
const b = new BST(); b.insert(10);
b.insert(5);
b.insert(35);
b.insert(40); console.log(b);
console.log(b.minValue(b.root));
console.log(b.maxValue(b.root));

OUTPUT:
Roll No-21
Name – Yogesh Uttam Jadhav
Assignment No

43

You might also like