0% found this document useful (0 votes)
3 views1 page

Python Stack

The document presents two implementations of a stack data structure: one using a linked list and the other using an array. The linked list implementation includes methods for checking if the stack is empty, pushing and popping items, peeking at the top item, and displaying the stack contents. The array implementation provides similar functionalities, including checking if the stack is empty, pushing and popping items, peeking at the top item, and determining the size of the stack.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Python Stack

The document presents two implementations of a stack data structure: one using a linked list and the other using an array. The linked list implementation includes methods for checking if the stack is empty, pushing and popping items, peeking at the top item, and displaying the stack contents. The array implementation provides similar functionalities, including checking if the stack is empty, pushing and popping items, peeking at the top item, and determining the size of the stack.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

# Stack using LL # Stack using Array

class Node: class Stack:


def __init__(self, data):
[Link] = data def __init__(self):
[Link] = None [Link] = []

class Stack: def isEmpty(self):


def __init__(self): return [Link] == []
[Link] = None
def push(self, item):
def isempty(self): [Link](item)
if [Link] == None:
return True def pop(self):
else: return [Link]()
return False
def peek(self):
def push(self, data): return [Link][len([Link])-1]
if [Link] == None:
[Link] = Node(data) def size(self):
else: return len([Link])
newnode = Node(data)
[Link] = [Link]
[Link] = newnode

def pop(self):
if [Link]():
return None
else:
poppednode = [Link]
[Link] = [Link]
[Link] = None
return [Link]

def peek(self):
if [Link]():
return None
else:
return [Link]

def display(self):
iternode = [Link]
if [Link]():
print("Stack Underflow")
else:
while(iternode != None):
print([Link], "->")
iternode = [Link]
return

You might also like