PPT: Stacks & Queues | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

Download, print and study this document offline
Please wait while the PDF view is loading
 Page 1


Stacks and 
Queues
Page 2


Stacks and 
Queues
Introduction to Stacks and Queues
Stack (LIFO)
A linear data structure 
following the Last In, First 
Out principle, where 
elements are added and 
removed from the same 
end.
Queue (FIFO)
A linear data structure 
following the First In, First 
Out principle, where 
elements are added at one 
end and removed from the 
other.
Key Characteristics
Both store elements in a 
specific order with 
operations restricted to 
specific ends of the 
structure.
Stacks are essential for function call management and expression evaluation, while queues 
excel in task scheduling and resource sharing cases.
Page 3


Stacks and 
Queues
Introduction to Stacks and Queues
Stack (LIFO)
A linear data structure 
following the Last In, First 
Out principle, where 
elements are added and 
removed from the same 
end.
Queue (FIFO)
A linear data structure 
following the First In, First 
Out principle, where 
elements are added at one 
end and removed from the 
other.
Key Characteristics
Both store elements in a 
specific order with 
operations restricted to 
specific ends of the 
structure.
Stacks are essential for function call management and expression evaluation, while queues 
excel in task scheduling and resource sharing cases.
Stack - Concept and 
Operations
Push
Adds an element to the 
top of the stack
Pop
Removes and returns the 
top element
Peek/Top
Returns the top element 
without removing it
IsEmpty/IsFull
Checks if the stack is 
empty or full
The defining property of a stack is its LIFO (Last In, First Out) 
behavior - the last element added is always the first to be removed. 
All primary stack operations have a time complexity of O(1).
For example, if we Push(3), Push(5), and then Pop(), we'll get 5 back 
and our stack will contain only [3].
Page 4


Stacks and 
Queues
Introduction to Stacks and Queues
Stack (LIFO)
A linear data structure 
following the Last In, First 
Out principle, where 
elements are added and 
removed from the same 
end.
Queue (FIFO)
A linear data structure 
following the First In, First 
Out principle, where 
elements are added at one 
end and removed from the 
other.
Key Characteristics
Both store elements in a 
specific order with 
operations restricted to 
specific ends of the 
structure.
Stacks are essential for function call management and expression evaluation, while queues 
excel in task scheduling and resource sharing cases.
Stack - Concept and 
Operations
Push
Adds an element to the 
top of the stack
Pop
Removes and returns the 
top element
Peek/Top
Returns the top element 
without removing it
IsEmpty/IsFull
Checks if the stack is 
empty or full
The defining property of a stack is its LIFO (Last In, First Out) 
behavior - the last element added is always the first to be removed. 
All primary stack operations have a time complexity of O(1).
For example, if we Push(3), Push(5), and then Pop(), we'll get 5 back 
and our stack will contain only [3].
Stack Implementation
Array-Based Implementation
Uses an array with a fixed or dynamic size and 
maintains a t o p pointer to track the top 
element. Push operation increments top and 
adds the element, while Pop returns the 
element at top and decrements the pointer.
A d v a n t a g e s : Simple and memory-efficient
D i s a d v a n t a g e s : Fixed size with risk of 
overflow
Linked List Implementation
Uses a singly linked list where the top is the 
head. Push operation adds a node at the head 
(O(1)), and Pop removes the head node (O(1)).
A d v a n t a g e s : Dynamic size with no overflow 
risk
D i s a d v a n t a g e s : Extra memory required for 
pointers
Page 5


