It can be hard to distinguish whether something is truly hard when it is new. Have patience.
None today. Just pre-reading for the lecture.
Stacks and queues can be built either with linked lists or arrays. Stacks and queues are usually put together as topics to learn because they are similar.
They are linear data structures (like an array or linked list). What makes them different are some critical rules about how to use them.
- Last-in, first-out: LIFO policy
This is easily visualized by thinking of a stack of pancakes. The last pancake on the plate goes on top and will be removed first.
In terms of computing, an example is the call stack
. A call stack stores information in the active subroutines of a computer program
Indeed, by now, you have received an error message in NodeJS.
This stack trace shows where the error happened (in this case, in the file sort2.js
on line 239). Below is a trace of the other functions that were called to execute this line of code.
Perhaps, you have also accidentally created an infinite loop while working on a recursive solution. Then you likely would have seen a message like Maximum call stack size exceeded
or stack overflow
.
There tends to be a specific terminology for specific actions with a stack.
- insert: push
- delete: pop
- First-in, first-out: FIFO policy
This is also readily visualized by thinking of a line of people at a checkout register. The first person who stood in line will be the first one to leave the line to be rung up.
In terms of computing, everyday use is for asynchronous data transfers. You may have heard of io buffers
(io
- input/output), pipes
, streams
, or file io
. The applications we've been building have handled this logic for us.
There tends to be a specific terminology for specific actions with a stack.
- insert : enqueue
- delete : dequeue
Now that we understand the general principles of these two data structures, the next task is to code examples of them.
It would be best if you worked through several problems to understand Stacks and Queues thoroughly. You can find the Codewars problems linked here on the Fellow Tracker under your name card.
From Codewars:
Additional problems to practice working with Stacks:
Hacker Rank:
Codesignal: