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
+7-1
Original file line number
Diff line number
Diff line change
@@ -7,13 +7,15 @@ In *stacks*, data follows *Last In, First Out* (LIFO), which basically means tha
7
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