Cpt S 122 Data Structures Templatized Queue Nirmalya

  • Slides: 16
Download presentation
Cpt S 122 – Data Structures Templatized Queue Nirmalya Roy School of Electrical Engineering

Cpt S 122 – Data Structures Templatized Queue Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Topics n Templated Queue ¡ ¡ enqueue dequeue is. Queue. Empty print. Queue

Topics n Templated Queue ¡ ¡ enqueue dequeue is. Queue. Empty print. Queue

Queues n A queue is similar to a supermarket checkout line ¡ ¡ n

Queues n A queue is similar to a supermarket checkout line ¡ ¡ n n n the first person in line is serviced first other customers enter the line at the end and wait to be serviced. Queue nodes are removed only from the head of the queue and are inserted only at the tail of the queue. For this reason, a queue is referred to as a first-in, firstout (FIFO) data structure. The insert and remove operations are known as enqueue and dequeue.

Queues (cont. ) n n n Queues have many applications in computer systems. Computers

Queues (cont. ) n n n Queues have many applications in computer systems. Computers that have a single processor can service only one user at a time. Entries for the other users are placed in a queue. Each entry gradually advances to the front of the queue as users receive service. The entry at the front of the queue is the next to receive service.

Queues (cont. ) n n n Queues are also used to support print spooling.

Queues (cont. ) n n n Queues are also used to support print spooling. For example, a single printer might be shared by all users of a network. Many users can send print jobs to the printer, even when the printer is already busy. These print jobs are placed in a queue until the printer becomes available. A program called a spooler manages the queue ¡ ensure that, as each print job completes, the next print job is sent to the printer.

Queues (cont. ) n n n Information packets also wait in queues in computer

Queues (cont. ) n n n Information packets also wait in queues in computer networks. Each time a packet arrives at a network node, it must be routed to the next node on the network along the path to the packet’s final destination. The routing node routes one packet at a time ¡ n n n additional packets are enqueued until the router can route them. A file server in a computer network handles file access requests from many clients throughout the network. Servers have a limited capacity to service requests from clients. When that capacity is exceeded, client requests wait in queues.

Queues (cont. ) n n Queue class template through private inheritance of the List

Queues (cont. ) n n Queue class template through private inheritance of the List class template. The Queue has member functions ¡ ¡ n enqueue dequeue is. Queue. Empty print. Queue These are essentially the ¡ ¡ insert. At. Back (enqueue) remove. From. Front (dequeue) is. Empty (is. Queue. Empty) print functions of the List class template (print. Queue).

Queues (cont. ) n The List class template contains other member functions ¡ n

Queues (cont. ) n The List class template contains other member functions ¡ n Queue class template inherits privately from the List class template. ¡ n makes all the List class template’s member functions private in the Queue class template. Queue’s member functions call the appropriate member function of the list class ¡ ¡ n do not make those accessible through the public interface to the Queue class. enqueue calls insert. At. Back dequeue calls remove. From. Front is. Queue. Empty calls is. Empty and print. Queue calls print. This delegation requires explicit use of the this pointer in is. Queue. Empty and print. Queue to avoid compilation errors.

Queue Class Template

Queue Class Template

Queue Class Template

Queue Class Template

Queues Test Program n n Queue class template to instantiate integer queue int. Queue

Queues Test Program n n Queue class template to instantiate integer queue int. Queue of type Queue< int >. Integers 0 through 2 are enqueued to int. Queue ¡ n n dequeued from int. Queue in first-in, first-out order. The program instantiates queue double. Queue of type Queue< double >. Values 1. 1, 2. 2 and 3. 3 are enqueued to double. Queue, ¡ dequeued from double. Queue in first-in, first-out order.

Queue Class Template Test program

Queue Class Template Test program

Test program

Test program

Test program

Test program

Test program output

Test program output

Test program output

Test program output