DSA Program
DSA Program
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.push(element);
}
pop() {
if (this.isEmpty()) {
return "Underflow";
}
return this.items.pop();
}
peek() {
return this.items[this.items.length - 1];
}
isEmpty() {
return this.items.length === 0;
}
size() {
return this.items.length;
}
printStack() {
let result = "";
for (let i = 0; i < this.items.length; i++) {
result += this.items[i] + " ";
}
return result.trim();
} }
let stack = new Stack();
stack.push(10); stack.push(20); stack.push(30);
console.log("Stack after push operations:", stack.printStack()); // Output: 10 20 30
console.log("Popped element:", stack.pop()); // Output: 30
console.log("Stack after pop operation:", stack.printStack()); // Output: 10 20
console.log("Top element:", stack.peek()); // Output: 20
console.log("Is the stack empty?", stack.isEmpty()); // Output: false
console.log("Size of the stack:", stack.size()); // Output: 2
Q2. Write a javaScript program to implement queue using arrays.
class Queue {
constructor() {
this.items = [];
}
enqueue(element) {
this.items.push(element);
}
dequeue() {
if (this.isEmpty()) {
return "Underflow";
}
return this.items.shift();
}
front() {
if (this.isEmpty()) {
return "Queue is empty";
}
return this.items[0];
}
isEmpty() {
return this.items.length === 0;
}
size() {
return this.items.length;
}
printQueue() {
let result = "";
for (let i = 0; i < this.items.length; i++) {
result += this.items[i] + " ";
}
return result.trim();
} }
let queue = new Queue();
queue.enqueue(10); queue.enqueue(20); queue.enqueue(30);
console.log("Queue after enqueue operations:", queue.printQueue()); // Output: 10 20 30
console.log("Dequeued element:", queue.dequeue()); // Output: 10
console.log("Queue after dequeue operation:", queue.printQueue()); // Output: 20 30
console.log("Front element:", queue.front()); // Output: 20
console.log("Is the queue empty?", queue.isEmpty()); // Output: false
console.log("Size of the queue:", queue.size()); // Output: 2
Q3. Write a javaScript program implement the following Stack applications
a) infix into postfix
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.push(element);
}
pop() {
if (this.isEmpty()) {
return "Underflow";
}
return this.items.pop();
}
peek() {
return this.items[this.items.length - 1];
}
isEmpty() {
return this.items.length === 0;
} }
function isOperator(char) {
return ['+', '-', '*', '/'].includes(char);
}
function precedence(operator) {
switch (operator) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
} }
function infixToPostfix(infixExpression) {
let stack = new Stack();
let postfix = "";
for (let i = 0; i < infixExpression.length; i++) {
let char = infixExpression[i];
if (/[a-zA-Z0-9]/.test(char)) {
postfix += char;
} else if (char === '(') {
stack.push(char);
} else if (char === ')') {
while (!stack.isEmpty() && stack.peek() !== '(') {
postfix += stack.pop();
}
stack.pop(); // Pop '('
} else if (isOperator(char)) {
while (!stack.isEmpty() && precedence(stack.peek()) >= precedence(char)) {
postfix += stack.pop();
}
stack.push(char);
}
}
while (!stack.isEmpty()) {
postfix += stack.pop();
}
return postfix;
}
let infixExpression = "a+b*(c-d)/e";
let postfixExpression = infixToPostfix(infixExpression);
console.log("Infix Expression:", infixExpression);
console.log("Postfix Expression:", postfixExpression);
b) Circular queue
class CircularQueue {
constructor(size) {
this.size = size;
this.queue = new Array(size).fill(null);
this.front = -1;
this.rear = -1;
}
enqueue(element) {
if ((this.rear + 1) % this.size === this.front) {
console.log("Queue is full. Cannot enqueue element:", element);
} else {
if (this.isEmpty()) {
this.front = 0;
}
this.rear = (this.rear + 1) % this.size;
this.queue[this.rear] = element;
console.log("Enqueued element:", element);
} }
dequeue() {
if (this.isEmpty()) {
console.log("Queue is empty. Cannot dequeue.");
return null;
} else {
let removedElement = this.queue[this.front];
if (this.front === this.rear) {
this.front = -1;
this.rear = -1;
} else {
this.front = (this.front + 1) % this.size;
}
console.log("Dequeued element:", removedElement);
return removedElement;
} }
peek() {
if (this.isEmpty()) {
console.log("Queue is empty. Cannot peek.");
return null;
} else {
return this.queue[this.front];
} }
isEmpty() {
return this.front === -1 && this.rear === -1;
}
isFull() {
return (this.rear + 1) % this.size === this.front;
}
display() {
if (this.isEmpty()) {
console.log("Queue is empty.");
} else {
let result = "Queue elements: ";
let i = this.front;
while (i !== this.rear) {
result += this.queue[i] + " ";
i = (i + 1) % this.size;
}
result += this.queue[this.rear];
console.log(result);
} } } // Example usage:
let circularQueue = new CircularQueue(5);
circularQueue.enqueue(1); circularQueue.enqueue(2); circularQueue.enqueue(3);
circularQueue.display(); // Output: Queue elements: 1 2 3
circularQueue.dequeue();circularQueue.display(); // Output: Queue elements: 2 3
circularQueue.enqueue(4); circularQueue.enqueue(5);
circularQueue.enqueue(6); // Output: Queue is full. Cannot enqueue element: 6
console.log("Front element:", circularQueue.peek()); // Output: Front element: 2
Q5 Write a JavaScript program to implement the Singly Linked List
class Node {
constructor(data) {
this.data = data;
this.next = null;
}}
class SinglyLinkedList {
constructor() {
this.head = null;
}
append(data) {
let newNode = new Node(data);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
prepend(data) {
let newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
}
delete(data) {
if (!this.head) {
return;
}
if (this.head.data === data) {
this.head = this.head.next;
return; }
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.prev = this.tail;
this.tail.next = newNode;
this.tail = newNode;
} } // Insert a new node at the beginning of the doubly linked list
prepend(data) {
let newNode = new Node(data);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
} } // Delete a node with the given data from the doubly linked list
delete(data) {
let current = this.head;
while (current) {
if (current.data === data) {
if (current.prev) {
current.prev.next = current.next;
} else {
this.head = current.next;
}
if (current.next) {
current.next.prev = current.prev;
} else {
this.tail = current.prev;
}
return;
} current = current.next;
} } // Search for a node with the given data in the doubly linked list
search(data) {
let current = this.head;
while (current) {
if (current.data === data) {
return true;
}
current = current.next;
}
return false;
} // Display the elements of the doubly linked list
display() {
let result = [];
let current = this.head;
while (current) {
result.push(current.data);
current = current.next;
} console.log("Doubly Linked List:", result.join(" <-> "));
} }// Example usage:
let doublyLinkedList = new DoublyLinkedList();
doublyLinkedList.append(1); doublyLinkedList.append(2); doublyLinkedList.append(3);
doublyLinkedList.display(); // Output: Doubly Linked List: 1 <-> 2 <-> 3
doublyLinkedList.prepend(0);
doublyLinkedList.display(); // Output: Doubly Linked List: 0 <-> 1 <-> 2 <-> 3
doublyLinkedList.delete(2);
doublyLinkedList.display(); // Output: Doubly Linked List: 0 <-> 1 <-> 3
console.log("Is 2 in the doubly linked list?", doublyLinkedList.search(2)); // Output: Is 2 in
the doubly linked list? False
Q7. Write a javaScript program to implement the following search algorithms.
i) Linear search
function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i; // Return the index if the target is found
} }
return -1; // Return -1 if the target is not found
}// Example usage:
let array = [5, 2, 8, 1, 3, 7, 9, 4, 6];
let targetValue = 7;let result = linearSearch(array, targetValue);
if (result !== -1) {
console.log(`Target ${targetValue} found at index ${result}.`);
} else {
console.log(`Target ${targetValue} not found in the array.`);
}
ii). Binary search
// Binary Search function
function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
return mid; // Element found, return its index
} else if (arr[mid] < target) {
left = mid + 1; // Adjust the search range to the right half
} else {
right = mid - 1; // Adjust the search range to the left half
} }
return -1; // Element not found
}// Example usage:
let sortedArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let targetElement = 6;
let result = binarySearch(sortedArray, targetElement);
if (result !== -1) {
console.log(`Element ${targetElement} found at index ${result}.`);
} else {
console.log(`Element ${targetElement} not found in the array.`);
}
ii) Fibonacci search
function fibonacciSearch(arr, x) {
let fibMMinus2 = 0;
let fibMMinus1 = 1;
let fib = fibMMinus1 + fibMMinus2;
// Find the smallest Fibonacci Number greater than or equal to the length of
the array
while (fib < arr.length) {
fibMMinus2 = fibMMinus1;
fibMMinus1 = fib;
fib = fibMMinus1 + fibMMinus2;
} let offset = -1;
while (fib > 1) {
let i = Math.min(offset + fibMMinus2, arr.length - 1);
if (arr[i] < x) {
fib = fibMMinus1;
fibMMinus1 = fibMMinus2;
fibMMinus2 = fib - fibMMinus1;
offset = i;
} else if (arr[i] > x) {
fib = fibMMinus2;
fibMMinus1 -= fibMMinus2;
fibMMinus2 = fib - fibMMinus1;
} else {
return i; // Element found
} } // Compare the last element with x
if (fibMMinus1 && arr[offset + 1] === x) {
return offset + 1;
} return -1; // Element not found
}// Example usage:
let sortedArray = [10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100];
let searchElement = 85;
let result = fibonacciSearch(sortedArray, searchElement);
if (result !== -1) {
console.log(`${searchElement} found at index ${result}`);
} else {
console.log(`${searchElement} not found in the array`);
}
Q8 Write a C program to implement the sorting algorithms.
bubbleSort
function bubbleSort(arr) {
const n = arr.length;
for (let i = 0; i < n - 1; i++) {
for (let j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
} } }
return arr;
}// Example usage:
let arrayToSort = [64, 34, 25, 12, 22, 11, 90];
let sortedArrayBubbleSort = bubbleSort(arrayToSort.slice());
console.log("Bubble Sort Result:", sortedArrayBubbleSort);
mergeSort
function mergeSort(arr) {
if (arr.length <= 1) {
return arr;
}
const mid = Math.floor(arr.length / 2);
const left = mergeSort(arr.slice(0, mid));
const right = mergeSort(arr.slice(mid));
return merge(left, right);
}function merge(left, right) {
let result = [];
let i = 0;
let j = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
result.push(left[i]);
i++;
} else {
result.push(right[j]);
j++;
} } return result.concat(left.slice(i)).concat(right.slice(j));
}// Example usage:
let arrayToSortMergeSort = [64, 34, 25, 12, 22, 11, 90];
let sortedArrayMergeSort = mergeSort(arrayToSortMergeSort.slice());
console.log("Merge Sort Result:", sortedArrayMergeSort);