JS - Linear Data Structures 02
Linear Data Structures
Queues
- A queue is a linear collection of nodes that exclusively
- adds (enqueues) nodes to the tail,
- removes (dequeues) nodes from the head of the queue
- peek (reveals) data from the “front” of the queue without removing it
- FIFO: first-in-first-out
- Bounded queues have a limited size.
- Enqueueing onto a full queue causes a queue overflow
- Dequeueing from an empty queue causes a queue underflow
Queues Implementation
- Queues can be implemented using a linked list as the underlying data structure.
- The front of the queue is equivalent to the head node of a linked list.
- The back of the queue is equivalent to the tail node.
Underflow & Overflow
- Underflow occurs when we try to remove elements from an already empty queue.
- Overflow occurs when we add an element to a queue that does not have room for a new node.
- Creating bounded queues with a .maxSize property
- prevents queue overflow and underflow by keeping track of the queue size
Stacks
- Like a queue, a stack is a linear collection of nodes that adds (pushes) data to one end of the data structure.
- However, unlike a queue, a stack removes data (pops) from the same end of the data structure.
- First In, Last Out (FILO)