Explore Data Structure and Algorithm Part-4

LIFO Principle of Stack

Basic Operations of Stack

# Stack in python
# Creating a stack
def create_stack():
stack = []
return stack

# Creating an empty stack
def check_empty(stack):
return len(stack) == 0

# Adding items into the stack
def push(stack, item):
print("pushed item: " + item)

# Removing an element from the stack
def pop(stack):
if (check_empty(stack)):
return "stack is empty"

return stack.pop()

stack = create_stack()
push(stack, str(1))
push(stack, str(2))
push(stack, str(3))
push(stack, str(4))
print("popped item: " + pop(stack))
print("stack after popping an element: " + str(stack))

Applications of Stack Data Structure

Queue Data Structure

Basic Operations of Queue

Working of Queue

Enqueue Operation

Dequeue Operation

# Queue in Python
class Queue:
def __init__(self):
self.queue = []

# Add an element
def enqueue(self, item):

# Remove an element
def dequeue(self):
if len(self.queue) < 1:
return None
return self.queue.pop(0)

# Display the queue
def display(self):

def size(self):
return len(self.queue)

q = Queue()
print("After removing an element")

Applications of Queue

There are four different types of queues:

Heap Data Structure

Max heap
Min Heap

Heap Operations

If there is no node, 
create a newNode.
else (a node is already present)
insert the newNode at the end (last node from left to right.)

heapify the array

Delete Element from Heap

If nodeToBeDeleted is the leafNode
remove the node
Else swap nodeToBeDeleted with the lastLeafNode
remove noteToBeDeleted

heapify the array

Peek (Find max/min)

Heap Data Structure Applications



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store