Stack
Stack — Study Notes
NCERT-aligned · 9 notes · 3 shown free
3.1 INTRODUCTION
Explanation3.1 INTRODUCTION
In this section, the concept of data structures is introduced as a fundamental mechanism for storing, organizing, and accessing data efficiently. Data structures group multiple data elements in a particular way to enable faster accessibility and efficient storage of data. For example, strings, lists, sets, and tuples in Python are sequence data types that can represent a collection of elements either of the same type or different types. A data structure defines a mechanism to store, organize, and access data along with operations (processing) that can be efficiently performed on the data. For instance, a string is a data structure containing a sequence of characters, while a list is a sequence data structure where each element may be of different types. Various operations such as reversal, slicing, and counting can be performed on these data structures. Other important data structures in computer science include arrays, linked lists, binary trees, heaps, graphs, and sparse matrices. A data structure in which elements are organized in a sequence is called a linear data structure. Stack and queue are two other popular linear data structures used in programming. Although not directly available as built-in types in Python, it is important to learn these concepts as they are extensively used in many programming languages. This chapter focuses on the stack data structure, its implementation in Python, and its applications.
- Data structures group multiple data elements for efficient storage and access.
- Strings, lists, sets, and tuples are sequence data types in Python.
- A data structure organizes data to perform operations efficiently.
- Linear data structures arrange elements in a sequence.
- Stack and queue are important linear data structures used in programming.
- Stack is not a built-in Python data type but can be implemented using lists.
- 📌 Data Structure: A mechanism to store, organize, and access data efficiently.
- 📌 Linear Data Structure: A data structure where elements are arranged sequentially.
3.2 STACK
Explanation3.2 STACK
This section introduces the stack data structure by drawing analogies with everyday objects such as piles of books or plates. A stack is a linear arrangement where elements are added and removed from only one end, called the top. For example, when placing a book or plate on a pile, it is always added on the top, and similarly, removal is done from the top. This is because accessing elements from the middle or bottom of a large pile is inconvenient. This arrangement follows the Last-In-First-Out (LIFO) principle, meaning the element inserted last is the first to be removed. The stack is a linear data structure where insertion and deletion happen at the same end, the top of the stack. This principle is fundamental to understanding stack operations and their applications in programming and real life.
- Stack is a linear data structure with insertion and deletion at one end only (top).
- Follows Last-In-First-Out (LIFO) principle.
- Elements are added (PUSH) and removed (POP) from the top of the stack.
- Analogous to piles of books or plates where only the top element is accessible.
- Stack is used to organize elements in a sequence with restricted access.
- Efficient for scenarios where recent data needs to be accessed first.
- 📌 Stack: A linear data structure that follows LIFO principle.
- 📌 Top: The end of the stack where elements are inserted or removed.
- 📌 LIFO (Last-In-First-Out): The last element inserted is the first to be removed.
3.2.1 APPLICATIONS OF STACK
Explanation3.2.1 APPLICATIONS OF STACK
This section explores various real-life and programming applications of the stack data structure. Real-life examples include piles of clothes in an almirah, multiple chairs stacked vertically, bangles worn on a wrist, and piles of boxes in a pantry o
Practice Questions — Stack
Includes NCERT exercise questions with answers
Q1.For queues, FIFO stands for:
Answer:
First In First Out
Q2.In a stack, Insertion is done at Top. From which end does deletion take place ?
Answer:
Top
Q3.In stack, LIFO stands for:
Answer:
Last In First Out
Q4.In a queue, if front is equal to rear, it means that:
Answer:
Queue has only one element
Q5.Which of the following is NOT an application of Stack:
Answer:
Handling of Interrupts
Q6.1. State TRUE or FALSE for the following cases: a) Stack is a linear data structure b) Stack does not follow LIFO rule c) PUSH operation may result into underflow condition d) In POSTFIX notation for expression, operators are placed after operands
Answer:
a) TRUE. Stack is a linear data structure as elements are arranged sequentially. b) FALSE. Stack follows LIFO (Last In First Out) rule. c) FALSE. PUSH operation may result in overflow condition, not underflow. Underflow occurs during POP when stack is empty. d) TRUE. In postfix notation, operators come after their operands.
Explanation:
a) Stack elements are arranged linearly. b) Stack follows LIFO, so statement is false. c) PUSH adds element; underflow occurs on POP when empty. d) Postfix places operators after operands.
Q7.2. Find the output of the following code: a) result=0 numberList=[10,20,30] numberList.append(40) result=result+numberList.pop() result=result+numberList.pop() print("Result=", result) b) answer=[]; output="" answer.append('T') answer.append('A') answer.append('M') ch=answer.pop() output=output+ch ch=answer.pop() output=output+ch ch=answer.pop() output=output+ch print("Result=", output)
Answer:
a) Initially, result=0, numberList=[10,20,30] numberList.append(40) makes numberList=[10,20,30,40] numberList.pop() removes 40 and returns it, so result=0+40=40 numberList.pop() removes 30 and returns it, so result=40+30=70 Output: Result= 70 b) answer=[]; output="" answer.append('T') -> ['T'] answer.append('A') -> ['T','A'] answer.append('M') -> ['T','A','M'] ch=answer.pop() -> 'M', output=""+"M"="M" ch=answer.pop() -> 'A', output="M"+"A"="MA" ch=answer.pop() -> 'T', output="MA"+"T"="MAT" Output: Result= MAT
Explanation:
a) The pop() method removes the last element added (LIFO). Adding 40, then popping twice removes 40 and 30. b) Stack operations append and pop reverse the order of letters, so output is 'MAT'.
Q8.3. Write a program to reverse a string using stack.
Answer:
Program to reverse a string using stack: ```python # Function to reverse string using stack def reverse_string(s): stack = [] # Push all characters to stack for char in s: stack.append(char) # Pop all characters from stack and form reversed string reversed_str = '' while stack: reversed_str += stack.pop() return reversed_str # Example usage input_str = 'HELLO' print('Original String:', input_str) print('Reversed String:', reverse_string(input_str)) ``` Output: Original String: HELLO Reversed String: OLLEH
Explanation:
The program uses a list as a stack. Each character of the string is pushed onto the stack. Then characters are popped from the stack to form the reversed string, demonstrating LIFO behavior.
All 13 Chapters in Computer Science
Computer Science · Class 12