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
<1> We initialize two variables `current` to the first node and `position` to keep track of the index.
87
84
<2> While `current` node is not null we keep going.
88
85
<3> On each loop we move to the next node and increment the index.
89
-
<4> We invoke the callback passing the current position and node. If the callback returns something then we stop and return that value.
86
+
<4> We invoke the callback passing the current position and node. If the callback returns something, then we stop and return that value.
90
87
<5> Return whatever result we got from the callback. E.g., we can return the index or the node itself or any other calculation.
91
88
92
89
We are going to use this `find` method again to implement searching by index.
@@ -268,4 +265,4 @@ Use a doubly linked list when:
268
265
* You want to insert elements at the start and end of the list. The linked list has O(1) while array has O(n).
269
266
* You want to save some memory when dealing with possibly large data sets. Arrays pre-allocate a large chunk of contiguous memory on initialization. Lists are more "grow as you go".
270
267
271
-
For the next two linear data structures <<Stack>> and <<Queue>>, we are going to use doubly linked list to implement them. We could use an array as well, but since inserting/deleting from the start perform better on linked-list, we are going use that.
268
+
For the next two linear data structures <<Stack>> and <<Queue>>, we are going to use a doubly linked list to implement them. We could use an array as well, but since inserting/deleting from the start perform better on linked-list, we are going use that.
Copy file name to clipboardExpand all lines: book/chapters/queue.adoc
+21-18Lines changed: 21 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,15 @@
1
1
= Queue
2
2
3
-
Queue is a linear data structure where the data flows in a *First-In-First-Out* (FIFO) manner.
3
+
A queue is a linear data structure where the data flows in a *First-In-First-Out* (FIFO) manner.
4
4
5
5
.Queue data structure is like a line of people: the First-in, is the First-out
6
6
image:image30.png[image,width=528,height=171]
7
7
8
+
A queue is like a line of people at the bank, the person that arrived first is the first to go out as well.
8
9
9
-
Queue is like a line of people, the first one to get in the line is the first out as well. Similar to the stack, we only have to operations. In a Queue, we insert elements to the back of the list and remove it from the front.
10
+
Similar to the stack, we only have two operations (insert and remove). In a Queue, we add elements to the back of the list and remove it from the front.
10
11
11
-
We could use an array or a linked list to implement a Queue. However, is recommended to only use linked list. An array has a runtime of O(n) to remove element from the start while a list is constant O(1).
12
+
We could use an array or a linked list to implement a Queue. However, it is recommended only to use a linked list. Why? An array has a linear runtime _O(n)_ to remove an element from the start while a linked list has constant time _O(1)_.
You can see that the items are dequeue in the same order they were added. For this operation we use the Linked List `removeFirst` method.
58
+
You can see that the items are dequeue in the same order they were added, FIFO (first-in, first out).
59
59
60
60
== Queue Complexity
61
61
62
-
A mode of experiment we can see in the following table that if we would have implemented the Queue using an array it’s enqueue time would be O(n) instead of O(1). Check it out.
62
+
As an experiment, we can see in the following table that if we had implemented the Queue using an array, its enqueue time would be _O(n)_ instead of _O(1)_. Check it out:
63
63
64
64
65
65
.Time complexity for queue operations
@@ -73,23 +73,26 @@ A mode of experiment we can see in the following table that if we would have imp
73
73
74
74
= Summary
75
75
76
-
In this chapter we explored the most used linear data structures such as Arrays, Lists, Stacks and Queues. We implemented them and discussed the runtime of their operations.
76
+
In this chapter, we explored the most used linear data structures such as Arrays, Linked Lists, Stacks and Queues. We implemented them and discussed the runtime of their operations.
77
77
78
78
To sum up,
79
79
80
80
.Use Arrays when...
81
81
* You need to access data in random order fast (using an index).
82
-
* Your data is multi-dimensional (e.g. matrix, tensor).
82
+
* Your data is multi-dimensional (e.g., matrix, tensor).
83
83
84
-
.Use Linked Lists when...
84
+
.Use Linked Lists when:
85
85
* You will access your data sequentially.
86
86
* You want to save memory and only allocate memory as you need it.
87
+
* You want constant time to remove/add from extremes of the list.
87
88
88
-
.Use Queues when...
89
-
* You need to access your data in a first-come, first-served basis.
89
+
.Use a Queue when:
90
+
* You need to access your data in a first-come, first served basis (FIFO).
91
+
* You need to implement a <<Breadth First Search>>
90
92
91
-
.Use Stacks when...
92
-
* You need to access your data first-in, last-out (FIFO)
93
+
.Use a Stack when:
94
+
* You need to access your data as last-in, first-out (LIFO).
95
+
* You need to implement a <<Depth First Search>>
93
96
94
97
.Time Complexity of Linear Data Structures (Array, LinkedList, Stack & Queues)
A queue is a linear data structure where the data flows in a *First-In-First-Out* (FIFO) manner.
3
+
The stack is a data structure that restricts the way you add and remove data. It only allows you to insert and retrieve in a *Last-In-First-Out* (LIFO) fashion.
4
4
5
-
.Queue data structure is like a line of people: the First-in, is the First-out
6
-
image:image30.png[image,width=528,height=171]
5
+
An analogy is to think the stack is a rod and the data are discs. You can only take out the last one you put in.
7
6
8
-
A queue is like a line of people at the bank, the person that arrived first is the first to go out as well.
7
+
.Stack data structure is like a stack of disks: the last element in is the first element out
8
+
image:image29.png[image,width=240,height=238]
9
9
10
-
Similar to the stack, we only have two operations (insert and remove). In a Queue, we add elements to the back of the list and remove it from the front.
10
+
// #Change image from https://www.khanacademy.org/computing/computer-science/algorithms/towers-of-hanoi/a/towers-of-hanoi[Khan Academy]#
11
11
12
-
We could use an array or a linked list to implement a Queue. However, it is recommended only to use linked list. Why? An array has a linear runtime _O(n)_ to remove an element from the start while a linked list has constant time _O(1)_.
12
+
As you can see in the image above, If you insert the disks in the order `5`, `4`, `3`, `2`, `1`. Then you can remove them on `1`, `2`, `3`, `4`, `5`.
13
13
14
-
.Queue's constructor
14
+
The stack inserts items to the end of the collection and also removes from the end. Both, an array and linked list would do it in constant time. However, since we don’t need the Array’s random access, a linked list makes more sense.
As discussed, this operation has a constant runtime.
50
+
This time we used the linked list’s `removeLast` method. That’s all we need for a stack implementation. Check out the full implementation https://github.com/amejiarosario/algorithms.js/blob/master/src/data-structures/stacks/stack.js[here]
You can see that the items are dequeue in the same order they were added, FIFO (first-in, first-out).
62
+
As you can see if we add new items they will be the first to go out to honor LIFO.
59
63
60
-
== Queue Complexity
64
+
== Stack Complexity
61
65
62
-
As an experiment, we can see in the following table that if we had implemented the Queue using an array, its enqueue time would be _O(n)_ instead of _O(1)_. Check it out:
66
+
Implementing the stack with an array and linked list would lead to the same time complexity:
63
67
64
-
65
-
.Time complexity for queue operations
68
+
.Time complexity for the stack operations
66
69
|===
67
70
.2+.^s| Data Structure 2+^s| Searching By 3+^s| Inserting at the 3+^s| Deleting from .2+.^s| Space Complexity
It's not very common to search for values on a stack (other Data Structures are better suited for this). Stacks especially useful for implementing <<Depth First Search>>.
0 commit comments