[Type C]Q3. Write a python program that pushes or pops an element in the stack. The elements are shifted towards the right so that the top always remains at the 0th index.

What is a Stack?

A stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). Consider an example of plates stacked over one another in the canteen. The plate which is at the top is the first one to be removed, i.e. the plate which has been placed at the bottommost position remains in the stack for the longest period. 

Operations of Stack:

Mainly the following two basic operations are performed in the stack:

  • Push: Adds an item at the top of the stack. If the stack is full, then it is said to be an Overflow condition.
  • Pop: Removes an item from the top of the stack. If the stack is empty, then it is said to be an Underflow condition.

Problem: Write a python program that pushes or pops an element in the stack. The elements are shifted towards the right so that the top always remains at the 0th index.

Program code:

Output:

[8, 4, 5, 6, 7]
[4, 5, 6, 7]

In the above code snippet basic functions of stack are demonstrated.

To view all the lessons in chapter 10: http://computertutor.in/class-12-computer-science/chapter-10/

To view entire class 12 computer science study material: http://computertutor.in/resources/

You cannot copy content of this page