0% found this document useful (0 votes)
7 views40 pages

Singly and Doubly Linked List Operations

The document provides a comprehensive set of programs for implementing and manipulating singly and doubly linked lists, as well as stacks and queues using both arrays and linked lists. It includes operations such as inserting and deleting nodes at various positions, traversing the lists, and managing stack and queue functionalities. Each program is structured with classes for nodes and lists, demonstrating various methods for list manipulation and data handling.

Uploaded by

manojpatil
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)
7 views40 pages

Singly and Doubly Linked List Operations

The document provides a comprehensive set of programs for implementing and manipulating singly and doubly linked lists, as well as stacks and queues using both arrays and linked lists. It includes operations such as inserting and deleting nodes at various positions, traversing the lists, and managing stack and queue functionalities. Each program is structured with classes for nodes and lists, demonstrating various methods for list manipulation and data handling.

Uploaded by

manojpatil
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

1.

Write a Program to perform operations on Singly Linked List


a)Insert Node at Front
b)Delete Node from Front

class Node
{
constructor(value)
{
[Link] = value;
[Link] = null;
}
}
class SinglyLinkedList {
constructor() {
[Link] = 0;
[Link] = null;
[Link] = null;
}
unshift(value)
{
const newNode = new Node(value);
if (![Link])
{
[Link] = newNode;
[Link] = newNode;
}
Else
{
[Link] = [Link];
[Link] = newNode;
}
[Link] += 1;
return newNode;
}
shift()
{
if (![Link])
{
return undefined

}
if([Link]==1)
{
[Link]=null
[Link]=null
[Link]=0
}
Else
{
const nodeToRemove = [Link];
[Link] = [Link];
[Link] -= 1;
[Link](nodeToRemove)
}
if (![Link])
{
[Link] = null;
}

}
}
const SLL =new SinglyLinkedList()
[Link](10)
[Link](20)
[Link](30)
[Link](SLL)
[Link]()
[Link](SLL)
2. Write a program to perform operation on Singly Linked List
a)Insert Node at END
b)Delete Node from END

class Node
{
constructor(value)
{
[Link] = value;
[Link] = null;
}
}
class SinglyLinkedList
{
constructor() {
[Link] = 0;
[Link] = null;
[Link] = null;
}

push(value)
{
const newNode = new Node(value);
if ([Link] > 0)
{
[Link] = newNode;
[Link] = newNode;
[Link] += 1;
return newNode;
}
else
{
[Link] = newNode;
[Link] = newNode;
[Link] += 1;
return newNode;
}
}

pop()
{
if(![Link]) //Case -1 if List is Empty
{
return undefined
}
if([Link]==1)
{
let temp=[Link]
[Link]=null
[Link]=null
[Link]--
return temp;
}
else
{

let temp=[Link]
let prev=[Link]
while([Link]!=temp)
{
prev=[Link]
}
[Link]=null
[Link]=prev
[Link]--
return temp

}
}
}
const SLL =new SinglyLinkedList()

[Link](14)
[Link](33)
[Link](53)
[Link](79)
[Link](SLL)
[Link]()
[Link](SLL)
3. Write a Program to perform operation on Singly Link List
a)To insert Node at given Position
b)Traverse Entire List and Print data

class Node
{
constructor(value)
{
[Link] = value;
[Link] = null;
}
}
class SinglyLinkedList
{
constructor() {
[Link] = 0;
[Link] = null;
[Link] = null;
}
push(value)
{
const newNode = new Node(value);
if ([Link] > 0)
{
[Link] = newNode;
[Link] = newNode;
[Link] += 1;
return newNode;
}
else
{
[Link] = newNode;
[Link] = newNode;
[Link] += 1;
return newNode;
}
}

insertMiddle(value,pos) //Inserting a node at the given position


{
const newNode = new Node(value);
if(pos==0)
{
[Link]=[Link]
[Link]=newNode
[Link]=newNode
[Link]++
return newNode
}
let current=[Link]
let currentpos=0
while(currentpos<pos-1 && [Link]!=null)
{
current=[Link]
currentpos++
}
if(currentpos<pos-1 )
{
[Link]("invalid position")
[Link]=newNode
}
else
{
[Link]=[Link]
[Link]=newNode
[Link]=newNode
[Link]++
}
return newNode
}

traverse()
{
[Link]([Link])
let current=[Link]
while(current)
{
[Link]([Link])
current=[Link]
}
return this;
}
}
const newSLL = new SinglyLinkedList();

