Full Transcript
https://www.youtube.com/watch?v=nqXaPZi99JI
[00:00] hey what's going on everybody it's you
[00:02] hey what's going on everybody it's you bro hope you're doing well
[00:03] bro hope you're doing well in this video i'm going to explain cues
[00:05] in this video i'm going to explain cues in computer science
[00:06] in computer science so sit back relax and enjoy the show
[00:11] if you find this video helpful please
[00:12] if you find this video helpful please remember to like
[00:14] remember to like comment and subscribe your support will
[00:16] comment and subscribe your support will help keep this channel running
[00:18] help keep this channel running whoa hey we're talking about cues today
[00:21] whoa hey we're talking about cues today so cues
[00:21] so cues are a fifo data structure not to be
[00:24] are a fifo data structure not to be confused with fifa
[00:25] confused with fifa fifo as in first in first out an example
[00:28] fifo as in first in first out an example would be a line of people it's first
[00:30] would be a line of people it's first come first serve whoever's there first
[00:32] come first serve whoever's there first gets helped first
[00:33] gets helped first so this is a collection designed for
[00:35] so this is a collection designed for holding elements prior to processing
[00:37] holding elements prior to processing it's a linear data structure here's an
[00:39] it's a linear data structure here's an example imagine that we're a cashier and
[00:41] example imagine that we're a cashier and we're selling tickets to a concert we
[00:43] we're selling tickets to a concert we will help
[00:44] will help whoever approaches our register first
[00:46] whoever approaches our register first it's first come
[00:47] it's first come first serve in other words it's fifo
[00:50] first serve in other words it's fifo
[00:50] first in first out unfortunately a wild karen
[00:53] first out unfortunately a wild karen appears and she would like to talk to
[00:54] appears and she would like to talk to the manager
[00:55] the manager now karen is going to waste everybody's
[00:57] now karen is going to waste everybody's time so it's going to be a while
[00:59] time so it's going to be a while in the meantime chad enters the line he
[01:01] in the meantime chad enters the line he will enter in at the back and wait his
[01:02] will enter in at the back and wait his turn
[01:03] turn followed by steve and then harold so
[01:06] followed by steve and then harold so karen is at the head of our queue
[01:07] karen is at the head of our queue our line and harold is all the way at
[01:09] our line and harold is all the way at the back our tail
[01:11] the back our tail as you can see harold is very anxious
[01:13] as you can see harold is very anxious right now once karen has been defeated
[01:15] right now once karen has been defeated chad will move to the head of our queue
[01:18] chad will move to the head of our queue so
[01:18] so chad will now be helped next and harold
[01:21] chad will now be helped next and harold will still be at the tail until somebody
[01:23] will still be at the tail until somebody else enters the line
[01:24] else enters the line once chad has been helped he will move
[01:26] once chad has been helped he will move out of our queue
[01:27] out of our queue and steve is at the head now and the
[01:30] and steve is at the head now and the tail will move as well
[01:31] tail will move as well and then once steve has been held harold
[01:35] and then once steve has been held harold is the only one currently waiting in
[01:36] is the only one currently waiting in line for his tickets he will be at both
[01:38] line for his tickets he will be at both the head and
[01:39] the head and the tail of our queue so that's kind of
[01:41] the tail of our queue so that's kind of how a queue works it's first come
[01:43] how a queue works it's first come first serve fifo first in first out if
[01:45] first serve fifo first in first out if you were here first then you will be
[01:47] you were here first then you will be helped first
[01:48] helped first now the concepts of adding and removing
[01:50] now the concepts of adding and removing objects from a queue are known as both
[01:53] objects from a queue are known as both enqueuing and dequeueing enqueuing is
[01:55] enqueuing and dequeueing enqueuing is the concept of adding an object
[01:57] the concept of adding an object to the end of our cue our tail and
[02:00] to the end of our cue our tail and dequeueing
[02:01] dequeueing is when we remove an object from the head of our queue.
[02:04] Now what methods do you need to enqueue and dq from a queue?
[02:08] Well it really depends on the programming language that you're working with.
[02:11] A lot of you voted for data structures and algorithms using Java, so I'll probably stick with Java then.
[02:16] Here's an example now to enqueue and dq.
[02:19] We need, well, a queue, so let's create one.
[02:23] Then we need to list the data type of the objects that we're going to add to this queue.
[02:27] Let's work with strings because strings are objects and they're fairly simple.
[02:31] And I will name this queue q equals new.
[02:34] Now check this out, I will attempt to create a queue object and instantiate it.
[02:38] And the data type is string and I will add a constructor.
[02:42] Now we cannot instantiate the type q.
[02:44] Here's the reason why.
[02:46] So if we take a look at the queue class right here, q is actually an interface and not a class.
[02:52] And based on our lesson on interfaces, we cannot create an instance of an interface.
[02:58] An interface is meant to be more of like a template that we can apply to another.
[03:02] a template that we can apply to another class.
[03:03] class so to utilize q technology we need a class.
[03:05] so to utilize q technology we need a class that implements q.
[03:06] now taking a look at that implements q.
[03:09] now taking a look at our java collections hierarchy.
[03:11] our java collections hierarchy there are two classes that implement cues.
[03:13] there are two classes that implement cues one of which is the linked list.
[03:16] cues one of which is the linked list and the other is the priority queue now.
[03:18] and the other is the priority queue now priority queues they will rearrange their elements based on a certain priority.
[03:20] priority queues they will rearrange their elements based on a certain priority.
[03:21] their elements based on a certain priority.
[03:22] priority so they wouldn't be a good example so to utilize the features of a queue.
[03:25] so to utilize the features of a queue we are going to create a linked list.
[03:26] utilize the features of a queue we are going to create a linked list because we cannot instantiate a queue.
[03:28] because we cannot instantiate a queue itself because it's an interface.
[03:30] itself because it's an interface with that being said let's change equals.
[03:32] with that being said let's change equals new q to equals new linked list.
[03:35] new q to equals new linked list.
[03:36] to equals new linked list we won't be focusing on any of the features of linked lists.
[03:39] we won't be focusing on any of the features of linked lists we'll save that for another video maybe the next one.
[03:41] features of linked lists we'll save that for another video maybe the next one i haven't decided.
[03:43] for another video maybe the next one i haven't decided yet so we will only cover the features that linked lists will utilize via the q interface.
[03:45] haven't decided yet so we will only cover the features that linked lists will utilize via the q interface.
[03:46] yet so we will only cover the features that linked lists will utilize via the q interface.
[03:49] now with the q interface there are three methods that we inherit from the collections parent class.
[03:52] interface now with the q interface there are three methods that we inherit from the collections parent class.
[03:55] there are three methods that we inherit from the collections parent class add remove and element.
[03:57] from the collections parent class add remove and element these will do.
[04:00] add remove and element these will do.
[04:02] add remove and element these will do approximately the same thing
[04:04] approximately the same thing as enqueuing dequeuing and peaking at
[04:07] as enqueuing dequeuing and peaking at the topmost element
[04:08] the topmost element however they throw exceptions and
[04:10] however they throw exceptions and according to the documentation
[04:12] according to the documentation it's actually better to use this column
[04:14] it's actually better to use this column of methods instead
[04:15] of methods instead these will return a special value we
[04:18] these will return a special value we have offer
[04:19] have offer which will enqueue or add an element to
[04:21] which will enqueue or add an element to the
[04:22] the tail of our queue poll will dequeue
[04:25] tail of our queue poll will dequeue it will remove the head of our current
[04:28] it will remove the head of our current queue and
[04:28] queue and then there is also peak it will not
[04:31] then there is also peak it will not remove the head but it will examine it
[04:33] remove the head but it will examine it and return it so those are the three
[04:35] and return it so those are the three dedicated methods
[04:37] dedicated methods that we implement via the queue
[04:39] that we implement via the queue interface there are some additional
[04:40] interface there are some additional methods too which we'll cover later
[04:42] methods too which we'll cover later which we inherit from the collections
[04:44] which we inherit from the collections class so let's begin
[04:46] class so let's begin by offering a bunch of people to our
[04:48] by offering a bunch of people to our queue this is how we enqueue or add
[04:50] queue this is how we enqueue or add elements to our queue so first in our
[04:53] elements to our queue so first in our line we have karen
[04:54] line we have karen so to add her to the queue we would type
[04:57] so to add her to the queue we would type the name of our queue
[04:58] the name of our queue q dot offer
[05:01] q dot offer and we will offer karen to the front of
[05:04] and we will offer karen to the front of our queue and she would like to complain
[05:05] our queue and she would like to complain to the manager
[05:07] to the manager next we have chad q dot offer
[05:12] chad then we have steve
[05:17] and lastly harold
[05:22] okay now if we were to display our cue
[05:25] okay now if we were to display our cue we'll place it within a print line
[05:27] we'll place it within a print line statement we will pass in
[05:29] statement we will pass in q as an argument and this should display
[05:31] q as an argument and this should display our q in order
[05:33] our q in order first we have karen at the head then
[05:35] first we have karen at the head then chad
[05:36] chad steve then harold at the tail one method
[05:38] steve then harold at the tail one method that we implement
[05:39] that we implement from the q interface is the peak method
[05:43] from the q interface is the peak method we can use the peak method to examine
[05:45] we can use the peak method to examine and take a look at the object
[05:47] and take a look at the object at the head of our queue we're not
[05:49] at the head of our queue we're not actually going to remove
[05:50] actually going to remove the object at the head of the cube but
[05:52] the object at the head of the cube but take a look at it so within
[05:54] take a look at it so within a print line statement i will use q
[05:57] a print line statement i will use q dot peak method and after peaking over
[06:00] dot peak method and after peaking over our cash register
[06:01] our cash register unfortunately karen is at the front of
[06:03] unfortunately karen is at the front of the line now
[06:05] the line now to dq we can use the pull method
[06:08] to dq we can use the pull method so type q dot poll
[06:12] so type q dot poll and this will dequeue your q
[06:15] and this will dequeue your q so karen is no longer at the head of our
[06:18] so karen is no longer at the head of our queue so let's pull a couple more times
[06:20] queue so let's pull a couple more times for practice
[06:21] for practice so after polling twice karen and chad
[06:25] so after polling twice karen and chad are now
[06:25] are now gone using pull a third time will remove
[06:29] gone using pull a third time will remove steve and lastly
[06:33] steve and lastly now the nice thing about the poll method
[06:35] now the nice thing about the poll method is that it will not cause an exception
[06:38] is that it will not cause an exception according to the java documentation you
[06:40] according to the java documentation you can use element to peak
[06:42] can use element to peak but it will throw an exception so if i
[06:44] but it will throw an exception so if i was to use
[06:45] was to use element at the end this would cause an
[06:48] element at the end this would cause an exception then
[06:49] exception then so usually it's preferable to use this
[06:52] so usually it's preferable to use this column of methods even though they're
[06:53] column of methods even though they're similar
[06:54] similar these will not throw exceptions offer
[06:56] these will not throw exceptions offer pull and peek now with cues there are
[06:58] pull and peek now with cues there are some additional methods that you might
[07:00] some additional methods that you might be interested in
[07:01] be interested in the q class will extend the collection
[07:04] the q class will extend the collection class
[07:04] class so the q class inherits everything that
[07:06] so the q class inherits everything that the collection class has
[07:08] the collection class has and there's some pretty useful methods
[07:09] and there's some pretty useful methods within here i'll show you a few
[07:11] within here i'll show you a few so the first is that we can check to see
[07:13] so the first is that we can check to see if our queue is empty
[07:15] if our queue is empty so within a print line statement i will
[07:17] so within a print line statement i will type q
[07:19] type q dot is empty and if our queue is empty
[07:23] dot is empty and if our queue is empty this will return true
[07:24] this will return true so if i move this line of code after we
[07:27] so if i move this line of code after we offer a bunch of
[07:28] offer a bunch of strings a bunch of people to our queue
[07:30] strings a bunch of people to our queue if we check to see if our queue is empty
[07:33] if we check to see if our queue is empty that will be false there are people
[07:35] that will be false there are people currently within our line
[07:37] currently within our line so that is the is md method we can also
[07:40] so that is the is md method we can also check to see
[07:40] check to see the size of our queue how long is our
[07:43] the size of our queue how long is our line system.out.printline
[07:45] line system.out.printline q dot size and the size of our line
[07:49] q dot size and the size of our line our q is four four objects we have four
[07:52] our q is four four objects we have four people waiting in line to be helped
[07:54] people waiting in line to be helped so that is the size method and another
[07:57] so that is the size method and another is the contains method
[07:58] is the contains method we can check to see if our queue has a
[08:01] we can check to see if our queue has a certain object that we're looking for
[08:03] certain object that we're looking for so within a print line statement i will
[08:05] so within a print line statement i will type
[08:06] type q dot contains and then pass in an
[08:09] q dot contains and then pass in an object.
[08:10] object let's check to see if harold is at the back of the line.
[08:11] he's been waiting here patiently.
[08:14] q dot contains herald and according to the contains method.
[08:18] herald is in fact within our line that is true.
[08:21] however this method does not give the index the position in which herald is in.
[08:26] imagine that we yell out hey harold are you there and he yells back yeah or true.
[08:33] that's kind of how the contains method works.
[08:34] so these are three useful methods that cues inherit from the collection class.
[08:40] now where could cues be useful.
[08:41] they're actually used in quite a number of things but here's a few examples that came to mind.
[08:45] so one that can be used in keyboard buffers.
[08:47] have you ever typed so fast that the computer couldn't render all the characters onto the screen.
[08:53] and all of those characters were added to a buffer.
[08:57] they were all displayed in sequence and they were waiting for their turn to be displayed.
[08:59] so with a keyboard buffer letters should appear on the screen in the order that they're pressed right.
[09:05] first in first out.
[09:06] they're also used in printer cues.
[09:09] print jobs should be completed.
[09:10] Jobs should be completed in order so we would start with page one.
[09:12] In order so we would start with page one then page two.
[09:13] Then page two and just follow that pattern and as we discussed before.
[09:15] And just follow that pattern and as we discussed before they're used in linked lists, priority queues as well as breadth.
[09:17] Discussed before they're used in linked lists, priority queues as well as breadth first search algorithms.
[09:21] Breadth first search algorithms well everybody in conclusion.
[09:23] In conclusion a queue is a FIFO data structure first in.
[09:27] A queue is a FIFO data structure first in first out.
[09:28] First out it's a collection designed for holding elements prior to some sort of processing.
[09:31] It's a collection designed for holding elements prior to some sort of processing it's a linear data structure.
[09:34] Linear data structure imagine a bunch of people waiting in line to enqueue.
[09:36] Imagine a bunch of people waiting in line to enqueue that means to add an element to the tail.
[09:39] That means to add an element to the tail of our queue to the end.
[09:42] Of our queue to the end we can use the offer method in Java to dequeue.
[09:44] We can use the offer method in Java to dequeue we would use the poll method in Java.
[09:47] Dequeue we would use the poll method in Java that will remove the element at the head.
[09:50] That will remove the element at the head of our queue.
[09:50] Of our queue so that is the queue data structure in the comments down below.
[09:53] So that is the queue data structure in the comments down below let me know what you would tell Karen if.
[09:56] Comments down below let me know what you would tell Karen if she asked to speak with your manager.
[09:57] Let me know what you would tell Karen if she asked to speak with your manager and that ladies and gentlemen is the queue.
[09:59] And that ladies and gentlemen is the queue data structure in the field of computer.
[10:01] Data structure in the field of computer science.