Queues ?? - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

Queues ??

Description:

Queues ... – PowerPoint PPT presentation

Number of Views:87
Avg rating:3.0/5.0
Slides: 49
Provided by: a01
Category:

less

Transcript and Presenter's Notes

Title: Queues ??


1
??
2
(No Transcript)
3
Queues ??
  • ??(Queues)????????????????
  • ?????????
  • ??????????,??????????,???????????????
  • ???????????????? (FIFO, first in first out).

4
Definition of Queues
  • ????(queue) ??????????,?????????,?????????
  • ???????????( the front ,or the head).
    ???????????(The rear or tail)?
  • ?? ???????????????CPU?????????????

5
Queue Operations
? Queue_entry ??????????
Constructor
Insertion (??)
6
Queue Operations (2)
Deletion ??
Get the front ?????
7
Queue Operations (3)
Check emptiness ??????
8
The Queue Class
  • The ADT Queue class
  • class Queue
  • public
  • Queue()
  • bool empty() const
  • Error_code append(const Queue_entry x)
  • Error_code serve()
  • Error_code retrieve(Queue_entry x)const
  • // the data part is left out

9
Extended Queue Operations
  • If we want to add some operations on queues, for
    example, full, clear, serve_and_retrieve, one way
    is to extend the class Queue
  • class Extended_queuepublic Queue
  • public
  • bool full() const
  • int size() const
  • void clear()
  • Error_code serve_and_retrieve(Queue_entry
    item)

10
??
11
Implementations of Queues
  • ????????????????,???????????
  • The physical model
  • A linear array with
  • the front always in the first position
  • and all entries moved up the array whenever the
    front is removed.
  • Poor!

12
Linear Implementation (????)
  • Two indices(??) to keep track of both the front
    and the rear of the queue
  • To serve an entry, take the entry and increase
    the front by one
  • To append an entry to the queue, increase the
    rear by one and put the entry in that position
  • Problem cannot reuse the discarded space
  • When the queue is regularly emptied, this is
    good.

13
Circular Queue
14
Circular Arrays(????)
  • ???????????,?????
  • ?????front?rear?????????
  • ?????,rear??,?????rear????rear??max?(last index),
    rear ? 0.
  • ?????,????front?????,??front??. ?front ?? max?, ?
    front ? 0?

15
Boundary Conditions
empty after deletion
remove
rear
front
No difference
front
rear
Full after addition
insert
rear
front
front
rear
16
Boundary Conditions
  • ??????????????
  • ????
  • 1. ?????????
  • 2. ????????????????rear????front???,? ????true.
  • 3. ???????( counter)????????????

17
Circular Implementation of Queues
template ltclass Tgt class Queue public
Queue() bool empty() const Error_code
serve() Error_code append(const T item)
Error_code retrieve(T item) const protected
unsigned maxqueue int count int
front,rear T entrymaxqueue
18
Implementations of Queues
  • template ltclass Tgt Error_code QueueltTgtappend(co
    nst T item)
  • if(count gt maxqueue) return overflow
  • count
  • rear ((rear 1) maxqueue)?0(rear 1)
  • entryrear item
  • return success

19
Implementations
  • template ltclass Tgt Queue ltTgtQueue()
  • /post the queue is initialized to be empty/
  • count 0
  • rear maxqueue -1
  • // rear is just before the front position
  • //when it is empty
  • front 0

20
??
  • ????bool?????????(???count)??????
  • ?? ???class??
  • ?????????????????
  • ????????

21
??
22
Demonstration and Testing
  • To verify the methods, we write a demonstration
    program.
  • The program interactively accepts commands and
    prints the results, a kind of black box testing.
  • Testing is the process of executing a program
    with the intent of finding errors.
  • Errors valid input produces invalid output.
  • Choosing those inputs which are likely to go
    wrong, especially the boundary cases.

23
Demonstration and Testing
  • Easy values. Test the program with data that are
    easy to check.
  • 2. Typical, realistic values. Always try a
    program on data chosen to represent how the
    program will be used.
  • 3. Extreme values. Many programs err at the
    limits of their range of applications.
  • 4. Illegal values. A good program should output
    some sensible error message for invalid inputs.

