A stack is a linear data structure in which the insertion of a new element and removal of an existing element takes place at the same end represented as the top of the stack.
This strategy states that the element that is inserted last will come out first. You can take a pile of plates kept on top of each other as a real-life example. The plate which we put last is on the top and since we remove the plate that is at the top, we can say that the plate that was put last comes out first.
In order to make manipulations in a stack, there are certain operations provided to us.
push()
: to insert an element into the stackpop()
: to remove an element from the stacktop() or peek()
: Returns the top element of the stack.isEmpty()
: returns true if the stack is empty else false.isFull()
: returns true if the stack is full.size()
: returns the size of the stack.
Problem Description The program creates a stack and allows the user to perform push and pop operations on it.
Problem Solution
- Create a class Stack with instance variable items initialized to an empty list.
- Define methods push, pop and is_empty inside the class Stack.
- The method push appends data to items.
- The method pop pops the first element in items.
- The method is_empty returns True only if items is empty.
- Create an instance of Stack and present a menu to the user to perform operations on the stack.
Program/Source Code Here is the source code of a Python program to implement a stack. The program output is shown below.
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def push(self, data):
self.items.append(data)
def pop(self):
return self.items.pop()
s = Stack()
while True:
print('push <value>')
print('pop')
print('quit')
do = input('What would you like to do? ').split()
operation = do[0].strip().lower()
if operation == 'push':
s.push(int(do[1]))
elif operation == 'pop':
if s.is_empty():
print('Stack is empty.')
else:
print('Popped value: ', s.pop())
elif operation == 'quit':
break
Program Explanation
- An instance of Stack is created.
- The user is presented with a menu to perform push and pop operations on the stack.
- The chosen operation is performed by calling the corresponding method of the stack.
Case 1:
push <value>
pop
quit
What would you like to do? push 3
push <value>
pop
quit
What would you like to do? push 5
push <value>
pop
quit
What would you like to do? pop
Popped value: 5
push <value>
pop
quit
What would you like to do? pop
Popped value: 3
push <value>
pop
quit
What would you like to do? pop
Stack is empty.
push <value>
pop
quit
What would you like to do? quit
Case 2:
push <value>
pop
quit
What would you like to do? pop
Stack is empty.
push <value>
pop
quit
What would you like to do? push 1
push <value>
pop
quit
What would you like to do? pop
Popped value: 1
push <value>
pop
quit
What would you like to do? quit
Problem Description The program creates a stack using a single queue and allows the user to perform push and pop operations on it.
Problem Solution
- Create a class Queue.
- Define methods enqueue, dequeue, is_empty and get_size inside the class Queue.
- Create a class Stack with instance variable q initialized to an empty queue.
- Pushing is done by enqueuing data to the queue.
- To pop, the queue is dequeued and enqueued with the dequeued element n – 1 times where n is the size of the queue. This causes the the last element added to the queue to reach the rear of the queue. The queue is dequeued one more time to get the item to be returned.
- The method is_empty returns True iff the queue is empty.
Program/Source Code Here is the source code of a Python program to implement a stack using a single queue. The program output is shown below.
class Stack:
def __init__(self):
self.q = Queue()
def is_empty(self):
return self.q.is_empty()
def push(self, data):
self.q.enqueue(data)
def pop(self):
for _ in range(self.q.get_size() - 1):
dequeued = self.q.dequeue()
self.q.enqueue(dequeued)
return self.q.dequeue()
class Queue:
def __init__(self):
self.items = []
self.size = 0
def is_empty(self):
return self.items == []
def enqueue(self, data):
self.size += 1
self.items.append(data)
def dequeue(self):
self.size -= 1
return self.items.pop(0)
def get_size(self):
return self.size
s = Stack()
print('Menu')
print('push <value>')
print('pop')
print('quit')
while True:
do = input('What would you like to do? ').split()
operation = do[0].strip().lower()
if operation == 'push':
s.push(int(do[1]))
elif operation == 'pop':
if s.is_empty():
print('Stack is empty.')
else:
print('Popped value: ', s.pop())
elif operation == 'quit':
break
Program Explanation
- An instance of Stack is created.
- The user is presented with a menu to perform push and pop operations on the stack.
- The chosen operation is performed by calling the corresponding method of the stack.
Case 1:
Menu
push <value>
pop
quit
What would you like to do? push 3
What would you like to do? push 5
What would you like to do? pop
Popped value: 5
What would you like to do? pop
Popped value: 3
What would you like to do? pop
Stack is empty.
Case 2:
Menu
push <value>
pop
quit
What would you like to do? push 1
What would you like to do? push 2
What would you like to do? push 3
What would you like to do? push 4
What would you like to do? pop
Popped value: 4
What would you like to do? pop
Popped value: 3
What would you like to do? pop
Popped value: 2
What would you like to do? pop
Popped value: 1
What would you like to do? pop
Stack is empty.
What would you like to do? quit
Problem Description The program creates a stack using two queues and allows the user to perform push and pop operations on it.
Problem Solution
- Create a class Queue with instance variable items initialized to an empty list.
- Define methods enqueue, dequeue and is_empty inside the class Queue.
- The method enqueue appends data to items.
- The method dequeue dequeues the first element in items.
- The method is_empty returns True only if items is empty.
- Create a class Stack with instance variable queue1 and queue2 initialized to empty queues.
- Define methods push, pop and is_empty inside the class Stack.
- The method push takes an argument and enqueues it in queue1. Then every element of queue2 is dequeued and enqueued in queue1. The names of queue1 and queue2 are then switched.
- The method pop dequeues from queue2 and returns the dequeued value.
- The method is_empty returns True only if queue2 is empty.
Program/Source Code Here is the source code of a Python program to implement a stack using two queues. The program output is shown below.
class Stack:
def __init__(self):
self.queue1 = Queue()
self.queue2 = Queue()
def is_empty(self):
return self.queue2.is_empty()
def push(self, data):
self.queue1.enqueue(data)
while not self.queue2.is_empty():
x = self.queue2.dequeue()
self.queue1.enqueue(x)
self.queue1, self.queue2 = self.queue2, self.queue1
def pop(self):
return self.queue2.dequeue()
class Queue:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def enqueue(self, data):
self.items.append(data)
def dequeue(self):
return self.items.pop(0)
s = Stack()
print('Menu')
print('push <value>')
print('pop')
print('quit')
while True:
do = input('What would you like to do? ').split()
operation = do[0].strip().lower()
if operation == 'push':
s.push(int(do[1]))
elif operation == 'pop':
if s.is_empty():
print('Stack is empty.')
else:
print('Popped value: ', s.pop())
elif operation == 'quit':
break
Program Explanation
- An instance of Stack is created.
- The user is presented with a menu to perform push and pop operations on the stack.
- The chosen operation is performed by calling the corresponding method of the stack.
Case 1:
Menu
push <value>
pop
quit
What would you like to do? pop
Stack is empty.
What would you like to do? push 3
What would you like to do? push 4
What would you like to do? pop
Popped value: 4
What would you like to do? pop
Popped value: 3
What would you like to do? pop
Stack is empty.
What would you like to do? push 1
What would you like to do? push 2
What would you like to do? pop
Popped value: 2
What would you like to do? quit
Case 2:
Menu
push <value>
pop
quit
What would you like to do? push 1
What would you like to do? push 2
What would you like to do? push 5
What would you like to do? push 0
What would you like to do? pop
Popped value: 0
What would you like to do? pop
Popped value: 5
What would you like to do? pop
Popped value: 2
What would you like to do? pop
Popped value: 1
What would you like to do? pop
Stack is empty.
What would you like to do? quit
Problem Description The program creates a stack and allows the user to perform push and pop operations on it.
Problem Solution
- Create a class Stack with instance variable items initialized to an empty list.
- Define methods push, pop, is_empty and display inside the class Stack.
- The method push appends data to items.
- The method pop pops the first element in items.
- The method is_empty returns True only if items is empty.
- The method display prints the elements of the stack from top to bottom.
- Define function insert_at_bottom which takes a stack and a data item as arguments.
- The function insert_at_bottom adds the data item to the bottom of the stack using recursion.
- Define function reverse_stack which takes a stack as argument.
- The function reverse_stack reverses the stack using recursion.
- Create an instance of Stack, push data to it and reverse the stack.
Program/Source Code Here is the source code of a Python program to reverse a stack. The program output is shown below.
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def push(self, data):
self.items.append(data)
def pop(self):
return self.items.pop()
def display(self):
for data in reversed(self.items):
print(data)
def insert_at_bottom(s, data):
if s.is_empty():
s.push(data)
else:
popped = s.pop()
insert_at_bottom(s, data)
s.push(popped)
def reverse_stack(s):
if not s.is_empty():
popped = s.pop()
reverse_stack(s)
insert_at_bottom(s, popped)
s = Stack()
data_list = input('Please enter the elements to push: ').split()
for data in data_list:
s.push(int(data))
print('The stack:')
s.display()
reverse_stack(s)
print('After reversing:')
s.display()
Program Explanation
- An instance of Stack is created.
- The user is prompted to enter data items to push to the stack.
- The stack is displayed.
- The stack is reversed by passing it to the function reverse_stack.
- The stack is displayed again.
Case 1:
Please enter the elements to push: 7 3 1 5
The stack:
5
1
3
7
After reversing:
7
3
1
5
Case 2:
Please enter the elements to push: 3
The stack:
3
After reversing:
3
Case 3:
Please enter the elements to push: 1 2
The stack:
2
1
After reversing:
1
2
Problem Description The program prompts the user for a string and determines whether it is a palindrome with the help of a stack.
Problem Solution
- Create a class Stack with instance variable items initialized to an empty list.
- Define methods push, pop and is_empty inside the class Stack.
- The method push appends data to items.
- The method pop pops the first element in items.
- The method is_empty returns True only if items is empty.
- Prompt the user for a string and push all the characters to a stack.
- Construct the reversed string by popping characters from the stack.
- Deterime whether the string is palindromic by comparing the two strings.
Program/Source Code Here is the source code of a Python program to check whether a string is a palindrome using a stack. The program output is shown below.
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def push(self, data):
self.items.append(data)
def pop(self):
return self.items.pop()
s = Stack()
text = input('Please enter the string: ')
for character in text:
s.push(character)
reversed_text = ''
while not s.is_empty():
reversed_text = reversed_text + s.pop()
if text == reversed_text:
print('The string is a palindrome.')
else:
print('The string is not a palindrome.')
Program Explanation
- An instance of Stack is created.
- The user is prompted to enter a string.
- The characters of the string are pushed to the stack.
- The reversed string is constructed by popping the characters from the stack.
- If the reversed string and the original string are the same, then the string is palindromic.
- The result is displayed.
Case 1:
Please enter the string: madam
The string is a palindrome.
Case 2:
Please enter the string: racecar
The string is a palindrome.
Case 3:
Please enter the string: palace
The string is not a palindrome.
Problem Description The program prompts the user for a string and determines whether it is a palindrome with the help of a stack.
Problem Solution
- Create a class Stack with instance variable items initialized to an empty list.
- Define methods push, pop and is_empty inside the class Stack.
- The method push appends data to items.
- The method pop pops the first element in items.
- The method is_empty returns True only if items is empty.
- Prompt the user for an expression.
- Iterate through the characters of the expression and push to the stack if an open parenthesis is encountered and pop if a close parenthesis is encountered.
- Determine whether the expression has balanced parentheses.
Program/Source Code Here is the source code of a Python program to check for balanced parentheses using a stack. The program output is shown below.
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def push(self, data):
self.items.append(data)
def pop(self):
return self.items.pop()
s = Stack()
exp = input('Please enter the expression: ')
for c in exp:
if c == '(':
s.push(1)
elif c == ')':
if s.is_empty():
is_balanced = False
break
s.pop()
else:
if s.is_empty():
is_balanced = True
else:
is_balanced = False
if is_balanced:
print('Expression is correctly parenthesized.')
else:
print('Expression is not correctly parenthesized.')
Program Explanation
- An instance of Stack is created.
- The user is prompted to enter an expression.
- Iterate through the expression and push to the stack for every open parenthesis and pop for every close parenthesis.
- If a pop is required when the stack is empty or the stack is not empty when the expression has been iterated over, then the expression is not correctly parenthesized.
- Display the result.
Case 1:
Please enter the expression: (3 + 4 * (1 + (2))/(7 * (8 + 9)))
Expression is correctly parenthesized.
Case 2:
Please enter the expression: (a + b))(3)
Expression is not correctly parenthesized.
Case 3:
Please enter the expression: (4 + (3 * 2)
Expression is not correctly parenthesized.