Student IM 3
Student IM 3
The two primary operations of a stack are adding items to the stack and taking items off the stack. The
Push operation adds an item to a stack. We take an item off the stack with a Pop operation. These
operations are illustrated above.
The other primary operation to perform on a stack is viewing the top of the item. The Pop operation
returns the top item, but the operation also removes it from the stack. We want to just view the top item
without actually removing it. This operation is named Peek.
Pushing, Popping, and Peeking are the primary operations we perform when using a stack; however,
there are other operations we need to perform and properties we need to examine. It is useful to be
able to remove all the items from a stack at one time. A stack is completely emptied by calling the Clear
operation. It is also useful to know how many items are in a stack at any one time. We do this by calling
the Count property. There is also an operation which returns true or false values if the stack is empty or
nor. This operation is the isEmpty operation.
First we create our own Stack class in a new file. Rename it as Stack.py with the methods: Push,
Pop, Peek, Count, Clear, and isEmpty.
class Stack:
def __init__(self, list):
self.list = list
def Pop(self):
self.list.pop(len(self.list) - 1)
def Clear(self):
self.list.clear()
def Count(self):
print(len(self.list))
def PrintStack(self):
print(self.list)
def isEmpty(self):
if (not len(self.list)):
NVSU-FR-ICD-05-00 (081220) Page 2 of 9
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.: IM-ITECC5-1S-2021-2022
return True
else:
return False
def Peek(self):
print(self.list[len(self.list) - 1])
Watch the indentations as well as the method spacings when coding in python and when coding the
Stack Class. The First Method we have is the __init__(self, list)
The __init__ method is the constructor. Constructors are generally used for instantiating objects in
programming. The task of the constructor is to initialize(assign values) to the data members when an
object of class is created.
So, here, __init__ is passing a list which we can get from other variables to the Stack class
implementation.
We have self as parameters because in python, it is used to represent the instance of the class. We all
have the operations and methods in our Stack Class in another file. Make sure that when using one or
more files, you make sure that they are in a single folder, the Main file as well as the other files which
the main file will be referencing.
Self.list = list
Just means that we are passing outside lists to our Stack Class. This does not mean however, that we
are only limited to only having lists as input to our Stack implementation. We can also insert individual
data using our Push operation/method indicated in our code.
mylist = [1, 2, 3, 4, 5, 6]
myStack = Stack(mylist)
print(myStack.isEmpty())
myStack.Count()
myStack.Pop()
myStack.PrintStack()
myStack.Clear()
print(myStack.isEmpty())
myStack.Push("aa")
myStack.Push("bn")
myStack.Count()
myStack.PrintStack()
The from Stack import Stack just means that we are referencing our Stack file, and we are importing
the Stack class from our Stack.py file. Implement the two files and see how the stacks work. You can
play around the main.py file were we implement the Stack Class.
The main operation for Queues are Enqueue and Dequeue. When we enter data into the Queue, we
use the operation Enqueue and when we need to remove data from our list or array, we use the
Dequeue operation. We also have Count, isEmpty, Print, Peek as well as Clear operations just like
our Stacks. When inserting data and removing data from Queues, we employ a First-in, First
Out(FIFO) data structure. For peeking in queues, we see the latest element which should be the first
to be dequeued which is the first one or the first element in our queue.
class Queue:
def __init__(self, list):
self.list = list
def Dequeue(self):
self.list.pop(0)
def Clear(self):
self.list.clear()
def Count(self):
print(len(self.list))
def PrintQueue(self):
print(self.list)
def isEmpty(self):
if (not len(self.list)):
return True
else:
return False
def Peek(self):
print(self.list[0])
and for our main.py, we can input the following to test out our Queue implementation
from Stack import Stack
from Queue import Queue
mylist = [1, 2, 3, 4, 5, 6]
myQueue = Queue(mylist)
myQueue.PrintQueue()
myQueue.Enqueue(7)
myQueue.Dequeue()
print(myQueue.isEmpty())
myQueue.Clear()
print(myQueue.isEmpty())
myQueue.PrintQueue()
myQueue.Enqueue(22)
myQueue.Enqueue(32)
myQueue.Enqueue(67)
myQueue.PrintQueue()
execute the main.py file and see how our Queue differs from our Stack implementation. Again, we can
input anything in our main.py file and we can test out our Queue and Stack implementation using a
single main.py.
To search for elements in our Stacks and Queues. We can implement a method which can check for
elements in out Stacks and Queues. This method is:
We can insert the Search method in our Stack Class and Queue Class and when we use the Search
method, we do the for example:
myStack.Search(7) ----- The method will be searching for 7 in our stack and will return a message if it
is in our stack or not
myQueue.Search(12) ---- The method will be searching for 12 in our queue and will return a message
if it is in our queue or not.
6. Priority Queues
As you know, a queue is a data structure where the first item placed in the structure is the first item
taken out of the structure. The effect of the behavior is the oldest item in the structure that is removed
first. For many applications, though, a data structure is needed where an item with the highest priority is
removed first, even if it isn’t the “oldest” item in the structure. There is a special case of the Queue
made for this type of application – the Priority Queue.
There are many applications that utilize priority queues in their operations. A good example is process
handling in a computer operating system. Certain processes have a higher priority that other
processes, such as printing processes, which typically have a low priority. Processes or tasks are
usually numbered by their priority, with Priority – process having a higher priority than a Priority 20 task.
Items that are stored in a priority queue are normally constructed as key-value pairs, where the key is
the priority level and the value identifies the item. For our python implementation, we borrow the
underlying principles of our Queue implementation, adding only a sorting function after we Enqueue
data into our Priority Queue.
self.list = list
def Dequeue(self):
self.list.pop(0)
def Clear(self):
self.list.clear()
def Count(self):
print(len(self.list))
def PrintQueue(self):
print(self.list)
def isEmpty(self):
if (not len(self.list)):
return True
else:
return False
def Peek(self):
print(self.list[0])
We create a new file, with PriorityQueue.py as its filename. Then, we implement the Priority Queue
Class in our PriorityQueue.py file. We can also test our Priority Queue implementation using our
main.py file, just as long as they are located in the same file for easy reference. An example of using
the main.py file to test the Priority Queue is as follows:
myPrioQueue.Enqueue((2, "Tolentino"))
myPrioQueue.PrintQueue()
The Priority Queue implementation also uses the same components as our Queue implementation.
The only difference is that the Priority Queue should accept key-value pairs. These pairs should be
inside parentheses and they contain priority keys such as numerical values to know which of which
should be prioritized to be dequeued in our Priority Queue implementation. For example, in the mylist
array, a list imported to the priority queue implementation should follow the format:
[(1, “data 1”),(5, “data 2”),(3, “data 3”)]. The numerical values beside our data are what we use to sort
all of the data being inserted and passed to our Priority Queue. After enqueueing data, we sort them
so, if for example, we were to add the list to our Priority Queue, we would have the following
sequence:
[(1, “data 1”),(3, “data 3”) ,(5, “data 2”)]
The data with a key value of 5 will be located at the last place of our Priority Queue.
Note: Say we don’t want to insert a list into our Queue, Stack or Priority Queue when coding, we can do
that by adjusting the def __init__ method in our implementations. For example, if we wanted to insert
data one by one using Enqueue operation in our Queue, then our implementation of our Queue
changes into:
class Queue:
def __init__(self):
self.list = []
def Dequeue(self):
self.list.pop(0)
def Clear(self):
self.list.clear()
def Count(self):
print(len(self.list))
def PrintQueue(self):
print(self.list)
def isEmpty(self):
if (not len(self.list)):
return True
else:
return False
def Peek(self):
print(self.list[0])
The same is true with implementing Stacks when we want to insert data into our stack on by one, or
even in our Priority Queue.
VI. REFERENCES
GOODRICH, ET. AL, DATA STRUCTURES AND ALGORITHMS IN PYTHON, 2013
RAMALHO, LUCIANO, FLUENT IN PYTHON, 2015
MCMILLAN, MICHAEL, DATA STRUCTURES AND ALGORITHMS USING C#, 2007
School Year
Semester
Course Number
e.g.:
IM-COURSE NO-SEMESTER-SCHOOL YEAR
IM-MCB180-1STSEM-2020-2021