Stacks and 
Queues
Introduction to Stacks and Queues
Stack (LIFO)
A linear data structure 
following the Last In, First 
Out principle, where 
elements are added and 
removed from the same 
end.
Queue (FIFO)
A linear data structure 
following the First In, First 
Out principle, where 
elements are added at one 
end and removed from the 
other.
Key Characteristics
Both store elements in a 
specific order with 
operations restricted to 
specific ends of the 
structure.
Stacks are essential for function call management and expression evaluation, while queues 
excel in task scheduling and resource sharing cases.
Stack - Concept and 
Operations
Push
Adds an element to the 
top of the stack
Pop
Removes and returns the 
top element
Peek/Top
Returns the top element 
without removing it
IsEmpty/IsFull
Checks if the stack is 
empty or full
The defining property of a stack is its LIFO (Last In, First Out) 
behavior - the last element added is always the first to be removed. 
All primary stack operations have a time complexity of O(1).
For example, if we Push(3), Push(5), and then Pop(), we'll get 5 back 
and our stack will contain only [3].
Stack Implementation
Array-Based Implementation
Uses an array with a fixed or dynamic size and 
maintains a t o p pointer to track the top 
element. Push operation increments top and 
adds the element, while Pop returns the 
element at top and decrements the pointer.
A d v a n t a g e s : Simple and memory-efficient
D i s a d v a n t a g e s : Fixed size with risk of 
overflow
Linked List Implementation
Uses a singly linked list where the top is the 
head. Push operation adds a node at the head 
(O(1)), and Pop removes the head node (O(1)).
A d v a n t a g e s : Dynamic size with no overflow 
risk
D i s a d v a n t a g e s : Extra memory required for 
pointers
Applications of Stacks
Function Call 
Management
Stores function call details 
including return addresses 
and local variables, 
managed by the system 
call stack
Expression Evaluation
Converts infix to postfix 
expressions (e.g., A+B*C ³ 
ABC*+) and evaluates 
postfix expressions (e.g., 
23*4+ ³ 10)
Backtracking
Used in algorithms like 
depth-first search (DFS) 
and maze solving
Undo Operations
Stores states for reverting actions in applications like text editors
Read More
158 docs|31 tests

FAQs on PPT: Stacks & Queues - Programming and Data Structures - Computer Science Engineering (CSE)

1. What are stacks and queues in computer science?
Ans.Stacks and queues are abstract data types used to store collections of elements. A stack follows the Last In First Out (LIFO) principle, where the last element added is the first to be removed. In contrast, a queue follows the First In First Out (FIFO) principle, where the first element added is the first to be removed. Both structures are fundamental in various applications, including algorithm design and managing resources.
2. What are the primary operations associated with stacks and queues?
Ans.The primary operations for a stack include push (adding an element) and pop (removing the top element). Additionally, there is a peek operation to view the top element without removing it. For queues, the main operations are enqueue (adding an element to the back) and dequeue (removing an element from the front). There is also a front operation to view the first element without removing it.
3. In what scenarios would you use a stack instead of a queue, and vice versa?
Ans.Stacks are typically used in scenarios where you need to reverse items or manage function calls, such as in recursive algorithms. Queues are preferred in situations requiring order preservation, like task scheduling, where tasks are processed in the order they arrive. Each structure serves distinct purposes based on the specific needs of the application.
4. Can you explain the differences between dynamic and static implementations of stacks and queues?
Ans.Dynamic implementations of stacks and queues use linked lists, allowing for variable sizes and efficient memory usage, as they grow and shrink as needed. Static implementations, on the other hand, use fixed-size arrays, which can lead to wasted space or overflow if the capacity is exceeded. Each approach has its advantages and trade-offs in terms of performance and memory management.
5. How are stacks and queues utilized in programming languages and algorithms?
Ans.Stacks and queues are widely used in programming languages for managing data flow and controlling execution. For example, stacks are essential in parsing expressions and tracking function calls, while queues are commonly used in breadth-first search algorithms and managing print jobs in operating systems. Their efficient data handling capabilities make them integral to many algorithms and data processing tasks.
Related Searches

MCQs

,

practice quizzes

,

shortcuts and tricks

,

study material

,

Objective type Questions

,

ppt

,

Important questions

,

PPT: Stacks & Queues | Programming and Data Structures - Computer Science Engineering (CSE)

,

past year papers

,

Extra Questions

,

Summary

,

Exam

,

PPT: Stacks & Queues | Programming and Data Structures - Computer Science Engineering (CSE)

,

PPT: Stacks & Queues | Programming and Data Structures - Computer Science Engineering (CSE)

,

Semester Notes

,

Free

,

Viva Questions

,

Sample Paper

,

mock tests for examination

,

Previous Year Questions with Solutions

,

pdf

,

video lectures

;