24
Demonstration and Testing
  • Alternatively, we can automate the testing
    generate a sequence of operations and compare two
    queues (one is assumed to be the standard) to see
    if they have the same state after each operation
    (See lab1).

25
Testing and Debugging
  • A classic book on testing, now also online
  • The art of software testing, Glenford Myers
  • Read Chapter 7 about debugging to reduce your
    effort on debugging.

26
Testing and Debugging
  • int main()
  • /Post Accept commands from user and execute
    them /
  • Extended_queue ltintgt test_queue
  • introduction()// instructions for the user
  • while (do_command(get_command(), test_queue))

27
  • bool do_command(char c,
  • Extented_queue ltintgt
    test_queue)
  • Pre c is a valid command
  • Post Perform the given command c on
    test_queue. Return false if c q (quit),
    otherwise, return true
  • void get_command()
  • Post Get a valid command from the user and
    reurn it.

28
bool do_command(char c, Extended_queueltintgt
test_queue) /Post perform the given commands.
return true if c ! 'q'. / bool
continue_input true int x switch(c)
case 'a'//append an item if
(test_queue.full()) cout ltlt"full!"ltltendl
else cout ltlt "input an
integer"ltltendl cin gtgt x
test_queue.append(x)
break case . return
continue_input
29
char get_command() char command bool
waiting true cout ltlt"select a command and
press enter"ltltendl while (waiting) cin
gtgt command command tolower(command)
if (command 'a' command 'q' command
's' command 'r') waiting false
else cout ltlt "enter a command" ltlt
endl return command
30
??
31
Software Lift Cycle
  • 1. Analyze the problem precisely and completely.
    Be sure to specify all necessary user interface
    with care.
  • 2. Build a prototype and experiment with it until
    all specifications can be finalized.
  • 3. Design the algorithm, using the tools of data
    structures and of other algorithms whose
    function is already known.
  • 4. Verify that the algorithm is correct, or make
    it so simple that its correctness is
    self-evident.
  • 5. Analyze the algorithm to determine its
    requirements. Make sure that it meets the
    specifications.

32
  • 6. Code the algorithm into the appropriate
    programming language.
  • 7. Test and evaluate the program on carefully
    chosen test data.
  • 8. Refine and repeat the foregoing steps as
    needed for additional functions until the
    software is complete and fully functional.
  • 9. Optimize the code to improve performance, but
    only if necessary.
  • 10. Maintain the program so that it will meet the
    changing needs of its users.

33
  • Requirement analysis what the software will do
  • Algorithm design and analysis how the task is
    done and if the algorithm satisfies the
    requirements
  • Coding and testing coding the algorithm and
    testing the program to see if it satisfies the
    requirements
  • Maintenance maintain the program so that it will
    meet the changing needs of its users.

34
(No Transcript)
35
Application Airport Simulation
  1. ????????????
  2. ???????????????????,???????????
  3. ????????????????
  4. ?????????????????????
  5. ??????????????????????????????

36
  • Requirements(????) ?????????????
  • ????????????????Generate random number of
    arriving planes and taking off planes.
  • ???????for each arriving plane, put the plane
    into the landing queue if the landing queue is
    not full, otherwise, refuse (direct it to another
    airport)
  • ??????put them into taking off queue
  • Taking care of the runway if the landing queue
    is not empty, let one land. Otherwise, if the
    taking off queue is not empty, let one take off.
    Otherwise, let the runway idle.

37
Objects Plane(??), Runway(??) Data Structures
landing queue arriving planes(?????) taking
off queue planes ready to take off(???????)
38
Plane Specification
39
(No Transcript)
40
(No Transcript)
41
The Runway Specification
// more for statistic
reason
42
(No Transcript)
43
(No Transcript)
44
(No Transcript)
45
(No Transcript)
46
(No Transcript)
47
(No Transcript)
48
????
  • ?????ADT??
  • ????????????????????????
  • ????????
  • ???????????
Write a Comment
User Comments (0)
About PowerShow.com