summaryrefslogtreecommitdiff
path: root/04-27.md
blob: 5d06ee1293061a328cbbb3a9a27e22b27a13fe34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
[\<- 04/24](04-24.md)

---

## Start

- Today: Ch. 3+4
- You are responsible for section 3.1
- You are responsible for section 4.1
- In other words, just the concepts of stacks and queues

## List

- A **list** is an ordered collection of items, which are not necessarily distinct. Ordered does not necessarily mean sorted, rather that there is a 1st, 2nd, ... ith, ... nth etc.
- Two important types of lists are:
	1. stacks
	2. queues

- We can look up items by position (indexing) on a list. Sometimes also by name/id/key, but position is usually more important

## Stack - Concept

- Concept:
	- A **stack** is a **linear list** in which all additions and deletions are restricted to one end, called the top

- Nomenclature
	- insert = push
	- remove/delete = pop

- Feature:
	- A stack obeys last-in/first-out order (LIFO). The last item inserted into the stack is always the first to be removed

## Stack - Example

- We can think of a stack where we need to remember a bunch of things and always go back to the **most recent** thing

- Examples:
	- stack of plates
	- back button on browser
	- undo button

## Stack - Implementation through Array

- Let's consider implementing a stack using an array
- Assume that we **use the end of the array** for push and pop. If there are **n** things in an array, the next available slot is slot ...?
- Push
	- Big-O: O(1)

```
//array is 'a'
//'n' is the length of the array that is filled
//'m' is the max length of the array

assert(n < m); //make sure there is room to "push" a new element in the list
a[n] = x;
n++;
```

- Pop
	- Big-O: O(1)

```
//array is 'a'
//'n' is the length of the array that is filled
//'m' is the max length of the array

assert(n > 0); //avoid going oob
x = a[n-1];
n--;
return x; //"pop" returns the "popped" element
```

---

## Queue - Concept

- Concept:
	- A **queue** is a **linear list** in which data can only be inserted at one end, called the rear, and deleted from the other end, called the front

- Nomenclature:
	- insertion = enqueue
	- deletion = dequeue

- Feature:
	- A queue obeys **first-in/first-out order (FIFO)**. The first item inserted is always the first to be removed

- In a queue we always remove **the oldest item** in the list
	- they're often used to ensure fairness

- We can think of a queue as a list in which insertions and deletions/removals **happen at opposite ends**

## Queue - Examples

- Standing in a line
- A buffer
- Netflix/Youtube Queue
- Music Playlist

## Queue - Implementation through Array (Method 1)

- Let's consider implementing a queue through an array
	- **Add at the end**, remove from the front (always keep the fron of the queue located at index 0)
		- What's the big-O for insertion? Deletion?

- Insertion
	- Big-O: O(1)

```
//array is 'a'
//'n' is the length of the array that is filled
//'m' is the max length of the array

assert(n < m);
a[n] = x;
n++;
```

- Deletion
	- Big-O: O(n) (because of shifting)

```
//array is 'a'
//'n' is the length of the array that is filled
//'m' is the max length of the array

assert(n > 0);
x = a[0];
n--;

//need to shift everything down
for(i = 0; i < n-1; i++){
	a[i] = a[i+1];
}

return x;
```

- Note: Changing the add and remove side will not change the Big-O

## Queue - Implementation through Array (Method 2)

- Can we make both of them O(1)?
	- There is no reason to force the front of the queue always to be in `items[0]`, we **can let it "move up" as items are dequeued**
	- Use a variable "first"

### Example

- Example: we use **"first"** to indicate the **index of the oldest element**, **"count"** for the current number of elements

|deq <-|0|1|2|3|<- enq|
|------|-|-|-|-|------|
|      |~|~|~|~|      |

- enq 3 (queue if often abbreviated "q")
	- count = 1
	- first = 0

|deq <-|0|1|2|3|<- enq|
|------|-|-|-|-|------|
|      |3|~|~|~|      |

- enq 7 (queue if often abbreviated "q")
	- count = 2
	- first = 0

|deq <-|0|1|2|3|<- enq|
|------|-|-|-|-|------|
|      |3|7|~|~|      |

- enq 4 (queue if often abbreviated "q")
	- count = 3
	- first = 0

|deq <-|0|1|2|3|<- enq|
|------|-|-|-|-|------|
|      |3|7|4|~|      |

- deq (you have no choice of what gets pop'd or deq'd)
	- 3 comes out
	- count = 2
	- first = 1

|deq <-|0|1|2|3|<- enq|
|------|-|-|-|-|------|
|      |~|7|4|~|      |

- enq 7 
	- count = 3
	- first = 1

|deq <-|0|1|2|3|<- enq|
|------|-|-|-|-|------|
|      |~|7|4|7|      |

- deq
	- 7 comes out
	- count = 2
	- first = 2

|deq <-|0|1|2|3|<- enq|
|------|-|-|-|-|------|
|      |~|~|4|7|      |

- enq 1
	- Where to insert the 1?
		- We "bend the array" into a circle
		- insert in index 0
	- count = 3
	- first = 2

|deq <-|0|1|2|3|<- enq|
|------|-|-|-|-|------|
|      |1|~|4|7|      |

---

- How do you create a circle in your code?
	- **mod (%)**
	- so we just have to do all our arithmetic **modulo the size of the array**

- Let's treat the indices as if the array was circular. This is known as a **circular** buffer
- To accomplish this, we'll do all our arithmetic modulo the size of the array
- Three variables:
	- length = length of the buffer
	- count = # of things in the buffer
	- first = location of the first item

- enq
	- Big-O: O(1)

```
//array is 'a'
//'x' is the new value

assert(count < length);
a[(count + first) % length] = x;
count++;
```

- deq
	- Big-O: O(1)

```
//array is 'a'
//'x' is the new value

assert(count > 0);
x = a[first];
first = (first+1) % length;
count--;
return x;
```

## Summary: Queue and Stack (ADT)

- Queue:
	- Operations - enqueue and dequeue
	- data structure - we've introduces array (possible to use others)
- Stack:
	- operations - push, pop, and top
	- Data structure - we've introduced array (possible to use others)

- Please note that Queue only allows operations on its two ends. Stack only allow operations on its top end

---

[05/01 ->](05-01.md)