[Type C]Q1. Write a program to push or pop an element from stack depending upon the user’s choice.

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 of time. 

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: Section C 1] Write a program to push or pop an element from stack depending upon the user’s choice.

Program code:

Output:

Enter your choice:
1:Press 1 to Push
2:Press 2 to Pop
Your Option:1Enter element to push on stack:5
[4, 5, 6, 7, 5]

Enter your choice:
1:Press 1 to Push
2:Press 2 to Pop
Your Option:2
The last entered element was popped!
[4, 5, 6]

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