0% found this document useful (0 votes)
19 views20 pages

DSA Lesson 4

Uploaded by

rolandoarjona0
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)
19 views20 pages

DSA Lesson 4

Uploaded by

rolandoarjona0
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
You are on page 1/ 20

CC104

Data Structures and Algorithms

Instructor: Gerald John M. Sotto


Last Updated: 7th of October, 2024
TABLE OF CONTENTS

LINEAR DATA STRUCTURES


Stacks: Characteristics, Operations, and Java Implementations
Queues: Characteristics, Operations, and Java Implementations
STACKS:
CHARACTERISTICS,
OPERATIONS, AND JAVA
IMPLEMENTATIONS
Stacks
Characteristics:

A stack is a linear data structure that follows the Last In, First Out (LIFO) principle,
where elements are added and removed from the same end.
While they are often implemented using arrays or linked lists, the concept of a stack
itself is defined by its behavior: Last-In, First-Out (LIFO). This characteristic makes it a
distinct data type, separate from the underlying implementation.
In essence, a stack is an abstract data type (ADT) that specifies the operations that
can be performed on it and the behavior of those operations, regardless of how it's
implemented.
The actual implementation (using arrays or linked lists) is a choice made by the
programmer to achieve the desired performance characteristics or fit specific use
cases.
Stacks
Stacks
Operations:

Stacks are a type of data structure that follow the Last-In-First-Out (LIFO) principle.
This means that the last element added to the stack is the first one to be removed.
1. Push: Adds an element to the top of the stack.
2. Pop: Removes and returns the top element from the stack.
3. Peek: Returns the top element of the stack without removing it.
4. IsEmpty: Checks if the stack is empty.
Stacks
Operations:
1. Push: Adds an element to the top of the stack.
2. Pop: Removes and returns the top element from the stack.
3. Peek: Returns the top element of the stack without removing it.
4. IsEmpty: Checks if the stack is empty.
Stacks
Common errors:
1. Stack Overflow: Attempting to push an element onto a full stack.
2. Stack Underflow: Attempting to pop an element from an empty stack.
3. Incorrect Implementation of Operations: Incorrect implementation of
the push, pop, peek, or isEmpty operations.
Stacks

Discussion via Code


Stacks
Implementation:
1. Built-in Stack class: Quick to use, but inherits overhead from Vector.
2. Array-based implementation: Fixed size, may cause overflow.
3. Linked List implementation: Dynamic size, no overflow but more memory overhead.

Use Cases:
1. Undo/Redo Functionality: Stacks can be used to store commands that can be
undone or redone. When an action is performed, it's pushed onto a stack. To undo the
action, the top command is popped and reversed.
2. Backtracking Algorithms: Stacks are used to keep track of the current state of a
backtracking algorithm. When a dead-end is reached, the algorithm can backtrack by
popping the top element from the stack.
3. Web Browsers: Stacks are used to manage the history of visited web pages. When a
user clicks the "Back" button, the browser pops the top URL from the stack and
navigates to that page.
QUEUES:
CHARACTERISTICS,
OPERATIONS, AND JAVA
IMPLEMENTATIONS
Queues
Characteristics:

A queue is a linear data structure that follows the First In First Out (FIFO) principle.
The specific implementation of a queue (e.g., using an array or linked list) can vary,
but as long as it adheres to these operations and the FIFO principle, it is considered a
queue.
In essence, a queues is an abstract data type (ADT) that specifies the operations that
can be performed on it and the behavior of those operations, regardless of how it's
implemented.
The actual implementation (using arrays or linked lists) is a choice made by the
programmer to achieve the desired performance characteristics or fit specific use
cases.
Queues
Queues
Operations:

An ADT defines a set of operations that can be performed on a data structure,


without specifying the underlying implementation. In the case of queues, the
operations include:
1. Enqueue: Add an element to the end of the queue.
2. Dequeue: Remove the element from the front of the queue.
3. Front/Peek: Retrieve the front element without removing it.
Queues
Operations:
1. Enqueue: Add an element to the end of the queue.
2. Dequeue: Remove the element from the front of the queue.
3. Front/Peek: Retrieve the front element without removing it.
Queues
Implementation:
1. Built-in Queue interface: Easy to use, flexible options for underlying implementation.
2. Array-based implementation: Fixed size, may cause overflow.
3. Linked List implementation: Dynamic size, no overflow but more memory overhead.

Use Cases:
1. Task Scheduling: Queues are used to manage tasks waiting for execution. The tasks
are added to the queue in the order they arrive (FIFO), and the scheduler processes
them from the front of the queue.
2. Print Spooler: Queues are used to manage print jobs. Jobs are added to the queue in
the order they are received, and the printer processes them one by one.
3. Breadth-First Search (BFS): Queues are used to implement BFS algorithms, which are
used to traverse graphs or trees level by level.
Queues
Common errors:
1. Enqueue and Dequeue Mismatch: Incorrectly using enqueue and
dequeue operations, leading to inconsistent queue behavior.
2. Null Pointer Exceptions: Attempting to access elements in a null queue
or when the queue is empty.
3. Index Out of Bounds: Incorrectly accessing elements in the underlying
data structure (e.g., array or linked list).
4. Circular Buffer Overflow: Using a fixed-size circular buffer
implementation and attempting to add elements when the buffer is full.
Queues

Discussion via Code


Questions?
Clarifications?
End of Presentation

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy