Home » Python Stack: Understanding the LIFO Data Structure and its Implementation in Python

Python Stack: Understanding the LIFO Data Structure and its Implementation in Python

In Python, a stack is a collection of elements with push and pop operations, stored in LIFO order. A list can implement a stack using append and pop.

by Isrg Buzz Team
Python Stack

In computer science, a stack is an abstract data type that serves as a collection of elements, with two main operations: push and pop. The push operation adds an element to the top of the stack, while the pop operation removes the element from the top of the stack. The elements are stored in a Last-In-First-Out (LIFO) manner, meaning that the last element added to the stack is the first one to be removed.

In Python, a list can be used to implement a stack by using append to simulate the push operation and pop to simulate the pop operation.

The (CBSE) NCERT Computer Science Python Class XII Chapter 2: Stack provides a comprehensive understanding of the Stack data structure and its implementation in Python. The chapter covers various important concepts such as infix and postfix expressions, evaluation of postfix expressions using Stack, and implementation of Stack to store only odd numbers.

It includes a wide range of questions, answers, and solutions to help students grasp the concepts effectively and apply them in real-life situations. The chapter is designed to provide hands-on experience and a strong foundation in the Stack data structure, making it an important resource for students preparing for their exams.

State TRUE or FALSE for the following cases:

  1. Stack is a linear data structure: TRUE
  2. Stack does not follow LIFO rule: FALSE
  3. PUSH operation may result into underflow condition: TRUE
  4. In POSTFIX notation for expression, operators are placed after operands: TRUE

Find the output of the following code

a)

The output of the code will be:

b)

The output of the code will be:

Write a program to reverse a string using stack

The code creates an empty stack, pushes each character from the input string onto the stack, and then pops each character from the stack to form the reversed string.

This code will produce the following output:

For the following arithmetic expression: ((2+3)*(4/2))+2

Here’s a step-by-step process to match parentheses using a stack data structure in Python:

  1. Initialize an empty stack.
  2. Iterate through the expression, character by character.
  3. For each opening parenthesis, push it onto the stack.
  4. For each closing parenthesis, pop the top element from the stack. If the popped element is not the corresponding opening parenthesis, there is a mismatch, and the expression is invalid.
  5. If the stack is not empty after processing all the characters, there is a mismatch, and the expression is invalid.
  6. If the stack is empty, the parentheses in the expression are properly matched.

Here’s an implementation in Python:

This code outputs:


In Python, you can use the eval() function to evaluate an arithmetic expression:

This will output:

Whereas, in Mathematics this expression can be evaluated as follows:

  1. Perform the operation inside the innermost parentheses first: 4/2 = 2.
  2. Replace the expression (4/2) with its result, 2: ((2+3)*2)+2.
  3. Perform the next set of parentheses: (2+3) = 5.
  4. Replace the expression (2+3) with its result, 5: (5*2)+2.
  5. Perform the next operation: 5*2 = 10.
  6. Replace the expression (5*2) with its result, 10: 10+2.
  7. Perform the final operation: 10+2 = 12.

So the expression ((2+3)*(4/2))+2 evaluates to 12.

Evaluate following postfix expressions while showing status of stack after each operation given

Postfix expressions while showing status of stack after each operation

Here is the implementation in Python:

Convert the following infix notations to postfix notations, showing stack and string contents at each step.

  1. A + B – C * D
  2.  A * (( C + D)/E)

Here is the step-by-step process to convert the following infix notations to postfix notations:

a) A + B – C * D

  • Step 1: Initialize an empty stack and empty string. Stack: [ ] String: [ ]
  • Step 2: Scan the infix expression from left to right.
  • Step 3: If the character is an operand (A, B, C, or D), add it to the string. Stack: [ ] String: [A]
  • Step 4: If the character is an operator (+), push it onto the stack. Stack: [+] String: [A]
  • Step 5: If the character is an operand (B), add it to the string. Stack: [+] String: [A B]
  • Step 6: If the character is an operator (-), push it onto the stack. Stack: [+ -] String: [A B]
  • Step 7: If the character is an operand (C), add it to the string. Stack: [+ -] String: [A B C]
  • Step 8: If the character is an operator (*), push it onto the stack. Stack: [+ – *] String: [A B C]
  • Step 9: If the character is an operand (D), add it to the string. Stack: [+ – *] String: [A B C D]
  • Step 10: Repeat Steps 3 to 9 until the end of the infix expression.
  • Step 11: If the stack is not empty, pop the operators from the stack and add them to the string. Stack: [ ] String: [A B C D * – +]
  • The postfix expression is A B C D * – +

b) A * ((C + D)/E)

  • Step 1: Initialize an empty stack and empty string. Stack: [ ] String: [ ]
  • Step 2: Scan the infix expression from left to right.
  • Step 3: If the character is an operand (A), add it to the string. Stack: [ ] String: [A]
  • Step 4: If the character is an operator (), push it onto the stack. Stack: [ ] String: [A]
  • Step 5: If the character is an opening parenthesis, push it onto the stack. Stack: [* ( ] String: [A]
  • Step 6: If the character is an operand (C), add it to the string. Stack: [* ( ] String: [A C]
  • Step 7: If the character is an operator (+), push it onto the stack. Stack: [* ( + ] String: [A C]
  • Step 8: If the character is an operand (D), add it to the string. Stack: [* ( + ] String: [A C D]
  • Step 9: If the character is a closing parenthesis, pop the operators from the stack and add them to the string until an opening parenthesis is encountered. Stack: [* ( ] String: [A C D +]
  • Step 10: If the character is an operator (/), push it onto the stack. Stack: [* ( / ] String: [A C D +]
  • Step 11: If the character is an operand (E), add it to the string. Stack: [* ( / ] String: [A C D + E]
  • Step 12: Repeat Steps 3

Write a program to create a Stack for storing only odd numbers out of all the numbers entered by the user. Display the content of the Stack along with the largest odd number in the Stack. (Hint. Keep popping out the elements from stack and maintain the largest element retrieved so far in a variable. Repeat till Stack is empty

 

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More