Skip to content

Stacks & Queues Assignment 2 #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
91 changes: 91 additions & 0 deletions JayKay24/Stacks and Queues 2/03-stacks_02.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Stack } from "./stack";

/**
* Reads in a string and reverses it using a Stack push
* and pop operations
* @param str - string to reverse
* @returns The reverse of str
*/
function reverseString(str: string): string {
const stack = new Stack<string>("");
let reversedStr = "";

for (const char of str) {
stack.push(char);
}

while (!stack.empty) {
reversedStr += stack.peek();
stack.pop();
}

return reversedStr;
}

/**
* Returns "YES" if brackets is balanced, "NO" if unbalanced
* @param brackets - a sequence of brackets
* @param "YES" if brackets is balanced, "NO" if unbalanced
*/
function balancedBrackets(brackets: string): string {
const enum balancedState {
BALANCED = "YES",
UNBALANCED = "NO",
}

const stack = new Stack<string>("");

const brackedPairs: Map<string, string> = new Map([
["(", ")"],
["{", "}"],
["[", "]"],
]);

for (const char of brackets) {
if (brackedPairs.has(char)) {
stack.push(char);
} else {
if (brackedPairs.get(stack.peek()) !== char) {
return balancedState.UNBALANCED;
}

stack.pop();
}
}

return stack.empty ? balancedState.BALANCED : balancedState.UNBALANCED;
}

/**
* A Stack with overflow logic implemented
*/
class OverFlow {
private nums: number[];
private defaultValue = Infinity;
private top = 0;

constructor(initialSize = 5) {
this.nums = new Array(initialSize).fill(this.defaultValue);
}

private stackIsFull() {
return this.top >= this.nums.length;
}

get size() {
return this.nums.length;
}

push(val: number): void {
if (this.stackIsFull()) {
console.log("Stack overflow");

// reduce printing out "Stack overflow" by doubling the size of full stack
const newArr = new Array(this.size * 2).fill(this.defaultValue);

this.nums = [...this.nums, ...newArr];
}

this.nums[this.top++] = val;
}
}
28 changes: 28 additions & 0 deletions JayKay24/Stacks and Queues 2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Q1: Implement Queue using Stacks

Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).

Implement the MyQueue class:

* void push(int x) Pushes element x to the back of the queue.

* int pop() Removes the element from the front of the queue and returns it.

* int peek() Returns the element at the front of the queue.

* boolean empty() Returns true if the queue is empty, false otherwise.


# Q2: Implement Stack using Queues

Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).

Implement the MyStack class:

* void push(int x) Pushes element x to the top of the stack.

* int pop() Removes the element on the top of the stack and returns it.

* int top() Returns the element on the top of the stack.

* boolean empty() Returns true if the stack is empty, false otherwise.
80 changes: 80 additions & 0 deletions JayKay24/Stacks and Queues 2/queue-using-stacks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Stack } from "./stack";

export class MyQueue {
private pushStack: Stack<number>;
private popStack: Stack<number>;
private defaultValue = -Infinity;

constructor() {
this.pushStack = new Stack<number>(this.defaultValue);
this.popStack = new Stack<number>(this.defaultValue);
}

/**
* Pushes element x to the back of the queue
*
* @param x - integer to add to the queue
*/
push(x: number): void {
this.pushStack.push(x);
}

/**
* Removes the element from the front of the queue
*
* @returns The popped element
*/
pop(): number {
let retVal: number = this.defaultValue;

if (!this.popStack.empty) {
retVal = this.popStack.peek();
this.popStack.pop();
} else {
this.populatePopStack();

retVal = this.popStack.peek();
this.popStack.pop();
}

return retVal === this.defaultValue ? this.defaultValue : retVal;
}

/**
* Returns the element at the front of the queue
*
* @returns The next item waiting to be popped
*/
peek(): number {
let retVal: number = this.defaultValue;

if (!this.popStack.empty) {
retVal = this.popStack.peek();
} else {
this.populatePopStack();

retVal = this.popStack.peek();
}

return retVal === this.defaultValue ? this.defaultValue : retVal;
}

/**
* Returns true if the queue is empty, false otherwise
*
*/
empty(): boolean {
return this.popStack.empty && this.pushStack.empty;
}

/**
* Drain the pushStack into the popStack
*/
private populatePopStack() {
while (!this.pushStack.empty) {
const nextVal = this.pushStack.peek();
this.pushStack.pop();
this.popStack.push(nextVal);
}
}
}
90 changes: 90 additions & 0 deletions JayKay24/Stacks and Queues 2/stack-using-queues.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { MyQueue as Queue } from "./queue-using-stacks";

class MyStack {
private pushQueue: Queue;
private helperQueue: Queue;
private defaultValue = -Infinity;

constructor() {
this.pushQueue = new Queue();
this.helperQueue = new Queue();
}

/**
* Pushes element x to the top of the stack
*
* @param x - Integer to push onto the stack
*/
push(x: number): void {
this.pushQueue.push(x);
}

/**
* Removes the element on the top of the stack and returns it
*
* @returns The element just popped from the stack
*/
pop(): number {
let lastQueueItem = this.drainPushQueue();

this.drainHelperQueue();

return lastQueueItem === this.defaultValue
? this.defaultValue
: lastQueueItem;
}

/**
* Returns the element on the top of the stack
*
* @returns The integer on top of the stack
*/
top(): number {
let lastQueueItem = this.drainPushQueue();

this.drainHelperQueue();

if (lastQueueItem !== this.defaultValue) {
this.push(lastQueueItem);
}

return lastQueueItem === this.defaultValue
? this.defaultValue
: lastQueueItem;
}

/**
* Returns true if the stack is empty, false otherwise
*
* @returns Boolean indicating whether the stack is empty or not
*/
empty(): boolean {
return this.pushQueue.empty() && this.helperQueue.empty();
}

/**
* Drain the helperQueue elements back into the pushQueue
*/
private drainHelperQueue(): void {
while (!this.helperQueue.empty()) {
this.pushQueue.push(this.helperQueue.pop());
}
}

/**
* Drain the pushqueue elements into the helperQueue
*/
private drainPushQueue(): number {
let lastQueueItem = this.defaultValue;

while (!this.pushQueue.empty()) {
if (lastQueueItem !== this.defaultValue) {
this.helperQueue.push(lastQueueItem);
}

lastQueueItem = this.pushQueue.pop();
}

return lastQueueItem;
}
}
62 changes: 62 additions & 0 deletions JayKay24/Stacks and Queues 2/stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
export class Stack<T> {
private elements: T[];
private defaultValue: T;

constructor(defaultVal: T) {
this.elements = [];
this.defaultValue = defaultVal;
}

/**
* Returns boolean representing if the stack is empty or not
*
* @returns boolean representing if size is equal to zero
*/
get empty(): boolean {
return this.size === 0;
}

/**
* Returns the size of the elements array
*
* @returns integer representing size of the elements array
*/
get size(): number {
return this.elements.length;
}

/**
* Returns the last added element without removing it from the stack.
*
* @returns the last added element
*/
peek(): T {
if (this.empty) {
return this.defaultValue;
}

return this.elements[this.lastIdx];
}

/**
* Pushes the element x onto the stack
*
* @param x - The next element to add to the stack
*/
push(x: T): void {
this.elements.push(x);
}

/**
* Removes the top element of the stack
*
*/
pop(): void {
if (this.size > 0) this.elements.pop();
}

private get lastIdx(): number {
if (this.size === 0) return -1;
return this.size - 1;
}
}