Cpt S 122 Data Structures Queues Nirmalya Roy

  • Slides: 25
Download presentation
Cpt S 122 – Data Structures Queues Nirmalya Roy School of Electrical Engineering and

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

Topics n Queues ¡ n enqueue, dequeue, printqueue Queues Applications

Topics n Queues ¡ n enqueue, dequeue, printqueue Queues Applications

Queues n n n Queue is another common data structure. A queue is similar

Queues n n n Queue is another common data structure. A queue is similar to a checkout line in a grocery store—the first person in line is serviced first, and other customers enter the line only 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, first-out (FIFO) data structure. The insert and remove operations are known as enqueue and dequeue, respectively.

Queues (Cont. ) n n n Queues have many applications in computer systems. For

Queues (Cont. ) n n n Queues have many applications in computer systems. For computers that have only a single processor, only one user at a time may be serviced. 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. A multiuser environment may have only a single printer. Many users may be generating outputs to be printed. If the printer is busy, other outputs may still be generated. These are spooled to disk where they wait in a queue until the printer becomes available.

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 its final destination. The routing node routes one packet at a time, so additional packets are enqueued until the router can route them.

Queue Manipulations dequeue n The program provides several options: ¡ insert a node in

Queue Manipulations dequeue n The program provides several options: ¡ insert a node in the queue (function enqueue), remove a node from the queue (function dequeue) and ¡ terminate the program. ¡ n enqueue Note the pointers to the head of the queue and the tail of the queue.

Queues Example

Queues Example

Queues Example

Queues Example

Queues Example

Queues Example

Function enqueue

Function enqueue

Function enqueue n Function enqueue receives three arguments from main: ¡ ¡ ¡ the

Function enqueue n Function enqueue receives three arguments from main: ¡ ¡ ¡ the address of the pointer to the head of the queue, the address of the pointer to the tail of the queue and the value to be inserted in the queue.

Function enqueue (Cont. ) n The function consists of three steps: ¡ ¡ To

Function enqueue (Cont. ) n The function consists of three steps: ¡ ¡ To create a new node: Call malloc, assign the allocated memory location to new. Ptr, assign the value to be inserted in the queue to new. Ptr->data and assign NULL to new. Ptr->next. Ptr. If the queue is empty, assign new. Ptr to *head. Ptr, because the new node will be both the head and tail of the queue; otherwise, n ¡ assign pointer new. Ptr to (*tail. Ptr)->next. Ptr, because the new node will be placed after the previous tail node. Assign new. Ptr to *tail. Ptr, because the new node is the queue’s tail. .

Operation enqueue n The dotted arrows in part (b) illustrate Steps 2 and 3

Operation enqueue n The dotted arrows in part (b) illustrate Steps 2 and 3 of function enqueue that enable a new node to be added to the end of a queue that is not empty.

Function dequeue

Function dequeue

Function dequeue n Function dequeue receives two arguments ¡ ¡ ¡ the address of

Function dequeue n Function dequeue receives two arguments ¡ ¡ ¡ the address of the pointer to the head of the queue the address of the pointer to the tail of the queue as arguments removes the first node from the queue.

Function dequeue n The dequeue operation consists of six steps: ¡ ¡ ¡ Assign

Function dequeue n The dequeue operation consists of six steps: ¡ ¡ ¡ Assign (*head. Ptr)->data to value to save the data. Assign *head. Ptr to temp. Ptr, which will be used to free the unneeded memory. Assign (*head. Ptr)->next. Ptr to *head. Ptr so that *head. Ptr now points to the new first node in the queue. If *head. Ptr is NULL, assign NULL to *tail. Ptr because the queue is now empty. Free the memory pointed to by temp. Ptr. Return value to the caller.

Operation dequeue n Part (b) shows temp. Ptr pointing to the dequeued node, and

Operation dequeue n Part (b) shows temp. Ptr pointing to the dequeued node, and head. Ptr pointing to the new first node of the queue. ¡ Function free is used to reclaim the memory pointed to by temp. Ptr.

Function print. Queue

Function print. Queue

Output

Output

Output

Output

Conclusions n What are the differences between a linked list and a stack? n

Conclusions n What are the differences between a linked list and a stack? n What are the differences between a stack and a queue?

Conclusions n What are the differences between a linked list and a stack? ¡

Conclusions n What are the differences between a linked list and a stack? ¡ ¡ n Possible to insert and remove a node from anywhere in a linked list. Nodes in a stack may be inserted only at the top of the stack and removed only from the top of the stack. What are the differences between a stack and a queue? ¡ ¡ A queue has pointers to both its head and tail so that nodes may be inserted at the tail and deleted from the head. A stack has a single pointer to the top of the stack where both insertion and deletion of nodes is performed.

Conclusions n Implementing linked list, stack & queue functions

Conclusions n Implementing linked list, stack & queue functions