You are on page 1of 7

A Data Structure is a named group of data of different data

types which is stored in a specific way and can be processed as


a single unit.
A data structure has well-defined operations , behaviour and
properties.
CLASSIFICATION OF DATA STRUCTURES
1.Simple Data Structures
These data structures are normally built from primitive data
types like integers , reals, characters, boolean. Eg : Array or
Linear Lists
2.Compound Data Structures
Simple data structures can be combined in various ways to
form more complex structures called compound data
structures.Compound data structures are classified into
following two types:
a) Linear data structures :These data structures are single
level data structures. In Linear Data structures the
elements are stored in sequential order.Eg: Stack ,Queue,
Linked List.
b) Non-Linear data structures: These data structures are
multilevel data structures.Eg: Tree
STACK
• Stack is a linear data structure
• Stack is a list of elements in which an element may be
inserted or deleted only at one end called the Top of the
stack.
• It follows the principle Last In First Out (LIFO). LIFO
means the element inserted last would be the first to be
deleted.
Operations on Stack
There are mainly two types of operations that can be done with
stack
1. Push 2. Pop
Push : Insertion of an element on top of the stack is called
Push.
Pop : Removal of an element from the top of the stack is called
POP.
Note: Push and Pop operations are done from single end called
TOP
Eg: Suppose a list of 5 items to be inserted in the stack .

2
Push 9 9
operation 4 4 4
56 56 56 56
24 24 24 24 24
Push Push Push Push Push Stack
24 56 4 9 2 overflow
2
Pop 9 9
operation 4 4 4
56 56 56 56
24 24 24 24 24
Pop Pop Pop Pop Pop Stack
underflow
Stack Overflow: It tries to a situation , when one tries to push
an element in stack, that is full .(Stacks are list implemented ,
since list can grow, overflow condition does not arise until all
memory is exhausted.
Stack Underflow : It refers to a situation when one tries to pop
an item from an empty stack .

#implementation of stack using List


stack =[]
choice = 'y'
while choice =='y':
print("1.PUSH")
print("2.POP")
print("3.Display elements of stack")
choice=int(input("Enter your choice"))
if choice == 1:
a=int(input("Enter the element which you want to push"))
stack.append(a)
elif choice == 2:
if stack == []:
print("Stack is empty...Underflow case...")
else:
print("Deleted element is :", stack.pop())
elif choice == 3:
print(stack)
else:
print("Wrong input...")
choice = input("Do you want to continue or not ? (y/n)")
1 Write a function in Python PUSH(Arr) , where Arr is a list of
numbers. From this list push all numbers divisible by 5 into a
stack implemented by using a list. Display the stack if it has
atleast one element, otherwise display appropriate error
message. (Sample paper 2020-21)
def PUSH(Arr):
S=[]
for x in range(0,len(Arr)):
if Arr[x] % 5 == 0:
S.append(Arr[x])
if S== [ ]:
print("Empty Stack")
else
print(S)

2 Write a function in Python POP(Arr) , where Arr is a stack


implemented by a list of numbers. The function returns the
value deleted from the stack. (Sample paper 2020-21)

def POP(Arr):
if Arr == []:
print("Stack is empty…Stack Underflow")
else:
print("Deleted element is :",Arr.pop())

3 Write a function in Python, MakePush(Package) and


MakePop(Package) to add a new Package and delete a Package
from a List of Package Description , considering them to act as
push and pop operations of the Stack data structure.(Sample
paper 2019-20)

def MakePush(Package):
a=input(“Enter package title :”)
Package.append(a)
def MakePop(Package):
if Package == []:
print("Stack is empty...Underflow case...")
else:
print("Deleted element is :", Package.pop())

4 Write PushOn(Book) and Pop(Book) methods in Python to


add a new book and delete a Book from List of Book titles,
considering them to act as push and pop operations of the Stack
data structure.(Board 2019 )
5 Write PUSH(Names) and POP(Names) methods in Python to
add Names and Remove names considering them to act as Push
and Pop operations of Stack.(Board 2015)
MULTIPLE CHOICE QUESTIONS
4.When a stack is empty and an element is popped from a
stack , it is called an……………… case

5.When a stack is full and an element is pushed in a stack , it is


called an……………… case

The deleted element in the stack


STACK =[‘A’,’J’,’P’,’N’]
a)N is deleted
STACK=[‘A’,’J’,’P’]
b)STACK=[‘A’,’J’,’P’,’K’]
c) STACK =[‘A’,’J’,’P’,’K’,’S’]
d)’S’ is deleted
STACK=[‘A’,’J’,’P’,’K’]
e) STACK=[‘A’,’J’,’P’,’K’,’G’]

You might also like