Data Structures in Python: Custom Stack

Before going ahead with this tutorial if you don’t know what stack is and how we can use a list as a stack you must go through this lecture.

In today’s tutorial, I will be creating a stack class and creating methods to append and pop from Stack.

Add value to Stack

First of all, let’s create a Stack class with a stack attribute in the constructor:

class Stack:
def __init__(self):
self.stack = []

Now we will create an add method to add values in this stack:

class Stack:
def __init__(self):
self.stack = []

def add(self, dataval):
if dataval not in self.stack:
self.stack.append(dataval)
return True
else:
return False

In the above code, we create an add method and first check if the value already exists and if it doesn’t then append the value to the stack.

Pop Value from Stack

Now let’s create a remove method to pop elements from the stack:

class Stack:
def __init__(self):
self.stack = []

def add(self, dataval):
if dataval not in self.stack:
self.stack.append(dataval)
return True
else:
return False
def remove(self):
if len(self.stack) <= 0:
return ("No element in the Stack")
else:
return self.stack.pop()

Here we first checked if an element exists in the stack and if not then show the message otherwise pop the element.

That’s all for this tutorial! Bye-bye!!

Lectures

Lecture 1: https://medium.com/@saifmdco/data-structures-with-python-introduction-4dadeffa2215

Lecture 2: https://medium.com/@saifmdco/data-structures-with-python-arrays-c498518bf7fd

Lecture 3: https://medium.com/@saifmdco/data-structures-in-python-lists-653a3ad103ab

Lecture 4: https://medium.com/@saifmdco/data-structures-in-python-tuples-3c350640cd9a

Lecture 5: https://medium.com/@saifmdco/data-structures-in-python-dictionary-fad27ffdda8b

Lecture 6: https://medium.com/@saifmdco/data-structures-in-python-2d-array-6bc0154aa717

Lecture 7: https://medium.com/@saifmdco/data-structures-in-python-matrix-22adba5aa597

Lecture 8: https://medium.com/@saifmdco/data-structures-in-python-sets-92d5445e97e8

Lecture 9: https://medium.com/@saifmdco/data-structures-in-python-chainmap-fca2a9d47249

Lecture 10: https://medium.com/@saifmdco/data-structures-in-python-linkedlist-50f2118c659e

Lecture 11: https://medium.com/@saifmdco/data-structures-in-python-stack-6bf182d63581

Lecture 12: https://medium.com/@saifmdco/data-structures-in-python-queue-6361d3dcff0

Lecture 13: https://medium.com/@saifmdco/data-structures-in-python-dequeue-1a585e269a55

Lecture 14: https://medium.com/@saifmdco/data-structures-in-python-custom-stack-b7b9173b4eae

Lecture 15: https://medium.com/@saifmdco/data-structures-in-python-hash-table-39be784eefc1

Lecture 16: https://medium.com/@saifmdco/data-structures-in-python-doubly-linked-list-fe698d74756c

--

--

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