[Link](10)
[Link](20)
[Link](10,0) //Trying to insert Node at 0th postion
[Link](10,1) //Trying to insert Node at 1th postion
[Link](newSLL)
4. Write a Program to perform operation on Singly Link List to insert Node after Given Node
class Node
{
constructor(data)
{
[Link] = data;
[Link] = null;
}
}
class SinglyLinkedList
{
constructor()
{
[Link] = null;
}
insertAfter(prevNodeData, newData)
{
const newNode = new Node(newData);
if (![Link])
{
[Link] = newNode;
return; // Node inserted successfully
}
let current = [Link];
while (current)
{
if ([Link] === prevNodeData)
{
[Link] = [Link];
[Link] = newNode;
return;
}
current = [Link];
}
[Link]("node not found" )
}
display()
{
let current = [Link];
while (current !== null)
{
[Link]([Link])
current = [Link];
}
[Link]("null");
}
}
const myList = new SinglyLinkedList();
[Link](1, 2); // Try to insert after 1 when the list is empty
[Link](0, 1); // Insert 1 at the beginning
[Link](1, 2); // Insert 2 after 1
[Link](2, 4); // Insert 4 after 2

[Link]("Linked List after Insertions:");


[Link]();
5. Create doubly link list program and perform following operation
a)insert node from front
b)delete node from front

class Node
{
constructor(value)
{
[Link]=value
[Link]=null
[Link]=null
}
}
class doubly
{
constructor(value)
{
const newnode=new Node(value);
[Link]=newnode;
[Link]=newnode;
[Link]=0;
}
unshift(value)
{
const newnode=new Node(value);
if([Link]===0)
{
[Link]=newnode;
[Link]=newnode;
}
else
{
[Link]=[Link];
[Link]=newnode;
[Link]=newnode;
}
[Link]++;
return this;
}
shift()
{
const temp=[Link]
if([Link]===0)
{
return undefined
}
if([Link]===1)
{
[Link]=null
[Link]=null
}
else
{
const temp=[Link]
[Link]=null
[Link]=null
[Link]=temp
}
[Link]--
return this
}

const d1=new doubly();


[Link](40);
//[Link](d1);
[Link](60);
//[Link](d1);
[Link](50);
//[Link](d1);
[Link]()
[Link](d1);
6. Create doubly link list program and perform following operation
a)insert node from END
b)delete node from END

