Stack Data Structure-Python
I have been working on a bit of larger project recently so I haven’t been able to upload on this however I thought I would do something short and simple so I have quickly programmed a very simple stack in about 5 minutes on Python which im going to go over.
What is a Stack?
A stack is a data structure(like an array, hash table, binary tree etc.) which works on a last in first out system. This means the value most recently added to the list will become the first value to leave this list. This has very useful real world applications, eg it is how the undo button works: any operation you do is loaded onto a stack and when you click Ctrl-Z, it’s just going to pull out the value at the top of the list(the most recent thing you have done) and remove that operation which would undo the change.
Coding it:
The way to code a stack would be using Object Orientated Programming so we would be able to add class specific methods to the stack.
class stack():
def __init__(self, size ):
self.values=[]
self.pointer = 0
self.size = size
def add_values(self, value):
self.pointer += 1
if self.pointer> self.size:
return self.isfull()
self.values.append(value)
def isfull(self):
if self.pointer== self.size:
return True
else:
return False
def isEmpty(self):
if self.pointer ==0:
return True
else:
return False
def remove_value(self):
final = self.values.pop(len(self.values)-1)
self.pointer -= 1
return final
def length(self):
return self.pointer
def printStack(self):
for _ in range(self.pointer-1,-1,-1):
print (self.values[_])
Declaration
class stack:
def __init__(self, size ):
self.values=[]
self.pointer = 0
self.size = size
The first things we have to do in a class is declare it as I have done above and then have an __init__ function which is a dunder function which is standard for OOP. The only parameter we need passed if which is the set size of the stack. If, for example, you wanted to leave your user the option to not input any set size for their stack, you would go around doing this with the parameters(since you can’t not pass in a parameter if they don’t enter something or you’re going to get an argument error on your function, you would make use of args, kwargs. I won’t go into massive detail about them since it’s pretty hard to explain but basically, it allows you to pass as many arguments into a function as you like and for args it will put them in a list for you automatically while with kwargs, it will put it into a dictionary for you.
We set our attributes in this __init__ function as the values which will be the actual data inside the stack, a pointer to point at the top of the stack and also the maximum size.
Adding/Subtracting values:
def add_values(self, value):
self.pointer += 1
if self.pointer> self.size:
return self.isfull()
self.values.append(value)
def remove_value(self):
final = self.values.pop(len(self.values)-1)
self.pointer -= 1
return final
You can add values by appending them onto the stack and then incrementing the pointer and then doing the opposite for removing a value. We use ‘pop’ because when you pop, it returns the value and assigns it to the variable which is important since we want to see the value that we are removing from the stack. This is the main difference between .remove() and .pop(), pop will return the user a value while remove wont.
State Checks
def isEmpty(self):
if self.pointer ==0:
return True
else:
return False
def isfull(self):
if self.pointer== self.size:
return True
else:
return False
These just compare the pointer to either the size or 0 to check how many values are in the stack and then can return true or false to whether it is full or empty.