Basics of Stacks
Implementation
Examples
Stacks
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.
Operations on Stacks
Operations on Stacks
Operations on Stacks
: insert x at the top
Operations on Stacks
: insert x at the top
Operations on Stacks
: insert x at the top
: delete the top element
Operations on Stacks
: insert x at the top
: delete the top element
Operations on Stacks
: insert x at the top
: delete the top element
: return the top element
Operations on Stacks
: insert x at the top
: delete the top element
: return the top element
: return no of elements
Not without removing the data.
40
40
40
20
40
20
40
20
8
40
20
8
40
20
8
9
40
20
8
9
40
20
8
9
40
20
8
9
40
20
8
9
40
20
8
40
20
8
40
20
8
40
20
8
Peek at the top
40
20
8
Peek at the top
Read 8
40
20
8
40
20
8
40
20
8
40
20
40
20
40
20
40
20
40
40
40
40
Peek at the top
40
Peek at the top
Read 40
40
40
25
40
25
40
25
Accessing pop() or top() method on an empty stack will lead to an
Error
Accessing pop() or top() method on an empty stack will lead to an
Stack Underflow Error
class Stack {
ArrayList<Integer> l = new ArrayList<>();
void push(int x) {
l.add(x);
}
void pop() {
l.remove(l.size() - 1);
}
int top() {
return l.get(l.size() - 1);
}
int size() {
return l.size();
}
}
class Stack:
l = []
def push(self, x):
self.l.append(x)
def pop(self):
self.l.pop()
def top(self):
return self.l[-1]
def size(self):
return len(self.l)
class Stack {
ArrayList<Integer> l = new ArrayList<>();
void push(int x) {
l.add(x);
}
void pop() {
if (l.size() == 0) {
System.out.println("Stack is empty");
}
l.remove(l.size() - 1);
}
int top() {
if (l.size() == 0) {
System.out.println("Stack is empty");
return -1;
}
return l.get(l.size() - 1);
}
int size() {
return l.size();
}
}
class Stack:
l = []
def push(self, x):
self.l.append(x)
def pop(self):
if len(self.l) == 0:
print("Stack is empty")
self.l.pop()
def top(self):
if len(self.l) == 0:
print("Stack is empty")
return self.l[-1]
def size(self):
return len(self.l)
class Stack {
ArrayList<Integer> l = new ArrayList<>();
void push(int x) {
l.add(x);
}
void pop() throws Exception {
if (l.size() == 0) {
throw new Exception("Stack underflow");
}
l.remove(l.size() - 1);
}
int top() throws Exception {
if (l.size() == 0) {
throw new Exception("Stack underflow");
}
return l.get(l.size() - 1);
}
int size() {
return l.size();
}
}
class Stack:
l = []
def push(self, x):
self.l.append(x)
def pop(self):
if len(self.l) == 0:
raise Exception("Stack underflow")
self.l.pop()
def top(self):
if len(self.l) == 0:
raise Exception("Stack underflow")
return self.l[-1]
def size(self):
return len(self.l)
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
class Node:
data = 0
next = None
def __init__(self, x):
self.data = x
self.next = None
class Stack {
Node head = null;
int s = 0;
void push(int x) {
Node n = new Node(x);
n.next = head;
head = n;
s++;
}
void pop() throws Exception {
if (head == null) {
throw new Exception("Stack underflow error");
}
head = head.next;
s--;
}
int top() throws Exception {
if (head == null) {
throw new Exception("Stack underflow error");
}
return head.data;
}
int size() {
return s;
}
}
class Stack:
head = None
s = 0
def push(self, x):
n = Node(x)
n.next = self.head
self.head = n
self.s += 1
def pop(self):
if self.head is None:
raise Exception("Stack underflow error")
self.head = self.head.next
self.s -= 1
def top(self):
if self.head is None:
raise Exception("Stack underflow error")
return self.head.data
def size(self):
return self.s
There are ready-made implementations of Stack in most language's libraries/modules. You can find them here :
Stacks in Java - https://www.scaler.com/topics/stack-class-in-java/
Stacks in C++ - https://www.scaler.com/topics/cpp/stack-in-cpp/