You are on page 1of 22

FYMCA DS Practical No: 03 Date: 25/02/2022

Div: B Abdullah Shethwala

1. Program to implement Stack using Linked List.

<!DOCTYPE html>
<html>
<script>

function stackUsingLL()
{
let Node = function (elm) {
this.element = elm;
this.next = null;
}

let length = 0;

let head = null;

this.push = function (elm)


{
let node = new Node(elm),
current;

current = head;
node.next = current;
head = node;

length++;
}

this.pop = function ()
{
let current = head;

if (current) {
let elm = current.element;
current = current.next;
head = current;
length--;
return elm;
}

return null;
}
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

this.peek = function ()
{
if (head) {
return head.element;
}

return null;
}

this.toArray = function ()
{
let arr = [];
let current = head;
while (current) {
arr.push(current.element);
current = current.next;
}

return arr;
}

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

this.size = function ()
{
return length;
}

this.clear = function ()
{
head = null;
length = 0;
}

let stack = new stackUsingLL();


document.write("Checking whether the stack is empty or not: <br>");
document.write(stack.isEmpty());
document.write("<br>Inserting elements into the Stack: <br>");
stack.push(11);
stack.push(22);
stack.push(33);
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

stack.push(44);
stack.push(55);

document.write("Displaying the elements of the stack: <br>");


document.write(stack.toArray());
document.write("<br>Peek: <br>");
document.write(stack.peek());

document.write("<br>Checking the size of the stack: <br>")


document.write(stack.size());

document.write("<br>Pop: <br>");
document.write(stack.pop());

document.write("<br>Clearing the stack: <br>")


stack.clear();

document.write("<br>Checking whether the stack is Empty: <br>");


document.write(stack.isEmpty());

</script>

</html>

Output:
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

2. Program to reverse string using Stack.

<!Doctype html>
<html>
<body>
<p> <script>
function reverse(str)
{
let stack = [];
for(let i=0; i<str.length; i++)
{
stack.push(str[i]);
}
let reverseStr = '';
while (stack.length > 0)
{
reverseStr += stack.pop();
}
return reverseStr;
}
document.writeln(reverse(' Abdullah '));
console.log((reverse)(" Sheth "));
</script>
</body>
</html>

Output:
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

3. Program to check balanced parenthesis by using Stacks.

<!DOCTYPE html>
<html>
<script>

function areBracketsBalanced(expr)
{
let stack = [];

for(let i = 0; i < expr.length; i++)


{
let x = expr[i];

if (x == '(' || x == '[' || x == '{')


{

stack.push(x);
continue;
}

if (stack.length == 0)
return false;

let check;
switch (x){
case ')':
check = stack.pop();
if (check == '{' || check == '[')
return false;
break;

case '}':
check = stack.pop();
if (check == '(' || check == '[')
return false;
break;

case ']':
check = stack.pop();
if (check == '(' || check == '{')
return false;
break;
}
}
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

return (stack.length == 0);


}

let expr = "([{(()}])";

if (areBracketsBalanced(expr))
document.write("Balanced ");
else
document.write("Not Balanced ");

</script>
</html>

Output:
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

4. Program to implement Stack using Array.

<!DOCTYPE html>
<html>
<script>

class Stack
{
constructor ()
{
this.data = [];
this.top = 0;
}

stackpush(element)
{
this.data[this.top] = element;
this.top = this.top + 1;
}

stacklength()
{
return this.top;
}

peek()
{
return this.data[this.top - 1];
}

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

stackpop()
{
if (this.isEmpty() == false) {
this.top = this.top - 1;
return this.data.pop();
}
}

print()
{
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

var t = this.top - 1;
while (t >= 0) {
document.write(this.data[t]);
t--;
}
}

reverse()
{
this.rev(this.top - 1);
}

rev(index)
{
if (index != 0) {
this.rev(index - 1);
}
document.write(this.data[index]);
}
}

const top2 = new Stack();


document.write("Checking whether the Stack is empty or not: <br>");
document.write(top2.isEmpty());

top2.stackpush(100);
top2.stackpush(200);
top2.stackpush(300);
top2.stackpush(400);
top2.stackpush(500);
top2.stackpush(600);

document.write("<br>Printing the elements of the Stack: <br>");


top2.print();
document.write("<br>Popping an element from the Stack: ");
top2.stackpop();
document.write("<br>Reversing the Stack: <br>");
top2.reverse();

</script>

</html>
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

Output:
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

5. Program to implement Queue using Array.

<!DOCTYPE html>
<html>
<script>
class Queue
{
constructor ()
{
this.items = [];
}

enqueue(element)
{
return this.items.push(element);
}

dequeue()
{
if (this.items.length > 0) {
return this.items.shift();
}
}

peek()
{
return this.items[this.items.length - 1];
}

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

size()
{
return this.items.length;
}

clear()
{
this.items = [];
}
}
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

let queue = new Queue();


document.write("Checking whether the Queue is empty or not: <br>");
document.write(queue.isEmpty());
document.write("<br>Adding elements into the Queue and printing it:
<br>");
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(4);
queue.enqueue(8);
document.write(queue.items);

document.write("<br>Removing an element form the Queue and printing the


Queue: <br>")
queue.dequeue();
document.write(queue.items);

document.write("<br>Getting the size of the Queue: <br>");


document.write(queue.size());

document.write("<br>Clearing the Queue and checking if the Queue is Empty


or not: <br>")
queue.clear();
document.write(queue.isEmpty());
</script>

</html>

Output:
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

6. Program to implement Queue using Linked List.

<!DOCTYPE html>
<html>

<head>
<script>
class Node {
constructor(value) {
this.value = value
this.next = null
}
}
class queue {
constructor() {
this.first = null
this.last = null
this.size = 0

}
isEmpty() {
return !this.size
}
enqueue(item) {

const newNode = new Node(item)

if (this.isEmpty()) {
this.first = newNode
this.last = newNode
}
else {
this.last.next = newNode
this.last = newNode
}
this.size++
return this
}

dequeue() {

if (this.isEmpty())
return null;
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

const itemToBeRemoved = this.first

if (this.first === this.last) {


this.last = null
}
this.first = this.first.next
this.size--
return itemToBeRemoved
}

peek() {
return this.first
}

display() {
var temp = this.first
while (temp != null) {
console.log(temp.value)
temp = temp.next
}
if (this.first == null)
console.log("Queue is empty.");
}

const que = new queue()


console.log("Checking whether Queue is empty or not: ")
que.isEmpty();
que.display();
console.log("Inserting elements into the Queue and displaying it: ");
que.enqueue(111);
que.enqueue(222);
que.enqueue(333);
que.enqueue(444);

que.display();
console.log("Deleting 1st element of the Queue: ")
que.dequeue();
que.display();
console.log("Deleting 2nd element of the Queue: ");
que.dequeue();
que.display();
console.log("Inserting a new element to the Queue: ")
que.enqueue('Abdullah');
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

que.display();
que.dequeue();
que.dequeue();
que.dequeue();
console.log("After removing all elements from the Queue: ")
que.display();

</script>
</head>

</html>

Output:
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

7. Program to implement Circular Queue.

<!DOCTYPE html>
<html>
<script>

class CircularQueue
{
constructor (size)
{
this.data = [];
this.size = size;
this.length = 0;
this.front = 0;
this.rear = -1;
}

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

enqueue(element)
{
if (this.length >= this.size)
console.log("full");

this.data[(this.rear + 1) % this.size] = element;


this.length++;
}

print()
{
for (let i = 0; i < this.data.length; i++)
{
console.log(this.data[i] + " ");
}
}

getfront() {
if (this.isEmpty())
{
console.log("No element in the circular queue!!");
}
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

return this.data[(this.front) % (this.size)];


}

dequeue()
{
if (this.isEmpty())
console.log("No element found!!");
const value = this.getfront();
this.data[this.front % this.size] = null;
this.front++;
this.length--;
console.log(value);
}
}
const cq = new CircularQueue();
console.log("Checking if the Queue is Empty or not: " + cq.isEmpty());
console.log("Adding elements into the Queue: ");
cq.enqueue(10);
cq.enqueue(15);
cq.enqueue(16);
cq.enqueue(17);
cq.enqueue(18);

console.log("Deleting an element: ");


cq.dequeue();

</script>

</html>

Output:
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

8. Program to implement Priority Queue.

<!DOCTYPE html>
<html>
<script>
class QueueElement {
constructor (element, priority) {
this.element = element;
this.priority = priority;
}
}

class PriorityQueue {
constructor () {
this.items = [];
}

enqueue = function (element, priority) {


let queueElement = new QueueElement(element, priority);

let added = false;


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

if (queueElement.priority > this.items[i].priority) {


this.items.splice(i, 0, queueElement);

added = true;
break;
}
}

if (!added) {
this.items.push(queueElement);
}
}

dequeue = function () {
return this.items.shift();
}

front = function () {
return this.items[0];
}
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

rear = function () {
return this.items[this.items.length - 1];
}

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

size = function () {
return this.items.length;
}

print = function () {
for (let i = 0; i < this.items.length; i++) {
console.log(`${this.items[i].element} -
${this.items[i].priority}`);
}
}
}

let pQ = new PriorityQueue();


console.log("Checking if the Queue is Empty or not: ");
console.log(pQ.isEmpty());
pQ.enqueue(1, 3);
pQ.enqueue(5, 2);
pQ.enqueue(6, 1);
pQ.enqueue(11, 1);
pQ.enqueue(13, 1);
pQ.enqueue(10, 3);
console.log("Adding elements in the Queue and displaying it: ")
pQ.print();
console.log("Deleting an element from the Queue and displaying it: ")
pQ.dequeue();
pQ.print();

</script>

</html>
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

Output:
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

9. Program to reverse Stack using Queue.

<!DOCTYPE html>
<html>
<script>

class Stack
{
constructor()
{
this.elements = [];
}

push(element)
{
this.elements.push(element);
}

pop()
{
if (this.isEmpty()) return "Underflow situation";
else return this.elements.pop();
}

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

print()
{
return this.elements;
}

class Queue
{
constructor ()
{
this.elements = [];
}

enqueue(element)
{
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

this.elements.push(element)
}

dequeue()
{
if (!this.isEmpty()) {
return this.elements.shift();
} else {
return 'Underflow situation';
}
}

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

function reverse(stack)
{
const queue = new Queue();
while (!stack.isEmpty())
{
queue.enqueue(stack.pop());
}

while (!queue.isEmpty())
{
stack.push(queue.dequeue());
}
}

const stack = new Stack();


stack.push('Harry');
stack.push('Ron');
stack.push('Hermoine');
document.write('<br>Printing stack before reversal: ', stack.print());
reverse(stack);
document.write('<br>Printing stack after reversal: ', stack.print());

</script>

</html>
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala

Output:

You might also like