Free System Design Interview Course
Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2023 with this popular free course.
Stack is a linear data structure in which the element inserted last is the element to be deleted first.
It is also called Last In First Out (LIFO).
In a stack, the last inserted element is at the top.
Operations of the stack are
push(): inserts an element into the stack at the endpop(): deletes and returns the last inserted element from the stackpeek(): returns the last inserted elementAfter inserting three elements in the stack, it will look like this:
After performing the pop operation twice, the stack elements will look like:
def push(stack,element):
"""insertion of 'element' into the stack"""
stack.append(element)
def pop(stack):
"""deletes and returns the last inserted element"""
if len(stack)==0:
print("stack underflow")
quit()
return stack.pop()
def peek(stack):
"""returns the last inserted element"""
if len(stack)==0:
print("stack is empty")
return -1
return stack[-1]
RELATED TAGS
CONTRIBUTOR