Skip to content

Commit

Permalink
add(ts): stack
Browse files Browse the repository at this point in the history
  • Loading branch information
thutasann committed Jan 1, 2025
1 parent 037b7c0 commit 9a3c769
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export interface IStack<T> {
/**
* Push an item onto the stack
* @param element - item to be pushed
*/
push(element: T): void;
/**
* Remove and return the Top Item from the stack
*/
pop(): T | undefined;
/**
* Return the top item from the stack without removing it
*/
peek(): T | undefined;
/**
* Check if the stack is empty
*/
isEmpty(): boolean;
/**
* Return the size of the stack
*/
size(): number;
/**
* Clear the Stack
*/
clear(): void;
/**
* Search for an element and return its position from the top
* @param element - element to be searched
*/
search(element: T): number;
/**
* Print all items in the stack
*/
print(): void;
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './ISinglyLinkedList';
export * from './IStack';
2 changes: 2 additions & 0 deletions java_vs_javascript/ts-java-structures/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SinglyLinkedListImpl } from './linked-list';
import { StackImpl } from './stack';

SinglyLinkedListImpl();
StackImpl();
44 changes: 44 additions & 0 deletions java_vs_javascript/ts-java-structures/src/stack/class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { IStack, logger } from '../core';

export class Stack<T> implements IStack<T> {
private items: T[];

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

push(element: T): void {
this.items.push(element);
}

pop(): T {
if (this.isEmpty()) throw new Error('Stack is empty');
return this.items.pop();
}

peek(): T {
if (this.isEmpty()) throw new Error('Stack is empty');
return this.items[this.items.length - 1];
}

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

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

clear(): void {
this.items = [];
}

search(element: T): number {
const index = this.items.lastIndexOf(element);
return index !== -1 ? this.items.length - index : 1;
}

print(): void {
logger.log(this.items.join(' -> '));
}
}
25 changes: 25 additions & 0 deletions java_vs_javascript/ts-java-structures/src/stack/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { logger } from '../core';
import { Stack } from './class';

export function StackImpl() {
logger.info('Stack');
const numberStack = new Stack<number>();

numberStack.push(10);
numberStack.push(20);
numberStack.push(30);
numberStack.print(); // Output: 10 -> 20 -> 30

console.log('Peek:', numberStack.peek()); // Output: Peek: 30

numberStack.pop();
numberStack.print(); // Output: 10 -> 20

console.log('Size:', numberStack.size()); // Output: Size: 2

console.log('Search 10:', numberStack.search(10)); // Output: Search 10: 2
console.log('Search 50:', numberStack.search(50)); // Output: Search 50: -1

numberStack.clear();
console.log('Is Empty:', numberStack.isEmpty()); // Output: Is Empty: true
}

0 comments on commit 9a3c769

Please sign in to comment.