class Node
{
constructor(value)
{
[Link]=value
[Link]=null
[Link]=null
}
}
class doubly
{
constructor(value)
{
const newnode=new Node(value);
[Link]=newnode;
[Link]=newnode;
[Link]=0;
}
push(value)
{
const newnode=new Node(value);
if([Link]===0)
{
[Link]=newnode;
[Link]=newnode;
}
else
{
[Link]=newnode
[Link]=[Link];
[Link]=newnode;
}
[Link]++;

}
pop()
{
const temp=[Link]
if([Link]===0)
{
return undefined
}
if([Link]===1)
{
[Link],next=null
[Link]=null
[Link]=null
}
else
{
const temp=[Link]
[Link]=null
[Link]=null
[Link]=temp
[Link]=temp
}
[Link]--
return temp
}

const d1=new doubly();


[Link](40);
//[Link](d1);
[Link](60);
// //[Link](d1);
[Link](50);
// [Link](80);
// //[Link](d1);
[Link]()
[Link](d1);
7. Write a Program To insert a node at the middle of a doubly linked list
Steps:
1. Calculate the middle position based on the length of the list. If the length is even, you can choose
either of the two middle positions.
2. Traverse the list to find the node at the middle position.
3. Create a new node with the given data.
4. Update the pointers of the new node, the node before the middle, and the node at the middle to
insert the new node into the list.

class Node
{
constructor(data)
{
[Link] = data;
[Link] = null;
[Link] = null;
}
}

class DoublyLinkedList
{
constructor()
{
[Link] = null;
[Link] = null;
}

// Function to calculate the length of the doubly linked list


getLength()
{
let length = 0;
let current = [Link];
while (current)
{
length++;
current = [Link];
}
return length;
}

// Function to insert a node at the middle


insertAtMiddle(data)
{
const newNode = new Node(data);

if (![Link])
{
[Link] = newNode;
[Link] = newNode;
return;
}
const length = [Link]();
const middlePosition = [Link](length / 2);

if (middlePosition === 0)
{
[Link] = [Link];
[Link] = newNode;
[Link] = newNode;
}
Else
{
let current = [Link];
let currentPosition = 0;

while (currentPosition < middlePosition)


{
current = [Link];
currentPosition++;
}

// Insert the new node before the current node (at the middle)
[Link] = [Link];
[Link] = current;
[Link] = newNode;
[Link] = newNode;
}
}

// Function to display the doubly linked list forward


displayForward()
{
let current = [Link];
let output = "";
while (current) {
output += `${[Link]} <-> `;
current = [Link];
}
[Link](output + "null");
}
}

// Create a doubly linked list and insert nodes at the middle


const myList = new DoublyLinkedList();
[Link](1);
[Link](2);
[Link](3);
[Link](myList)
[Link](4);
[Link](myList)
// Display the modified doubly linked list forward
[Link]("Doubly Linked List (Forward):");
[Link]();
8. Write a Program to insert Node at given position in doubly Linked List
Steps:-
1. Create a new node with the given data.
2. Traverse the list to find the node just before the desired position (at position - 1).
3. Update the next pointer of the new node to point to the node at the desired position (at position).
4. Update the previous pointer of the new node to point to the node at position - 1.
5. Update the next pointer of the node at position - 1 to point to the new node.
6. Update the previous pointer of the node at position (if it exists) to point to the new node.

class Node
{
constructor(data)
{
[Link] = data;
[Link] = null;
[Link] = null;
}
}
class DoublyLinkedList
{
constructor()
{
[Link] = null;
[Link] = null;
}
getLength()
{
let length = 0;
let current = [Link];
while (current)
{
length++;
current = [Link];
}
return length;
}

// Function to insert a node at a specific position and


insertAtPosition(position, data)
{
if (position < 0 || position > [Link]())
{
[Link]("Invalid position. Node not inserted.");
return;
}

const newNode = new Node(data);


if (position === 0)
{
[Link] = [Link];
if ([Link])
{
[Link] = newNode;
}
[Link] = newNode;
if (![Link])
{
[Link] = newNode;
}
}
else
{

let current = [Link];


let currentPosition = 0;
while (currentPosition < position - 1)
{
current = [Link];
currentPosition++;
}
[Link] = [Link];
[Link] = current;
[Link] = newNode;

if ([Link])
{
[Link] = newNode;
}
Else
{
[Link] = newNode;
}
}
}
displayForward()
{
let current = [Link];
let output = "";
while (current) {
output += `${[Link]} <-> `;
current = [Link];
}
[Link](output + "null");
}
}
// Create a doubly linked list and insert nodes at specific positions
const myList = new DoublyLinkedList();
[Link](0, 1); // Insert 1 at the beginning
[Link](1, 2); // Insert 2 after 1
[Link](2, 4); // Insert 4 at the end
[Link](2, 3); // Insert 3 at position 2

[Link]("Doubly Linked List:");


[Link]();
9. Create Stack using Array and perform following operations
a)PUSH b)POP c)PEEK
class stack
{
constructor()
{
[Link]=Array()
[Link]=0
[Link]=-1
}

push(value)
{
[Link]++
[Link][[Link]]=value
[Link]++
return this
}
pop(){
if(!this==0)
{
[Link][[Link]]=null
[Link]--
[Link]--
return this
}
}
peek()
{
return [Link][[Link]]
}

const s=new stack()


[Link](10)
[Link](20)
[Link]()
[Link](s)
[Link]([Link]())
10. Create Stack using Linked List and perform following operations
a)PUSH b)POP c)PEEK d) isEmpty e)Display

