🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
A Stack is a Last In First Out (LIFO) data structure that allows operations at one end, often referred to as the "top". In this guide, we will demonstrate how to implement a stack using arrays in Kotlin.
2. Program Overview
The stack will support the following operations:
1. Push: To add an element to the top of the stack.
2. Pop: To remove and return the top element of the stack.
3. Peek: To view the top element without removing it.
4. Check if the stack is empty.
We will use a fixed-size array to hold the stack elements and a variable to keep track of the top position in the stack.
3. Code Program
class Stack(private val size: Int) {
private val array = IntArray(size)
private var top = -1 // This will track the top position. Initialized to -1 because the stack is initially empty.
// Push operation
fun push(value: Int) {
if (top < size - 1) {
top++
array[top] = value
} else {
println("Stack Overflow!")
}
}
// Pop operation
fun pop(): Int? {
return if (top >= 0) {
val poppedValue = array[top]
top--
poppedValue
} else {
println("Stack Underflow!")
null
}
}
// Peek operation
fun peek(): Int? {
return if (top >= 0) {
array[top]
} else {
println("Stack is Empty!")
null
}
}
// Check if the stack is empty
fun isEmpty(): Boolean = top == -1
}
fun main() {
val stack = Stack(5) // Stack of size 5
stack.push(10)
stack.push(20)
stack.push(30)
stack.push(40)
stack.push(50)
println("Top element: ${stack.peek()}")
println("Popped element: ${stack.pop()}")
println("Is stack empty? ${stack.isEmpty()}")
}
Output:
Top element: 50 Popped element: 50 Is stack empty? false
4. Step By Step Explanation
1. Class Declaration & Initialization:
- We declare a Stack class that accepts a size (for the fixed-size array).- An array (array) is initialized with the given size.- The top variable is initialized at -1, signifying an empty stack.
2. Push Operation:
- Check if there's space left in the stack.- If so, increment the top and store the value.- If not, print an error message ("Stack Overflow").
3. Pop Operation:
- If the stack isn't empty, return the top element and decrement the top variable.- If the stack is empty, print an error message ("Stack Underflow").
4. Peek Operation:
- Return the top element without popping it.- If the stack is empty, inform the user.
5. isEmpty Function:
- Returns a Boolean indicating if the stack is empty.
The main function demonstrates how to use the stack. The push operation is executed five times, and then the peek and pop operations are used. The program concludes by checking if the stack is empty using the isEmpty function.
With this program, one can understand the foundational principles behind a stack and its standard operations using arrays in Kotlin.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment