stack in python

 

stack in python

 

How to Implement a Python Stack


Introduction to stacks and Queue data structures, which organized computer storage for efficient access and manipulation of data.

The stacks and queue are very simple to learn and execute with ease, and their usage is very common.

Stacks follows the LIFO principle that is last in first out. The last element will be removed or pop first.

Two operations are therein, one is pushed, which adds an element to the top of the stack, and the second one is pop, which removes the element at the top of the stack.

stack in python

So let us begin with implementing stacks, using lists in python. Let us first create an empty list name[].

We are going to add elements to the list, name [] using the append () method.

# create an empty list name

name=[]

# add elements into the list with append method in the list as Stack

name.append('Siraz')

name.append('Dhoni')

name.append('Shami')

name.append('Kholi')

name.append('Rahul')

name.append('Bumrah')

print(name)

Adding elements to the list using the append() method ensures the push operation of the stack.

Now let us try removing elements from the list.

name.pop()

It will delete the Bumrah as it is entered last

['Siraz', 'Dhoni', 'Shami', 'Kholi', 'Rahul']

Let us try to delete the item again from the list for which we have to write the same syntax

name.pop()

The above code deletes Rahul from the list, and we are left with only

['Siraz', 'Dhoni', 'Shami', 'Kholi']

Stack using deque in python

Implementing push with the help of append() method of the deque, We have to import deque from python collection library as follows

 from collections import deque

# We can initialize a deque with a list

name = deque()

 

# Use append like before to add elements

name.append ('Siraz')

name.append ('Dhoni')

name.append ('Shami')

name.append ('Kholi')

name.append ('Rahul')

name.append ('Bumrah')

 so when we print the print(name), it will print as follows

deque(['Siraz', 'Dhoni', 'Shami', 'Kholi', 'Rahul', 'Bumrah'])

 

Now using pop() of deque, we can implement stack pop operation as follows

name.pop()

print (name)

deque(['Siraz', 'Dhoni', 'Shami', 'Kholi', 'Rahul'])

The last element is Bumrah is deleted first

name.pop()

print (name)

deque(['Siraz', 'Dhoni', 'Shami', 'Kholi'])

The second last element Rahul is removed second and so on.

 

python queue module

Implementation using python queue module

In the Python queue module there is a LIFO Queue, which is basically nothing but Stack. Element is inserted into the queue using put() method that act as PUSH and get () remove data out from the queue that act as POP operation of stack.

from queue import LifoQueue

 

# Initializing the stack with maxsize is 10

stack = LifoQueue(maxsize = 10)

 

# qsize() show the number of elements

# in the stack

print(“Initial size of  ”stack.qsize())

 

# put() function to push

# element in the stack

stack.put ('Siraz')

stack.put ('Dhoni')

stack.put ('Shami')

stack.put ('Kholi')

stack.put ('Rahul')

stack.put ('Bumrah')

print("Full is over or not : ", stack.full())

print("Total size of the stack:  ", stack.qsize())

 

# This get() method to act as pop operation of stack in

# LIFO order that is (Last in First Out)

print ('\nPoped operation from the stack')

print (stack.get())

print (stack.get())

print (stack.get())

 

print("\nStack Empty or not : ", stack.empty())

 

Output

Initial size of  0

Full:  False

Size:  6

 

Elements poped from the stack

Bumrah

Rahul

Kholi

Empty:  False


Python Articles



SHARE THIS
Previous Post
Next Post