[Type C]Q7. Write a python program to implement the following operations in the stack.

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:

  • 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: Each node of a Stack contains the following information:

1) Pin code of a city

2) Name of the city

Write a python program to implement the following operations in the above stack.

(a) PUSH() : to push a node onto the stack

(b) POP() : to remove a node from the stack

Program code:

Output:

[{'pin': 842001, 'city': 'Muz'},
{'pin': 110078, 'city': 'Delhi'},
{'pin': 820067, 'city': 'Patna'}]
Popped element is {'pin': 820067, 'city': 'Patna'}
Popped element is {'pin': 110078, 'city': 'Delhi'}
Final List = [{'pin': 842001, 'city': 'Muz'}]

In the above code snippet basic functions of Queue 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