Read 10 ~ Stacks & Queues
By Abdallah obaid
NAME | URL |
---|---|
Home | Home. |
Prep | Prep: Engineering Topics. |
Read 01 | Node Ecosystem, TDD, CI/CD. |
Read 02 | Classes, Inheritance, Functional. |
Read 03 | Data Modeling & NoSQL Databases. |
Read 04 | Advanced Mongo/Mongoose. |
Read 05 | Linked Lists. |
Read 06 | HTTP and REST. |
Read 07 | Express. |
Read 08 | Express Routing & Connected API. |
Read 09 | API Server. |
Read 10 | Stacks and Queues. |
Read 11 | Authentication. |
Read 12 | OAuth. |
Read 13 | Bearer Authorization. |
Read 14 | Access Control (ACL). |
Read 15 | Trees. |
Read 16 | Event Driven Applications. |
Stacks & Queues
## Stacks:
- Stack is a linear data structure, consists of Nodes. Each Node references the next Node, but does not refer to previous node.
- Stack follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).
- Common terminology for a stack is:
- Push: to add node.
-
Pop: remove node if empty return exception. - Top: top of the stack.
-
Peek: used to view the top if empty return exception. - IsEmpty: returns true when stack is empty otherwise returns false.
- When you push something to the stack, it becomes the new top.
- Pop remove Node from the top.
- The Big O for bush is O(1), it takes the same amount of time no matter how many Nodes (n)
- When we try to use pop we should check before if the stack empty or not with isEmpty.
- Peek can just check for the top node.
- With peek we need also to check if stack isEmpty to avoid the exception.
## Queue:
- Its also a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).
- Common terminology for a queue is:
- Enqueue: Nodes or items that are added to the queue.
- Dequeue: Nodes or items that are removed from the queue. If called when the queue is empty an exception will be raised.
- Front: This is the front/first Node of the queue.
- Rear: This is the rear/last Node of the queue.
- Peek: When you peek you will view the value of the front Node in the queue. If called when the queue is empty an exception will be raised.
- IsEmpty: returns true when queue is empty otherwise returns false.