Skip to content

The Stack Data Structure implement #17

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

Merged
merged 1 commit into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions javascript-today-magazine/stack-data-structure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* The Stack Data Structure
* A stack is a linear data structure that follows the principle of Last In First Out (LIFO).
* This means the last element inserted inside the stack is removed first. 🔥🔥🔥
* You can think of the stack data structure as the pile of plates on top of another.
* Here's a JavaScript implementation of a stack data structure 👏👏👏
*/
var Stack = /** @class */ (function () {
// private totalItems: number;
function Stack() {
this.data = [];
this.data = [];
}
/**
* push
*/
Stack.prototype.push = function (record) {
this.data.push(record);
};
/**
* pop
*/
Stack.prototype.pop = function () {
return this.data.pop();
};
/**
* peek
*/
Stack.prototype.peek = function () {
return this.data[this.data.length - 1];
};
return Stack;
}());
var instanceOfStack = new Stack();
instanceOfStack.push(1);
instanceOfStack.push(2);
instanceOfStack.push(12);
instanceOfStack.push(14);
instanceOfStack.push(3);
console.log(instanceOfStack);
console.log(instanceOfStack.peek());
instanceOfStack.pop();
console.log(instanceOfStack);
46 changes: 46 additions & 0 deletions javascript-today-magazine/stack-data-structure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* The Stack Data Structure
* A stack is a linear data structure that follows the principle of Last In First Out (LIFO).
* This means the last element inserted inside the stack is removed first. 🔥🔥🔥
* You can think of the stack data structure as the pile of plates on top of another.
* Here's a JavaScript implementation of a stack data structure 👏👏👏
*/

class Stack {
private data: (number | string | undefined)[] = [];
// private totalItems: number;
constructor() {
this.data = [];
}

/**
* push
*/
public push(record: number | string) {
this.data.push(record);
}
/**
* pop
*/
public pop() {
return this.data.pop()
}

/**
* peek
*/
public peek() {
return this.data[this.data.length - 1];
}
}

const instanceOfStack = new Stack();
instanceOfStack.push(1);
instanceOfStack.push(2);
instanceOfStack.push(12);
instanceOfStack.push(14);
instanceOfStack.push(3);
console.log(instanceOfStack);
console.log(instanceOfStack.peek());
instanceOfStack.pop();
console.log(instanceOfStack);