1
00:00:00,005 --> 00:00:02,007
- [Instructor] A queue is first in first out
2
00:00:02,007 --> 00:00:05,005
status structure, queues are useful if you are
3
00:00:05,005 --> 00:00:08,001
controlling access to shared resources.
4
00:00:08,001 --> 00:00:11,005
For example CPU scheduling or doing something like
5
00:00:11,005 --> 00:00:14,007
simulating planes waiting for landing instructions.
6
00:00:14,007 --> 00:00:17,001
In this example, I have two classes.
7
00:00:17,001 --> 00:00:20,009
One called customer and one called store.
8
00:00:20,009 --> 00:00:24,006
The customer class has two fields.
9
00:00:24,006 --> 00:00:27,003
One is a sting called name,
10
00:00:27,003 --> 00:00:29,006
and the name must be passed into the constructor
11
00:00:29,006 --> 00:00:32,004
when a new customer is created.
12
00:00:32,004 --> 00:00:35,009
The second is a bullion called has been served.
13
00:00:35,009 --> 00:00:40,002
This is set to false by default when a customer is created.
14
00:00:40,002 --> 00:00:42,007
There is a method in this class called serve,
15
00:00:42,007 --> 00:00:45,001
which sets has been served to true
16
00:00:45,001 --> 00:00:48,004
and prints a message to say that they have been served.
17
00:00:48,004 --> 00:00:51,008
I have also overridden the string method in this class,
18
00:00:51,008 --> 00:00:55,002
and set it to return the name field.
19
00:00:55,002 --> 00:00:57,005
This is so that when I add customers to a queue
20
00:00:57,005 --> 00:01:00,008
and then print the queue it will show me the customers
21
00:01:00,008 --> 00:01:02,005
name rather than the objects reference.
22
00:01:02,005 --> 00:01:05,002
In the store class I have a main method.
23
00:01:05,002 --> 00:01:06,007
Inside the main method I have
24
00:01:06,007 --> 00:01:09,001
created a variable called queue.
25
00:01:09,001 --> 00:01:11,002
There are several classes in the JAVA API
26
00:01:11,002 --> 00:01:13,006
that implements the queue interface.
27
00:01:13,006 --> 00:01:15,007
One of these classes is linked list,
28
00:01:15,007 --> 00:01:18,003
which I am using for this example.
29
00:01:18,003 --> 00:01:21,003
Next I have added four new customers to the queue
30
00:01:21,003 --> 00:01:25,009
called Sally, Ben, Emma, and Fred using the add method.
31
00:01:25,009 --> 00:01:28,009
The add method always an elements to the end of the list
32
00:01:28,009 --> 00:01:31,009
also known as the tail, then I print out the list.
33
00:01:31,009 --> 00:01:35,002
When I run the program you can see that I have a list
34
00:01:35,002 --> 00:01:37,000
with Sally, Ben, Emma and Fred
35
00:01:37,000 --> 00:01:39,002
in the order that I added them.
36
00:01:39,002 --> 00:01:41,001
Next I want to define a method
37
00:01:41,001 --> 00:01:43,006
to serve a customer at my store.
38
00:01:43,006 --> 00:01:44,009
So I will create a new
39
00:01:44,009 --> 00:01:52,002
static void method called serve customer.
40
00:01:52,002 --> 00:01:54,006
Which takes a linked list
41
00:01:54,006 --> 00:02:03,004
containing customers called queue
42
00:02:03,004 --> 00:02:07,009
as an argument.
43
00:02:07,009 --> 00:02:09,008
I want this to behave like a real customer
44
00:02:09,008 --> 00:02:12,000
being served in a real store.
45
00:02:12,000 --> 00:02:13,007
When a customer has been served
46
00:02:13,007 --> 00:02:15,006
they should be removed from the queue
47
00:02:15,006 --> 00:02:18,002
and it should always be the first person who joins the queue
48
00:02:18,002 --> 00:02:20,003
that gets served first.
49
00:02:20,003 --> 00:02:23,003
The queue interface provides us with a method to do this
50
00:02:23,003 --> 00:02:28,005
called poll so I am going to write customer c
51
00:02:28,005 --> 00:02:33,008
is equal to queue.poll.
52
00:02:33,008 --> 00:02:37,000
The poll method returns the first customer in the queue
53
00:02:37,000 --> 00:02:39,002
and also removes them from the queue.
54
00:02:39,002 --> 00:02:41,005
Next I will call the serve method on the customer
55
00:02:41,005 --> 00:02:44,002
that has been removed from the queue.
56
00:02:44,002 --> 00:02:48,000
Now I can call the serve customer method in the main method
57
00:02:48,000 --> 00:02:51,001
and pass in my queue.
58
00:02:51,001 --> 00:02:52,003
Then on the next line
59
00:02:52,003 --> 00:02:59,005
I will print out the contents of the queue again.
60
00:02:59,005 --> 00:03:01,002
When I run the program again
61
00:03:01,002 --> 00:03:05,001
the output first shows me my queue with Sally at the front.
62
00:03:05,001 --> 00:03:07,001
Then it shows me that Sally has been served
63
00:03:07,001 --> 00:03:08,009
and when I print out the queue again
64
00:03:08,009 --> 00:03:11,000
Sally is no longer in it.
65
00:03:11,000 --> 00:03:13,005
Queues are a useful way of applying constraints
66
00:03:13,005 --> 00:03:16,004
in situations like this where you want the first item
67
00:03:16,004 --> 00:03:18,009
in the queue to be the one that gets removed first.
No comments:
Post a Comment