Professional Documents
Culture Documents
<!DOCTYPE html>
<html>
<script>
function stackUsingLL()
{
let Node = function (elm) {
this.element = elm;
this.next = null;
}
let length = 0;
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;
}
stack.push(44);
stack.push(55);
document.write("<br>Pop: <br>");
document.write(stack.pop());
</script>
</html>
Output:
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala
<!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
<!DOCTYPE html>
<html>
<script>
function areBracketsBalanced(expr)
{
let stack = [];
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
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
<!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]);
}
}
top2.stackpush(100);
top2.stackpush(200);
top2.stackpush(300);
top2.stackpush(400);
top2.stackpush(500);
top2.stackpush(600);
</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
<!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
</html>
Output:
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala
<!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) {
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
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.");
}
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
<!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");
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
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);
</script>
</html>
Output:
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala
<!DOCTYPE html>
<html>
<script>
class QueueElement {
constructor (element, priority) {
this.element = element;
this.priority = priority;
}
}
class PriorityQueue {
constructor () {
this.items = [];
}
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}`);
}
}
}
</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
<!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());
}
}
</script>
</html>
FYMCA DS Practical No: 03 Date: 25/02/2022
Div: B Abdullah Shethwala
Output: