CH5 Stacks
CH5 Stacks
2. St_len = le n(Stack)
3. ,f St_len == [I:
pr int("U nderfl ow"J
ou e 1etln& II, l.e~ peek()
the most ~cently • dde d element t ad POP operat1·ons arer m the top posi't'10n, 1.e.,
8 ' l t r ~ . . _ ~ ..
. Ias1
nmows k henever PUSH an elements ro =l~ ;~;•1:~:~:·~•,.-..,·1
or ~l•mrnts on lhe Stac w I traversal process, the se order. &tact• II ""'""'•-cit ..,.,~,, ..._u •
t-lemc.-nt> be-comes mandatory. n d rocessed in the rever r:, lnter l11,.,uc1
•flattar111 ._i..
1
lnsertrd elemen~ gel displayed an p 1
lementatlon-1 Stack, such as adding element In the above program, we have to find the number of non-repetitive vowels present in the inputted
Practical Imp I nt all basic operations of ad. laying the Stack elements word. This is done by comparing each letter of the word with the vowels lost and, when found. the
Wrote a Python program to Imp eme (POP operation) and ,sp
(PUSH operation). removing element vowels are added to the Stack (only unique vowels). Thus, Stack m the above program acts as a
(Traversal operation) using lists. container to hold all the vowels which are present m the given word and finally displayed using
.... - ... -- len(Stack) function with print() statement and, hence, the given output is obtained.
•. ,ppend(f)
•l it tdlolw-llt
1 •1
.... - . . a.--... -
I Uoqtuo to idd, <klU• ind ClUphy tllll 1eco1a.. ot Ill DISJI~ ~• l r.q l ht
,ac,1-nutlonthrouqnst.ac\ ..... ____ _ I
,1u
prlnt l"O.l Ued •l-11t u
tcboto.-11,
•, e _td, _ ) ~::!"
J.Ol.apl•J
l•lu!lllployN)
:1;;:4,.:!:-::t~t;no!1or110t1y
l
1.MII
J.,OP
}. Dhpl•Y
f 'll>l"'J" 'I
:
1
;:1,.:!~:\!:t~ni!'or IIOt? y :~:~.r:~~~~~:
Qoyou -
: I O AlLI
l toCOO.I UII.O•O• "'"ll J
I.fl/SIi
J.,or
l, Dllphy
~: :::!'
111t1ryoureholc:11: J ~t~~-~Cl\l>ICII )
C'2', 'ob.o»ry• ' 1
'~ you Wint to cont111u. or i:io:t1 11
_n ,~\~•~~:! - tu... o, _ , I
•
Appllcattons of Stacks
Some of the important applications of Stacks include: f . I'
f k pplication is reversa1o a given ine. This
1. Reversing a Word/Line: Asimple example O Slac • a Stack as it is read. When the line
can be accomplished by pushing each character on to ·11 off in the reverse o d
is finished, characters are popped off the Stack and they WI come r er.
Conversion of decimal number to binary is also done using S ack. t
• of Arithmetic Expressions: AStack is
2. Evaluation • a very e ffective data
. structure . for evaluating
.
arithmetic expressions in programming languages. An arithmetic express10n consists of
operands and operators. It may also include parentheses l1.ke "Ie ft parenthesis" . and "ng · ht
parenthesis". Stack is used to evaluate these arithmetic expressions on the basis of operator
precedence (BEDMAS - Brackets off; Exponentiation; Division/Multiplication; Addition/
Subtraction).
3. Processing Function Calls: The compilers use Stacks to store the previous state of a program
when a function is called or during recursion.
4. Backtracking: Backtracking is a form of recursion which involves choosing only one option
out of Lhe possibilities. Backtracking is used in a large number of puzzles like Sudoku and
in optimization problems such as knapsack.
5. Undo Mechanism in Text Editors: This operation is accomplished by keeping all text changes
in a Stack.
5.4 QUEUE*
In the previous topics, we have discussed implementation
of Stack. Now, we will learn implementation of Queue with
Python list.
f MEMORY BYTES
► A list is a collection of elements which are stored in a sequence.
► A Stack is a linear structure implemented in LIFO {Last In, First Out) manner where insertions and deletions take
place only at one end, i.e., the Stack's top.
► An insertion in a Stack is called pushing and a deletion from a Stack is called popping.
► A Queue is a linear structure implemented in FIFO (first in, first out) manner where insertions can occur only at
the rear end and deletions can occur only at the front end.
► Traversal of a list or Stack or a Queue means visiting each element of the list.
► A Queue is a container of objects that are inserted and deleted according to the FIFO principle.
► Inserting an element in a Queue is called Enqueue.
► Deleting an element from the Queue is called Dequeue.
► When a Queue is implemented with the help of a list, it is termed as a Linear Queue.