class Node
{
constructor(value)
{
[Link]=value
[Link]=null
}
}
class stackLL
{
constructor(value)
{
const newnode=new Node(value);
[Link]=newnode;
[Link]=0;
}
push(value)
{
const newnode=new Node(value);
if([Link]===0)
{
[Link]=newnode
}
else
{
[Link]=[Link]
[Link]=newnode
}
[Link]++

}
pop()
{
const temp=[Link]
if(![Link]===0)
{
return undefined
}
else{
[Link]=[Link]
[Link]=null

}
[Link]--
return temp

}
isempty()
{
if([Link]===null)
{
[Link]("Stack is empty")
return [Link]=null
}
else {
[Link]("Stack contains Elements")
}
return this

}
display()
{
let temp=[Link]
while(temp)
{
[Link]([Link])
temp=[Link]
}
}

peek()
{
return [Link]
}
}
const n=new stackLL()
[Link](11)
[Link](22)
[Link](34)

[Link]([Link]())
//[Link]([Link]())
11. Create Queue using Linked List and perform following operations
a)Enqueue b)Dequeue c)Peek d)isEmpty E)Traverse f)GetSize
class Node
{
constructor(data)
{
[Link] = data;
[Link] = null;
}
}
class Queue
{
constructor()
{
[Link] = null;
[Link] = null;
[Link] = 0;
}
enqueue(data)
{
const newNode = new Node(data);
if (![Link])
{
[Link] = newNode;
[Link] = newNode;
}
else
{
[Link] = newNode;
[Link] = newNode;
}
[Link]++;
return this;
}

dequeue()
{
if (![Link])
{
return undefined
}
if([Link]==1)
{
let removedData = [Link]
[Link]=-1
[Link]=-1
[Link]--;
return removedData
}
else
{
let removedData = [Link]
[Link]=[Link]
[Link]=null
[Link]--;
return removedData
}

peek()
{
if (![Link])
{
return null; // Queue is empty
}
return [Link];
}
// Check if the queue is empty
isEmpty()
{
return [Link] === 0;
}
// Get the size of the queue
getSize()
{
return [Link];
}

traverse()
{
if([Link]===0)
{
[Link]("Queue is Empty")
}
else
{
let current=[Link]
while(current)
{
[Link]([Link])
current=[Link]
}
}
}

}
const Q=new Queue()
[Link](10)
[Link](20)
[Link]()
// [Link](Q)
[Link]()
// [Link]([Link]() )
// [Link]([Link]() )
[Link]()
// [Link]([Link]() )
[Link]()
[Link](20)
[Link]()

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

class cQueue
{
constructor(maxsize)
{
[Link]=new Array(maxsize)
[Link]=maxsize
[Link]=0
[Link]=-1
[Link]=-1
}
isFull()
{
return [Link]==[Link]
}
isEmpty()
{
return [Link]==0
}
enqueue(element)
{
if (![Link]())
{
[Link] = ([Link] + 1) % [Link];
[Link][[Link]] = element;
[Link]++;

if ([Link] === -1)


{
[Link] = 0;
}
}
else
{
return ("Queue is Full")
}
}

dequeue()
{
if([Link]())
{
return("Queue is Empty")
}
else
{
let item=[Link][[Link]]
[Link][[Link]]=null
[Link]--
if([Link]())
{
[Link]=-1
[Link]=-1
}
[Link]++
return item
}
}
peek()
{
if([Link]())
{
return "Queue is empty"
}
else
{
return [Link][[Link]]
}
}
printQueue()
{
return [Link]()
}
}

let cq=new cQueue(5)


[Link](10)
[Link](20)
[Link](30)
[Link](40)
[Link](50)
[Link](cq)
[Link]([Link]())
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
// [Link](50)
[Link]([Link]())
// [Link]([Link]())
//[Link](cq)
[Link](20)
[Link]([Link]())
[Link](cq)
[Link](70)
[Link]([Link]())
[Link](cq)
[Link]()
[Link]([Link]())
[Link](cq)
13. Create Circular Queue using Linked List and perform following operations
a)Enqueue b)Dequeue c)Peek d)isEmpty E)Traverse f)GetSize g)FRONT
f)REAR

