Categories: Data Structurepython

Implementation of Stack in Python

Overview of Stack Data Structure πŸ€”πŸ˜—

  • A stack is an ordered collection of items where the addition of new items and the removal of existing items always takes place at the same end.
  • This end is commonly referred to as the β€œtop.”
  • The end opposite the top is known as the β€œbase.”
  • The base of the stack is significant since items stored in the stack that are closer to the base represent those that have been in the stack the longest.
  • The most recently added item is the one that is in position to be removed first.
  • This ordering principle is sometimes called LIFO, last-in-first-out. πŸ‘ˆπŸ˜²
LIFO, last-in-first-out in Stack
  • It provides an ordering based on the length of time in the collection.
  • Newer items are near the top, while older items are near the base.
  • Note how the first items β€œpushed” to the stack begin at the base, and as items are β€œpopped” out.
  • Stacks are fundamentally important, as they can be used to reverse the order of items.
  • The order of insertion is the reverse of the order of removal.
  • Considering this reversal property, you can perhaps think of examples of stacks that occur as you use your computer. For example, every web browser has a Back button. πŸ€”πŸ€“
  • As you navigate from web page to web page, those pages are placed on a stack (actually it is the URLs that are going on the stack). 😍😍
  • The current page that you are viewing is on the top and the first page you looked at is at the base.
  • If you click on the Back button, you begin to move in reverse order through the pages.

Now we are going to implement our own Stack class πŸ‘‡πŸ‘‡

class Stack:

 def __init__(self):

  self.items = []

 def isEmpty(self) -> bool:

  return self.items == []

 def push(self, data: any) -> list:

  self.items.append(data)

 def pop(self) -> any:

  return self.items.pop()

 def peek(self) -> any:

  return self.items[len(self.items) - 1]

 def size(self) -> int:

  return len(self.items)

s = Stack()

#Add element to Stack
s.push(1)
s.push(2)
s.push("hello")

#size of stack
n = s.size()
print("Stack Size {}".format(n))

#delete element 
print(s.pop())

#pick top element
print(s.peek())

#check empty
print(s.isEmpty())

Recommended Reading: Balanced Parentheses Check in Python πŸ‘ˆ

Follow Me ❀😊

If you like my post please follow me to read my latest post on programming and technology.

Instagram

Facebook

Share
Published by
Hassan Raza

Recent Posts

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

2 hours ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

1 day ago

Largest Sum of Non-Adjacent Numbers

Problem Statement (Asked By Airbnb) Given a list of integers, write a function to compute…

3 days ago

Count Unival Subtrees in a Binary Tree

A unival tree (short for "universal value tree") is a tree in which all nodes…

4 days ago

Count Decoding Ways for Encoded Messages

Problem Statement (Asked By Facebook) Given the mapping a = 1, b = 2, ...,…

5 days ago

Implement an XOR Linked List

An XOR-linked list is a memory-efficient version of a doubly linked list. Instead of each…

6 days ago