A Stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. Elements can be inserted and removed only from one end, called the top of the stack.
Operation | Time Complexity |
---|---|
Push | O(1) |
Pop | O(1) |
Peek | O(1) |
Search | O(n) |
package main
import "fmt"
// Stack structure
type Stack struct {
items []int
}
// Push operation
func (s *Stack) Push(item int) {
s.items = append(s.items, item)
}
// Pop operation
func (s *Stack) Pop() (int, error) {
if len(s.items) == 0 {
return 0, fmt.Errorf("stack is empty")
}
item := s.items[len(s.items)-1]
s.items = s.items[:len(s.items)-1]
return item, nil
}
// Peek operation
func (s *Stack) Peek() (int, error) {
if len(s.items) == 0 {
return 0, fmt.Errorf("stack is empty")
}
return s.items[len(s.items)-1], nil
}
// IsEmpty operation
func (s *Stack) IsEmpty() bool {
return len(s.items) == 0
}
func main() {
stack := &Stack{}
// Push elements
stack.Push(1)
stack.Push(2)
stack.Push(3)
// Pop and print elements
fmt.Println("Stack operations:")
for !stack.IsEmpty() {
item, _ := stack.Pop()
fmt.Printf("%d ", item)
}
}
public class Stack {
private int maxSize;
private int[] stackArray;
private int top;
// Constructor
public Stack(int size) {
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}
// Push operation
public void push(int value) {
if (top < maxSize - 1) {
stackArray[++top] = value;
} else {
System.out.println("Stack is full");
}
}
// Pop operation
public int pop() {
if (top >= 0) {
return stackArray[top--];
} else {
System.out.println("Stack is empty");
return -1;
}
}
// Peek operation
public int peek() {
if (top >= 0) {
return stackArray[top];
} else {
System.out.println("Stack is empty");
return -1;
}
}
// IsEmpty operation
public boolean isEmpty() {
return (top == -1);
}
public static void main(String[] args) {
Stack stack = new Stack(3);
// Push elements
stack.push(1);
stack.push(2);
stack.push(3);
// Pop and print elements
System.out.println("Stack operations:");
while (!stack.isEmpty()) {
System.out.print(stack.pop() + " ");
}
}
}
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
return None
def peek(self):
if not self.is_empty():
return self.items[-1]
return None
def is_empty(self):
return len(self.items) == 0
def size(self):
return len(self.items)
# Example usage
if __name__ == "__main__":
stack = Stack()
# Push elements
stack.push(1)
stack.push(2)
stack.push(3)
# Pop and print elements
print("Stack operations:")
while not stack.is_empty():
print(stack.pop(), end=" ")
Function Call Management
Expression Evaluation
Undo Operations
Memory Management
Array-based Implementation
Linked List Implementation
When to Use Stacks
Implementation Considerations
Stack Overflow
Stack Underflow
Memory Leaks
Stacks are fundamental data structures used in many algorithms and system implementations. Understanding stack operations and their applications is crucial for solving various programming problems efficiently.