📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
The Stack Data Structure Overview
What is a Stack?
Stack Real-World Examples
1. In the below diagram, the first image shows a container is filled with balls we will try to always take a last inserted ball so it is a last in first out. The second image is a list of mail envelopes and most of the peoples will always take the topmost envelope and then second top etc.
Creating a Stack
class Stack {
//properties and methods go here
}
let items = [];
- push(element(s)): This adds a new item (or several items) to the top of the stack.
- pop(): This removes the top item from the stack. It also returns the removed element.
- peek(): This returns the top element from the stack. The stack is not modified (it does not remove the element; it only returns the element for information purposes).
- isEmpty(): This returns true if the stack does not contain any elements, and false if the size of the stack is bigger than 0.
- clear(): This removes all the elements of the stack.
- size(): This returns the number of elements that the stack contains. It is similar to the length property of an array
Pushing elements to the Stack
push(element) {
this.items[this.count] = element;
this.count++;
}
Popping elements from the stack
pop() {
if (this.isEmpty()) {
return undefined;
}
this.count--;
const result = this.items[this.count];
delete this.items[this.count];
return result;
}
Verifying if the stack is empty
isEmpty() {
return this.count === 0;
}
Clearing and printing the elements of the stack
clear() {
this.items = {};
this.count = 0;
}
toString() {
if (this.isEmpty()) {
return '';
}
let objString = `${this.items[0]}`;
for (let i = 1; i < this.count; i++) {
objString = `${objString},${this.items[i]}`;
}
return objString;
}
Complete Example and Output
class Stack {
constructor() {
this.count = 0;
this.items = {};
}
push(element) {
this.items[this.count] = element;
this.count++;
}
pop() {
if (this.isEmpty()) {
return undefined;
}
this.count--;
const result = this.items[this.count];
delete this.items[this.count];
return result;
}
peek() {
if (this.isEmpty()) {
return undefined;
}
return this.items[this.count - 1];
}
isEmpty() {
return this.count === 0;
}
size() {
return this.count;
}
clear() {
/* while (!this.isEmpty()) {
this.pop();
} */
this.items = {};
this.count = 0;
}
toString() {
if (this.isEmpty()) {
return '';
}
let objString = `${this.items[0]}`;
for (let i = 1; i < this.count; i++) {
objString = `${objString},${this.items[i]}`;
}
return objString;
}
}
Using Stack
const stack = new Stack();
console.log('stack.isEmpty() => ', stack.isEmpty()); // outputs true
stack.push(12);
stack.push(13);
console.log('stack after push 12 and 13 => ', stack.toString());
console.log('stack.peek() => ', stack.peek()); // outputs 13
stack.push(14);
console.log('stack.size() after push 11 => ', stack.size()); // outputs 3
console.log('stack.isEmpty() => ', stack.isEmpty()); // outputs false
stack.push(15);
stack.pop();
stack.pop();
console.log('stack.size() after push 15 and pop twice => ', stack.size()); // outputs 2
Output
stack.isEmpty() => true
stack after push 12 and 13 => 12,13
stack.peek() => 13
stack.size() after push 11 => 3
stack.isEmpty() => false
stack.size() after push 15 and pop twice => 2
Check out Stack implementation in Java at https://www.javaguides.net/2018/09/stack-data-structure-in-java.html.
Comments
Post a Comment
Leave Comment