DAY 3 — Stacks

Yasir
2 min readSep 20, 2020

--

It’s a series of revising DS algo. Check this link for more details .

Photo by Fotis Fotopoulos on Unsplash

A stack is an ordered list in which insertion and deletion a re done al one end, called top. The last element inserted is the first one to be deleted. Hence, it is called the Last in First out (LI PO) or First in Last out (FI LO) list.

Applications

Stack is a wisely used data structure , some of them are below :

  • Balancing of symbols
  • lnfix-to-postfix conversion Evaluation of postfix expression
  • Implementing functions calls (including recursion)
  • Finding of s pans (finding spans in stock markets, refer to Problems section)
  • Page-visited history in a Web browser [Back Buttons]
  • Undo sequence in a text editor
  • Matching Tags in HTML and XML

We use stack to implement in graphs or tree travel also .

Implementing Stack

We can Implement stack using Array

We can Implement stack using Dynamic Array (List)

We can Implement stack using Linked List

1. Implementing Stack by Static Array in Python

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

def isEmpty(self):
return len(self.stack) <= 0

def push(self,value):
if len(self.stack) >= self.limit:
print("Stack is Overflow")
else:
self.stack.append(value)

def pop(self):
if len(self.stack) <= 0:
print("Stack is Underflow")
return

else:
return self.stack.pop()

def peek(self):
if len(self.stack) <= 0:
print("Stack Underflow")
return
else:
return self.stack[-1]

def size(self):
return len(self.stack)
myStack = Stack()
myStack.push(3)
print(myStack.peek())

2. Implementing Stack by Dynamic Array in Python

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

def isEmpty(self):
return self.stack == []

def push(self,ele):
self.stack.append(ele)

def pop(self):
if not self.isEmpty():
return self.stack.pop()
else:
return -1

def peek(self):
if not self.isEmpty():
return self.stack[-1]
else:
return -1

3. Implementing Stack by Linked List in Python

class Node: 
def __init__(self,data):
self.data = data
self.next = None

class Stack:
def __init__(self):
self.head = None

def isempty(self):
if self.head == None:
return True
else:
return False

def push(self,data):

if self.head == None:
self.head=Node(data)

else:
newnode = Node(data)
newnode.next = self.head
self.head = newnode

def pop(self):

if self.isempty():
return None

else:
poppednode = self.head
self.head = self.head.next
poppednode.next = None
return poppednode.data

def peek(self):

if self.isempty():
return None

else:
return self.head.data

def display(self):

iternode = self.head
if self.isempty():
print("Stack Underflow")

else:

while(iternode != None):

print(iternode.data,"->",end = " ")
iternode = iternode.next
return
myStack = Stack()myStack.push(1)
print(myStack.peek())

Sources

My LinkedIn — linkedin.com/in/my-pro-file

To become a part of this series checkout this link .

--

--

Yasir
Yasir

Written by Yasir

Exploring Swift, SwiftUI, and Apple Universe.

No responses yet