Download presentation
Presentation is loading. Please wait.
Published byYanti Tedjo Modified over 6 years ago
1
Queues A queue is a data structure that is similar in spirit to a fair line. As you can see in this photo, the first dog in line can expect to be the first to pee on the tree.
2
FIFO enqueue dequeue Similarly, with queues, the first element inserted will be the first element retrieved. We'll refer to this pattern of insertion and retrieval as "first-in, first-out," or FIFO for short. A queue's enqueue function places a new element at a queue's "tail" end, while dequeue retrieves the element at a queue's "head" (i.e., front). ***Be aware that you may see variations on the names of these functions in different textbooks as they aren't as standardized as push and pop are for stacks.*** Like stacks (and unlike arrays), queues typically don't allow access to elements in the middle.
3
char* strings[CAPACITY]; int size; } queue;
typedef struct { int head; char* strings[CAPACITY]; int size; } queue; This is one way to define a queue for char*s. head is the index of the queue's head element. We'll adjust it as we dequeue elements. Why would we need to keep track of the head of our queue? Why not simply consider the element at strings[0] to be the head and the element at strings[size - 1] to be the tail? This would require us to shift all of the elements from strings[1] to strings[size - 1] down by one position every time we call dequeue, which is a big waste of time especially if we've got a long queue! CAPACITY is a constant and strings is a statically-sized array of char*s that you'll use for storing the char* elements. size stores the number of elements currently in the queue. You'll need to adjust it appropriately so that you can track the location of the "tail" of the queue. Why is this design suboptimal? It imposes a limit on the size of the queue.
4
Enqueue TODOs: [5] [4] [3] [2] [1] [0]
size < CAPACITY? store at tail size++ { head [5] [4] [3] [2] [1] [0] To enqueue an element, first make sure that the array isn't full by comparing size to CAPACITY. If size < CAPACITY, store the element in the next available open slot, which should be at index [(head + size) % CAPACITY]. Don't forget to increment size!
5
Dequeue TODOs: [5] [4] [3] [2] [1] [0]
size > 0? move head size-- return element { head [5] [4] [3] [2] [1] [0] To dequeue an element, first make sure that there are elements in the array by comparing size to 0. If size > 0, the element at the head of the list is the one you'll want to dequeue. Don't forget to reposition head and decrement size!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.