0% found this document useful (0 votes)
6 views10 pages

Stack

A stack is a linear data structure that operates on a Last In First Out (LIFO) principle, where the last element added is the first to be removed. There are two types of stacks: fixed size, which has a predefined capacity, and dynamic size, which can grow and shrink as needed. Common operations include push, pop, top, isEmpty, and size, with real-life examples such as plates in a cafeteria and function calls in programming.

Uploaded by

Nafisa s
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Stack

A stack is a linear data structure that operates on a Last In First Out (LIFO) principle, where the last element added is the first to be removed. There are two types of stacks: fixed size, which has a predefined capacity, and dynamic size, which can grow and shrink as needed. Common operations include push, pop, top, isEmpty, and size, with real-life examples such as plates in a cafeteria and function calls in programming.

Uploaded by

Nafisa s
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Stack

Stack
 Stack is a linear data structure that follows LIFO (Last In First Out) Principle,

the last element inserted is the first to be popped out. It means both

insertion and deletion operations happen at one end only.


LIFO(Last In First Out) Principle

 The LIFO principle means that the last element added to a stack is the first
one to be removed.
 New elements are always pushed on top.
 Removal (pop) also happens only from the top.
 This ensures a strict order: last in → first out.
Types of Stack:

 Fixed Size Stack


 A fixed size stack has a predefined capacity.
 Once it becomes full, no more elements can be added (this causes overflow).
 If the stack is empty and we try to remove an element, it causes underflow.
 Typically implemented using a static array.

 Example: Declaring a stack of size 10 using an array


 Dynamic Size Stack
 A dynamic size stack can grow and shrink automatically as needed.
 If the stack is full, its capacity expands to allow more elements.
 As elements are removed, memory usage can shrink as well.
 Can be implemented using:
-> Linked List → grows/shrinks naturally.

 Example: Stack implementation using linked list or resizable array.


Common Operations on Stack:

 push() to insert an element into the stack.


 pop() to remove an element from the stack.
 top() Returns the top element of the stack.
 isEmpty() returns true if stack is empty else false.
 size() returns the size of the stack.
Real-life examples of a Stack
 Plates in a cafeteria
 You place plates one on top of another.
 The last plate placed is the first one removed.

 Books stacked on a table


 You remove the top book first.
 You can’t easily remove a book from the middle.

 Call stack in programming


 When a function is called, it’s placed on the stack.
 When the function finishes, it’s removed.
 Very important in recursion.
Where stacks are used in computer science

Checking balanced parentheses


 A stack is used to check whether brackets are properly opened and closed.
 Opening brackets are pushed onto the stack.
 Closing brackets are matched and popped from the stack.
 If the stack is empty at the end → expression is balanced.
 📌 Example:
 (a + b) → Balanced ✔
 (a + b] → Not balanced ✖
 Function calls and recursion
 When a function is called, its details are pushed onto the call stack.
 When the function finishes, it is popped from the stack.
 This allows:
 Programs to return to the correct place
 Recursive functions to work properly
📌 Example:
 main() → fun1() → fun2()
 fun2() finishes first (LIFO)
 Undo / Redo operations

 Each user action is stored in a stack.

 The most recent action is undone first.

 Redo can be managed using another stack.

 📌 Example:
Typing → Delete → Format
Undo removes Format first.

You might also like