Create Circular Queue


class Node {
constructor(data) {
[Link] = data;
[Link] = null;
}
}
class CircularQueue
{
constructor()
{
[Link] = null;
[Link] = null;
[Link] = 0;
}
enqueue(data) {
const newNode = new Node(data);
if (![Link]) {
[Link] = newNode;
[Link] = newNode;
[Link] = [Link]; // Make it circular
}
else
{
[Link] = newNode;
[Link] = newNode;
[Link] = [Link]; // Make it circular
}
[Link]++;
}
dequeue() {
if (![Link]) {
return null; // Queue is empty
}

const removedData = [Link];


if ([Link] === [Link])
{
[Link] = null;
[Link] = null;
} else {
[Link] = [Link];
[Link] = [Link]; // Update the circular reference
}
[Link]--;

return removedData;
}
peek()
{
if (![Link]) {
return null; // Queue is empty
}
return [Link];
}

isEmpty()
{
return [Link] === 0;
}

getSize()
{
return [Link];
}
frontele()
{
if([Link]==0)
{
[Link]("Circular Queue is Empty")
}
else
{
[Link]([Link])
}

Rareele()
{
if([Link]==0)
{
[Link]("Circular Queue is Empty")
}
else
{
[Link]([Link])
}

const cq=new CircularQueue()


[Link](10)
[Link](20)
[Link](cq)
[Link]()
[Link]()
14. Create Priority Queue using Array and perform following operations
a)Enqueue b)Dequeue c)Peek d)isEmpty E)Traverse

class pqueue
{
constructor()
{
[Link]=[]
}
enqueu(element,priority)
{
const item={element,priority}
let add=false
for(let i=0;i<[Link];i++)
{
if([Link]<[Link][i].priority)
{
[Link](i,0,item )
add=true
break
}
}
if(!add)
{
[Link](item)
}

}
peek(){
if(![Link]())
{
return [Link][0]
}
else
{
return null
}
}

isEmpty()
{
return [Link]==0
}

dequeue()
{
if(![Link]())
{
return [Link]()

}
else
{
return null
}
}
traverse()
{
let str=""
for(let i=0;i<[Link];i++)
{
// str+=[Link][i].element+" "
str+=`{ element: ${[Link][i].element} , priority: ${[Link][i].priority}}`
}
return str
}

const pq=new pqueue()


[Link](10,2)
[Link](20,1)
[Link](35,0)
[Link]([Link]())
[Link]()
[Link]([Link]())
15. Create Priority Queue using Linked List and Print all Nodes in Ascending Order
A)Enqueue b) Dequeue c)printAsc

class Node {
constructor(data, priority) {
[Link] = data;
[Link] = priority;
[Link] = null;
}
}

class PriorityQueue {
constructor() {
[Link] = null;
}

enqueue(data, priority)
{
const newNode = new Node(data, priority);

if (![Link] || priority < [Link])


{
[Link] = [Link];
[Link] = newNode;
return;
}

let current = [Link];


while ([Link] && priority >= [Link])
{
current = [Link];
}

[Link] = [Link];
[Link] = newNode;
}

dequeue()
{
if (![Link])
{
[Link]("Priority queue is empty.");
return null;
}

const removedNode = [Link];


[Link] = [Link];
return [Link];
}
print()
{
let current = [Link];
while (current)
{
[Link]([Link] + " (priority: " + [Link] + ")");
current = [Link];
}
}
}

const pq = new PriorityQueue();


[Link]("Task A", 2);
[Link]("Task B", 1);
[Link]("Task C", 3);
[Link]("Task D", 2);

[Link]("Priority Queue (in ascending order of priority):");


