DSA #1 — Stacks, Queues and everything in between.

Preh-She-Us
3 min readDec 1, 2021

Slightly awkward developer trying to build her SE skills here. This post is strictly for accountability and improving my learning. Read at your own risk, lol.

STACKS:

These data structures essentially structure lists in a LIFO way. It’s like stacking a box of cartons on top of each other. The last one you put in, gets to be the first one out.

I’m making use of the python programming language to learn DSAs and in the language, it can be written like so:

#LIFO - last in, first out
#Normal example
my_stack = list()my_stack.append(4)my_stack.append(7)my_stack.append(12)my_stack.append(19)print(my_stack)print(my_stack.pop())print(my_stack.pop())print(my_stack)#Wrapper Class Exampleclass ListStack: #defines the class name#assigns a variable name to the list
def __init__(self):
self._L = []
#tells my computer to append whatever value i input to the list.
def push(self, item): self._L.append(item)#function that says if my list is less than or equal to zero, don't remove anything. If it is more than zero, remove(pop) the last item. def pop(self): if len(self._L) <= 0: return None else: return self._L.pop()
#function that displays the last item in the list, it returns none if the list is empty
def peek(self): if len(self._L) <= 0: return None else: return self._L[-1]
#function returns list length
def __len__(self): return len(self._L)
#function returns true or false depending on if the list is empty or not.
def isempty(self): return len(self) == 0
#function returns a string. (the __str__ is responsible for converting things like [__main__ at _L x00000234] to readable text by humans.)
def __str__(self): return str(self._L)L=ListStack()L.push(3)L.push(5)L.peek()print(L.isempty(), f"There are about {len(L)} inputs.")

The “normal example” is for just writing them plainly, while the “wrapper class” example helps make room for different types of functions to be carried out on the list.

QUEUES [FIFO(First in, first out)]:

If you’ve had to be in line for something, you’ve practised this data structure. It basically means, the first one in, gets to be the first one out. Two python functions that help out with this are the “enqueue” — which essentially means “add to queue” and the “dequeue” — which means “remove from queue”.

#FIFO - First in, first out#a python module that makes the queuing life easierfrom collections import dequemy_queue = deque()my_queue.append(5)my_queue.append(10)print(my_queue)print(my_queue.popleft()) #popleft means pop from the beginning of the list#Example for Wrapper Classclass Queue:#assign a name to my variable   def __init__(self):       self.my_queue = []
#create function that adds item to queue
def enqueue(self, item): return self.my_queue.append(item)
#create function that removes item from queue
def dequeue(self): return self.my_queue.pop()
#create function that gets length of items in queue
def get_size(self): return len(self.my_queue)
#create function that returns output in strings
def __str__(self): return str(self.my_queue)myQ = Queue()myQ.enqueue(7)myQ.enqueue(8)myQ.enqueue(1)myQ.dequeue()print(f"There are {myQ.get_size()} values in the list")print(myQ)

I learnt about MaxHeaps, but they’re still super confusing so I can’t write about them right now. What I do know though, is that they seem super similar to Binary Search Trees.

Ciao!

--

--