0% found this document useful (0 votes)
12 views3 pages

Data Structure

The document provides an overview of stacks as a fundamental data structure in computer science, emphasizing their Last-In-First-Out (LIFO) nature. It details key operations such as push, pop, peek, and checks for stack emptiness, along with error conditions like overflow and underflow. Additionally, it includes a Python implementation of stack operations and a sample run of the program.

Uploaded by

Harish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Data Structure

The document provides an overview of stacks as a fundamental data structure in computer science, emphasizing their Last-In-First-Out (LIFO) nature. It details key operations such as push, pop, peek, and checks for stack emptiness, along with error conditions like overflow and underflow. Additionally, it includes a Python implementation of stack operations and a sample run of the program.

Uploaded by

Harish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DATA STRUCTURE

Introduction
Stacks are a fundamental data structure in computer science, and they’re covered in
the CBSE Class 12 curriculum. Here’s a breakdown of how to work with stacks in
Python:
What is a Stack?
A stack is a linear data structure that follows the Last-In-First-Out (LIFO)
principle. Think of it like a stack of plates: you can only add or remove plates from the
top.
Key Operations in stacks:
 Push: Add an element to the top of the stack.
 Pop: Remove the top element from the stack.
 Peek: View the top element without removing it.
 isEmpty: Check if the stack is empty.
Stacks in python are a dynamic data structure as they can grow (with increase in
number of elements) or shrink (with decrease in number of elements). A static data
structure, on the other hand, is the one that has fixed size.

Some Other Stack Terms in python


There are some other terms related to stacks, such as peek, overflow and Underflow.

Peek:
Refers to inspecting the value at the stack’s top without removing it; it is also
sometimes referred as inspection.

Overflow:
Refers to situation (ERROR) when one tries to push an item in stack that is full. This
situation occurs when the size of the stack is fixed and cannot grow further or there is
no memory left to accommodate new item.

Underflow:
Refers to situation (ERROR) when one tries to pop/ delete an item from an empty stack.
That is, stack is currently having no item and still one tries to pop an item. Consider
some examples illustrating stack-functioning in limited- size stack. (Please note, we
have bound fixed the capacity of the stack for understanding purposes.)

Implementing stacks in python


Program Code:
Python program to implement stack operations.
STACK IMPLEMENTATION
“““
Stack: implemented as a list
top: integer having position o topmost element in stack
““ “
def is Empty (stk):
if stk == [ ]
return True
else:
returns False
def push (stk, item) :
stk. append (item)
top = len (stk) -1
def pop (stk):
if is Empty (stk) :
return “underflow”
else:
item = stk. Pop ( )
if len (stk) == 0:
top = None
else:
top = len (stk) -1
return item
def peek (stk) :
if is Empty (stk) :
return “underflow”
else:
top = len (stk) -1
return stk [top]
def Display (stk) :
if is Empty (stk) :
print (:stack empty”)
else:
top = len (stk) -1
print (stk[top], “<- top” )
for a in range (top- 1, -1, -1 ) :
print (stk [a])

#_main_
stack = [ ] # initially stack is empty
top = None
While True:
print (“STACK OPERATIONS”)
print (“1. Push”)
print (“2. Pop”)
print (“3. Peek”)
print (“4. Display stack”)
print ( “5. Exit”)
ch = int (input (“Enter your choice (1-5) :” ) )
if ch == 1 :
item = int (input (“Enter item:” ) )
push (stack, item)
elif ch == 2 :
item = pop (stack)
if item ==”underflow”
print (“underflow” : stack is empty !” )
else:
print (“popped item is”, item)
elif ch == 3:
item = peek (stack)
if item ==”underflow”
print (“underflow! Stack is empty!”)
else:
print (“Topmost item is”, item)
elif ch == 4:
Display (stack)
elif ch == 5:
break
else:
print (“Invalid choice!” )
Sample run of the above program is as shown below:

You might also like