[Link]();
[Link] Priority Queue using Linked List and Print all Nodes in Descending Order
A)Enqueue b) Dequeue c)printdesc

class Node {
constructor(data, priority) {
[Link] = data;
[Link] = priority;
[Link] = null;
}
}

class PriorityQueue {
constructor() {
[Link] = null;
}

enqueue(data, priority) {
const newNode = new Node(data, priority);

if (![Link] || priority > [Link]) {


[Link] = [Link];
[Link] = newNode;
return;
}

let current = [Link];


while ([Link] && priority <= [Link]) {
current = [Link];
}

[Link] = [Link];
[Link] = newNode;
}

dequeue() {
if (![Link]) {
[Link]("Priority queue is empty.");
return null;
}

const removedNode = [Link];


[Link] = [Link];
return [Link];
}

print() {
let current = [Link];
while (current) {
[Link]([Link] + " (priority: " + [Link] + ")");
current = [Link];
}
}
}
const pq = new PriorityQueue();
[Link]("Task A", 2);
[Link]("Task B", 1);
[Link]("Task C", 3);
[Link]("Task D", 2);

[Link]("Priority Queue (in descending order of priority):");


[Link]();

[Link]("\nDequeue elements from the Priority Queue:");


[Link]([Link]()); // Dequeue highest priority element
[Link]([Link]()); // Dequeue next highest priority element

[Link]("\nUpdated Priority Queue (in descending order of priority):");


[Link]();
Implementation of general Binary Tree

function TreeNode(data) { constructor function


[Link] = data;
[Link] = null;
[Link] = null;
}

function createTree() { //is a function that creates a sample binary tree


let root = new TreeNode(10);

[Link] = new TreeNode(20);


[Link] = new TreeNode(30);

[Link] = new TreeNode(40);


[Link] = new TreeNode(50);

[Link] = new TreeNode(60);


[Link] = new TreeNode(70);

[Link] = new TreeNode(80);

return root
}
const root1 = createTree();
[Link]([Link])
[Link]([Link])
[Link]([Link])
[Link]([Link])
[Link] using iterative implementation to insert element in BST

//const treeify=require('treeify')
class Node
{
constructor(data){
[Link]=data
[Link]=null
[Link]=null
}
}
class BST{
constructor()
{
[Link]=null
}
insert(data)
{
const newNode=new Node(data)
if(![Link])
{
[Link]=newNode
return this
}
let current =[Link]
while(true)
{
if(data==[Link]) //CHECKING FOR duplicate value
return undefined
if(data<[Link])
{
if(![Link])
{
[Link]=newNode
return this
}
current=[Link]
}
else if (data>[Link])
{
if(![Link])
{
[Link]=newNode
return this
}
current=[Link]

}
}
}
}
const b=new BST()
//[Link]=new Node(20)
//[Link](b)
//[Link]([Link](b,true))
[Link](20)
[Link](10)
[Link](30)
[Link](b)

[Link] using recursive implementation to insert element in BST

Perform operations

a) Insert b) search

class Node
{
constructor(data)
{
[Link]=data
[Link]=null
[Link]=null
}
}
class BST
{
constructor()
{
[Link]=null
}
insert(data)
{
const newNode=new Node(data)
if(![Link])
{
[Link]=newNode
return this
}
let current =[Link]
function addNode() //recursion function
{
if(data==[Link])
return undefined
if(data<[Link])
{
if(![Link])
{
[Link]=newNode
return this
}
current=[Link]
addNode()
}
else if (data>[Link])
{
if(![Link])
{
[Link]=newNode
return this
}
current=[Link]
addNode()

}
}
addNode()
}

search(root, val)
{
if (root === null)
{
return false;
}
else if ([Link] === val)
{
return true;
}
else if ([Link] > val)
{
return [Link]([Link], val);
}
else
{
return [Link]([Link], val);
}
}
}
const b=new BST()
//[Link]=new Node(30)
//[Link](b)
//[Link]([Link](b,true))
[Link](10)
[Link](5)
[Link](35)
[Link](40)
[Link](b)

