Data Structures - Stack - Day 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Pavithra Karthik

• A stack is a data structure that allows adding and removing elements according to the Last-In First-
Out (LIFO) principle. Every time an element is added, it goes on the top of the stack; the only
element that can be removed is the element that was at the top of the stack.

• The operations performed on the stack are:

• → Push: Adding (inserting) new element on to the stack.

• → Pop: Removing (deleting) an element from the stack

• → Traversal: Printing elements of the stack

➔ Peek : Get the element that is at the top of stack


Pavithra Karthik
“Stack is a linear data structure which follows a particular order in which Board 2022
the operations are performed.”
What is the order in which the operations are performed in a Stack ?
Name the List method/function available in Python which is used to
remove the last element from a list implemented stack.
Also write an example using Python statements for removing the last
element of the list.
Pavithra Karthik

Differentiate between Push and Pop operations in the context of stacks. Compt 2022
Pavithra
Write the definition Karthik
of a user defined function PushNV(N) which accepts a list of strings in the parameter N and
pushes all strings which have no vowels present in it, into a list named NoVowel.
● Write a program in Python to input 5 Words and push them one by one into a list named All.
The program should then use the function PushNV() to create a stack of words in the list NoVowel so that it
stores only those words which do not have any vowel present in it, from the list All.
Thereafter, pop each word from the list NoVowel and display the popped word. When the stack is empty, display
the message
"EmptyStack".
For example:
If the Words accepted and pushed into the list All are
['DRY', 'LIKE', 'RHYTHM', 'WORK', 'GYM’]
Then the stack NoVowel should store
['DRY', 'RHYTHM', 'GYM']
And the output should be displayed as
GYM RHYTHM DRY EmptyStack
Pavithra Karthik
Pavithra Karthik
●Write the definition of a user defined function Push3_5(N) which accepts a list of
integers in a parameter N and pushes all those integers which are divisible by 3 or
divisible by 5 from the list N into a list named Only3_5.

●Write a program in Python to input 5 integers into a list named NUM. The program
should then use the function Push3_5() to create the stack of the list Only3_5. There
after pop each integer from the list Only3_5 and display the popped value. When the
list is empty, display the message "StackEmpty".
Pavithra Karthik
Pavithra Karthik
Write separate user defined functions for the following : 3 Compt 2022

(i) PUSH(N) This function accepts a list of names, N as parameter.


It then pushes only those names in the stack named OnlyA which contain the letter 'A'.
(ii) POPA(OnlyA) This function pops each name from the stack OnlyA and displays it. When the stack is empty,
the message "EMPTY" is displayed.
For example :
If the names in the list N are
['ANKITA', 'NITISH', 'ANWAR', 'DIMPLE', 'HARKIRAT']
Then the stack OnlyA should store
['ANKITA', 'ANWAR', 'HARKIRAT']
And the output should be displayed as
HARKIRAT ANWAR ANKITA EMPTY
Pavithra Karthik
Pavithra Karthik
Write the following user defined functions : 3 Compt 2022
(i) pushEven(N) This function accepts a list of integers named N as parameter. It then pushes only even
numbers into the stack named EVEN.
(ii) popEven(EVEN) This function pops each integer from the stack EVEN and displays the popped value.
When the stack is empty, the message "Stack Empty" is displayed.
For example :
If the list N contains
[10,5,3,8,15,4]
Then the stack, EVEN should store
[10,8,4]
And the output should be
4 8 10 Stack Empty
Pavithra Karthik
Pavithra Karthik
Pavithra Karthik
Pavithra Karthik
Pavithra Karthik

You might also like