12 - Revision-III-UNIT-I Computational Thinking and Programming - 2
12 - Revision-III-UNIT-I Computational Thinking and Programming - 2
Data Structures
Data Structures: Stack using List
Data Structure
In Data Structure w e a r e collecting and organizing data in such a way that wecan perform
operations on these data in an effective way.
Stack
A stack is a data structure whose elements are accessed according to the Last- In First-
Out (LIFO) mechanism. This is because in a stack, insertion and deletion of elements can
only take place at one end, which is called top of the stack.
In the above picture coins are kept one above the other and if any additional coinI need to be
added, it can be added only on the top.
Similarly, If we want to remove any coin from the stack, the coin on the top of the stack has to
be removed first. That means, the coin that was kept last (i.e. at the top) in the stack has to
be taken out first.
The two operations performed on the stack are:
Push operation: It means inserting element at the top of the stack. Thiscan be
done with the help of append() method of the list as: st.append(element) where ‘st’ is
a list.
Pop operation: It means removing the topmost element from the stack. This can be performed
using pop() method of the list as: st.pop() where ‘st’is a list. This method returns the removed
element that can be displayed.