0% found this document useful (0 votes)
4 views30 pages

Understanding Stacks and Queues

The document provides an introduction to linear data structures, focusing on Stacks (LIFO) and Queues (FIFO), detailing their definitions, operations, implementations, and applications. It highlights the advantages and limitations of each structure, as well as their real-life examples and error handling. Understanding Stacks and Queues is essential for task management and programming logic.

Uploaded by

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

Understanding Stacks and Queues

The document provides an introduction to linear data structures, focusing on Stacks (LIFO) and Queues (FIFO), detailing their definitions, operations, implementations, and applications. It highlights the advantages and limitations of each structure, as well as their real-life examples and error handling. Understanding Stacks and Queues is essential for task management and programming logic.

Uploaded by

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

Introduction to Linear Data

Structures
• Linear Data Structures store data sequentially.
• Examples: Arrays, Linked Lists, Stacks, Queues.
• Focus: Stack (LIFO) and Queue (FIFO).
What is a Stack?
• A Stack is a linear data structure following
Last-In-First-Out (LIFO).
• Real-life Example: Stack of plates.
• Operations: PUSH, POP, PEEK.
Stack Operations Explained
• PUSH: Add element at the top.
• POP: Remove element from the top.
• PEEK: View top element without removing.
Stack Diagram
• Example:
• TOP → 30
• 20
• 10
• After PUSH(40): TOP → 40
• After POP(): 40 removed → TOP → 30
Stack Implementation (Array)
• #define MAX 10
• int stack[MAX]; int top = -1;
• void push(int item){...}
• void pop(){...}
Stack Characteristics
• Linear structure, operations on one end, LIFO
principle.
• Efficient for recursion and expression
evaluation.
Applications of Stack
• Undo/Redo feature, String reversal, Syntax
checking, Function calls, Expression
evaluation.
Advantages and Limitations of
Stack
• Advantages: Simple, fast, efficient for
recursion.
• Limitations: Fixed size (in arrays), Stack
overflow/underflow.
What is a Queue?
• Queue is a linear structure following First-In-
First-Out (FIFO).
• Real-life Example: People waiting in line.
• Operations: ENQUEUE, DEQUEUE.
Queue Operations Explained
• ENQUEUE: Insert element at rear.
• DEQUEUE: Remove element from front.
• FRONT: Points to first element.
• REAR: Points to last element.
Queue Diagram
• FRONT → [10] [20] [30] [40] ← REAR
• ENQUEUE(50) adds at rear.
• DEQUEUE() removes from front.
Queue Implementation (Array)
• #define MAX 5
• int queue[MAX]; int front=-1,rear=-1;
• void enqueue(int val){...}
• void dequeue(){...}
Types of Queues
• 1. Simple Queue – FIFO order.
• 2. Circular Queue – Last connects to first.
• 3. Priority Queue – Based on priority.
• 4. Deque – Both ends accessible.
Applications of Queue
• CPU scheduling, Disk scheduling, Data transfer,
Print buffer, Call center systems.
Advantages and Limitations of
Queue
• Advantages: Orderly processing, efficient
scheduling.
• Limitations: Fixed size in arrays, possible
wasted space.
Stack vs Queue
• Stack (LIFO) vs Queue (FIFO):
• Insertion – Top vs Rear
• Deletion – Top vs Front
• Example – Plates vs Ticket line.
Stack in Real Life
• Plates, Books, Undo feature, Recursion calls.
Queue in Real Life
• Bank counters, Ticket lines, CPU task
management.
Stack Implementation Using Linked
List
• Dynamic memory allocation, No overflow
unless memory full.
• Node structure: data + pointer to next node.
Queue Implementation Using
Linked List
• Dynamic memory allocation.
• Node structure: data + next pointer.
• Useful for dynamic queues like CPU tasks.
Circular Queue Explanation
• Connects rear to front.
• Avoids wasted space.
• Example: Round robin scheduling.
Priority Queue Explanation
• Elements served based on priority.
• Higher priority processed first.
Deque Explanation
• Double-ended queue – insertion and deletion
possible from both ends.
Applications in Operating Systems
• Stack – function calls, recursion.
• Queue – scheduling, buffering.
Error Handling in Stack/Queue
• Overflow – trying to add in full structure.
• Underflow – trying to remove from empty
structure.
Complexity of Stack and Queue
• Both have O(1) for push/pop or
enqueue/dequeue.
• Memory usage depends on array or linked list
type.
Comparison Summary
• Stack – LIFO, single end.
• Queue – FIFO, both ends.
• Both fundamental for data handling.
Quiz Time!
• 1. What principle does Stack follow?
• 2. What operation removes the first item in
Queue?
• 3. What causes Stack Overflow?
• 4. Give one real-life example of each.
Summary
• Stacks and Queues are core linear data
structures.
• Help in task management, recursion,
scheduling.
• Understanding them builds programming
logic.
Thank You
• Prepared for Non-IT Students
• Topic: Stack and Queue
• Based on [Link]

You might also like