[Link]([Link]([Link],40))
let d=[Link]([Link],25)
if(d)
{
[Link]("Element found")
}
else
{
[Link]("Element found")
}
[Link] using recursive implementation to insert element in BST

Perform operations

a) Insert b) Min c) Max

class Node
{
constructor(data)
{
[Link]=data
[Link]=null
[Link]=null
}
}
class BST
{
constructor()
{
[Link]=null
}
insert(data)
{
const newNode=new Node(data)
if(![Link])
{
[Link]=newNode
return this
}
let current =[Link]
function addNode()
{
if(data==[Link])
return undefined
if(data<[Link])
{
if(![Link])
{
[Link]=newNode
return this
}
current=[Link]
addNode()
}
else if (data>[Link])
{
if(![Link])
{
[Link]=newNode
return this
}
current=[Link]
addNode()
}
}
addNode()
}

minValue(root)
{
if(![Link])
{
return [Link]
}
else{
return [Link]([Link])
}

}
maxValue(root)
{
if(![Link])
{
return [Link]
}
else{
return [Link]([Link])
}

}
const b=new BST()
//[Link]=new Node(30)
//[Link](b)
//[Link]([Link](b,true))
[Link](10)
[Link](5)
[Link](35)
[Link](40)
[Link](b)

[Link]([Link]([Link]))
[Link]([Link]([Link]))
[Link] using recursive implementation to insert element in BST

Perform operations

a) Insert b) InOrder c) PreOrder d) PostOrder

class Node
{
constructor(data)
{
[Link]=data
[Link]=null
[Link]=null
}
}
class BST
{
constructor()
{
[Link]=null
}
insert(data)
{
const newNode=new Node(data)
if(![Link])
{
[Link]=newNode
return this
}
let current =[Link]
function addNode()
{
if(data==[Link])
return undefined
if(data<[Link])
{
if(![Link])
{
[Link]=newNode
return this
}
current=[Link]
addNode()
}
else if (data>[Link])
{
if(![Link])
{
[Link]=newNode
return this
}
current=[Link]
addNode()

}
}
addNode()
}

preOrder(root)
{
if (root)
{
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
}
}
inOrder(root)
{
if (root)
{
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
}
}

postOrder(root)
{
if (root)
{
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
}
}

}
const b=new BST()
//[Link]=new Node(30)
//[Link](b)
//[Link]([Link](b,true))
[Link](10)
[Link](5)
[Link](35)
[Link](40)
[Link](b)
[Link]("----PreOrder----")
[Link]([Link])
[Link]("---PostOrder-----")
[Link]([Link])
[Link]("---InOder-----")
[Link]([Link])

Write a Program to Create Stack using Array to reverse Stack

class Stack {
constructor() {
[Link] = [];
}

push(item) {
[Link](item);
}

pop() {
if ([Link]()) {
return "Stack is empty";
}
return [Link]();
}

peek() {
if ([Link]()) {
return "Stack is empty";
}
return [Link][[Link] - 1];
}

isEmpty() {
return [Link] === 0;
}

size() {
return [Link];
}

reverse() {
const reversedStack = new Stack();
while (![Link]()) {
[Link]([Link]());
}
return reversedStack;
}
}

// Example usage:
const stack = new Stack();
[Link](1);
[Link](2);
[Link](3);
[Link](4);

[Link]("Original Stack: " + [Link](", "));


const reversedStack = [Link]();
[Link]("Reversed Stack: " + [Link](", "));

Write a Program to Check for balanced parentheses by using Stacks

function areParenthesesBalanced(inputString) {
const stack = [];
const openingBrackets = "({[";
const closingBrackets = ")}]";
for (let char of inputString) {
if ([Link](char)) {
[Link](char);
} else if ([Link](char)) {
const topOfStack = [Link]();
if (!topOfStack || !areBracketsMatching(topOfStack, char)) {
return false;
}
}
}
return [Link] === 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";

[Link](`"${testString1}" is balanced: ${areParenthesesBalanced(testString1)}`);


[Link](`"${testString2}" is balanced: ${areParenthesesBalanced(testString2)}`);

You might also like