-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
java_vs_javascript/ts-java-structures/src/core/interfaces/IStack.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
1 change: 1 addition & 0 deletions
1
java_vs_javascript/ts-java-structures/src/core/interfaces/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './ISinglyLinkedList'; | ||
export * from './IStack'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(' -> ')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |