You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contents/stacks_and_queues/stacks_and_queues.md
+9-3Lines changed: 9 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -2,18 +2,20 @@
2
2
3
3
Stacks and Queues are two sides of the same coin in computer science. They are both simple data structures that hold multiple elements, but allow you to use a single element at a time. The biggest difference between the two structures is the order in which you can access the elements in the data structure.
4
4
5
-
In *stacks*, data follows *Last In, First Out* (LIFO), which basically means that whichever element you put in last will be the first element you take out. It acts exactly like a stack in real life. If you put a book on a stack of other books, the first book you will look at when sifting through the stack will be the book you just put on the stack.
5
+
In _stacks_, data follows _Last In, First Out_ (LIFO), which basically means that whichever element you put in last will be the first element you take out. It acts exactly like a stack in real life. If you put a book on a stack of other books, the first book you will look at when sifting through the stack will be the book you just put on the stack.
6
6
7
-
In *Queues*, data follows *First In, First Out* (FIFO), which means that whichever element you put in first will be the first element you take out. Imagine a queue of people. It would be unfair if the first person in line for groceries were not the first person to receive attention once the attendant finally shows up.
7
+
In _Queues_, data follows _First In, First Out_ (FIFO), which means that whichever element you put in first will be the first element you take out. Imagine a queue of people. It would be unfair if the first person in line for groceries were not the first person to receive attention once the attendant finally shows up.
8
8
9
9
For the most part, though, queues and stacks are treated the same way. There must be a way to:
10
+
10
11
1. look at the first element (`top()`)
11
12
2. to remove the first element (`pop()`)
12
13
3. to push elements onto the data structure (`push()`)
13
14
14
15
The notation for this depends on the language you are using. Queues, for example, will often use `dequeue()` instead of `pop()` and `front()` instead of `top()`. You will see the language-specific details in the source code under the algorithms in this book, so for now it's simply important to know what stacks and queues are and how to access elements held within them.
15
16
16
17
## Example Code
18
+
17
19
Here is a simple implementation of a stack:
18
20
{% method %}
19
21
{% sample lang="ts" %}
@@ -24,6 +26,8 @@ Here is a simple implementation of a stack:
24
26
[import, lang:"cpp"](code/cpp/stack.cpp)
25
27
{% sample lang="rust" %}
26
28
[import, lang:"rust"](code/rust/Stack.rs)
29
+
{% sample lang="python" %}
30
+
[import, lang:"python"](code/python/stack.py)
27
31
{% endmethod %}
28
32
29
33
Here is a simple implementation of a queue:
@@ -36,9 +40,10 @@ Here is a simple implementation of a queue:
36
40
[import, lang:"cpp"](code/cpp/queue.cpp)
37
41
{% sample lang="rust" %}
38
42
[import, lang:"rust" ](code/rust/Queue.rs)
43
+
{% sample lang="python" %}
44
+
[import, lang:"python"](code/python/queue.py)
39
45
{% endmethod %}
40
46
41
-
42
47
## License
43
48
44
49
##### Code Examples
@@ -54,4 +59,5 @@ The text of this chapter was written by [James Schloss](https://github.com/leios
54
59
##### Pull Requests
55
60
56
61
After initial licensing ([#560](https://github.com/algorithm-archivists/algorithm-archive/pull/560)), the following pull requests have modified the text or graphics of this chapter:
0 commit comments