1 / 24

Queue

Queue. Example: A line of customers waiting at a checkout counter. Queue => a list data structure in which elements are inserted at one end and removed from the other end. A queue is called a first in, first out (FIFO) list. The first element stored is the first to be removed.

leena
Télécharger la présentation

Queue

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Queue • Example: A line of customers waiting at a checkout counter. • Queue => a list data structure in which elements are inserted at one end and removed from the other end. • A queue is called a first in, first out (FIFO) list. The first element stored is the first to be removed. • Use of a queue: A steam of jobs waiting to be printed by a printer.

  2. Implementing a Queue using Link List • We can implement a queue using a link list that grows and shrinks in size as elements are added and deleted. • We need to keep track of the first node (front) of the queue, since nodes are removed from the front. • We need to keep track of the last node (rear) of the queue, since nodes are added from the rear. • We also need to know the size of the queue. • Two important operations required to maintain a queue: • - addition of elements • - removal of elements • Another helpful operation is to display the queue.

  3. Brown Watson E(conomy) B(usiness) 2 1 A queue of passengers in a ticket line q.frontp q.rearp q.size 2

  4. Structure Types for a Linked List Implementation of a Queue • typedef struct queue_node_s • { • queue_element_t element; • struct queue_node_s *restp; • } queue_node_t; • typedef struct • { • queue_node_t *frontp, *rearp; • int size; • } queue_t;

  5. Function to Add element in a Queue • void add_to_q (queue_t *qp, queue_element_t ele) // qp is a pointer to queue • { • if (qp->size == 0) // adds to empty queue • { • qp->rearp = (queue_node_t *)malloc(sizeof (queue_node_t)); • qp->frontp = qp->rearp; • } • else // add to non-empty queue • { • qp->rearp->restp = (queue_node_t *)malloc(sizeof (queue_node_t)); • qp->rearp = qp->rearp->restp; • } • qp->rearp->element = ele; // defines newly added node • qp->rearp->restp = NULL; • ++(qp->size); • }

  6. Brown Carson Watson Watson Brown E(conomy) E(irstClass) B(usines) B(usiness) E(conomy) 2 2 1 1 2 q.frontp Before q.rearp q.size 2 Addition of one passenger to a Queue add_to_q(&q, next_pass); • Add Carson: q.frontp q.rearp After q.size 3

  7. Function to Remove element of a Queue • queue_element_t remove_from_q (queue_t *qp) • { • queue_node_t *to_freep; // pointer to node removed • queue_element_t ans; // initial queue value which is to be // returned. • to_free = qp->frontp; // saves pointer to node being deleted • ans = to_free->element; // retrieves value to return • qp->front = to_freep->restp; // deletes first node • free(to_freep); // deallocates space • --(qp->size); • if (qp->size == 0) // queue’s only node was deleted • qp->rearp = NULL; • return (ans); • }

  8. Carson Brown Brown Watson Watson Carson E(irstClass) B(usines) E(conomy) B(usines) E(irstClass) E(conomy) 2 1 2 2 1 2 3 Removal of one passenger from a Queue • Remove Brown: q.frontp Before q.rearp q.size 3 to_freep q.frontp q.rearp q.size During function Call

  9. Creates and manipulates a queue of passengers int scan_passenger(queue_element_t * passp); void print_passenger(queue_element_t pass); void add_to_q(queue_t *qp, queue_element_t ele); queue_element_t remove_from_q(queue_t *qp); void display_q(queue_t q);

  10. Creates and manipulates a queue of passengers int main() { queue_t pass_q = {NULL, NULL, 0}; queue_element_t next_pass, fst_pass; char choice; do { printf(“Enter A(dd), R(emove), D(isplay), or Q(uit)>”); scanf(“c”, &choice); switch(toupper(choice)) { case ‘A’: printf(“Enter passenger data> “); scan_passenger(&next_pass); add_to_q(&pass_q, next_pass); break;

  11. Creates and manipulates a queue of passengers case ‘R’: if (pass_q.size > 0) { fst_pass = remove_from_q(&pass_q); printf(“Passenger removed from queue: \n”); printf_passenger(fst_pass); } else printf(“Queue empty - no-one to delete\n”); break; case ‘D’: if (pass_q.size > 0) display_q(pass_q); else printf(“Queue is empty\n”); break;

  12. Creates and manipulates a queue of passengers case ‘Q’: printf(“Leaving passenger queue program with %d\n”, pass_q.size); printf(“passengers in the queue\n”); break; default: printf(“Invalid choice ---try again\n”); } } while (toupper(choice) ! = ‘Q’); return (0); }

  13. Ordered Lists • In queues and stacks, the time when a node was inserted in the list determines the position of the node in the list. • A key component determines the position of the node in an ordered list. Example of key component: ID number. • Ordered list => a data structure in which each element’s position is determined by the value of its key component , so that the key values form an increasing or decreasing sequence. • Use of ordered list: We can use an ordered list to maintain a list of integers, real numbers, or airline passengers. • An advantage of using an ordered list is that we can delete any passenger from the list, whereas in queue only the passenger at the front can be removed.

  14. 1234 Ordered Lists • An ordered list should have a component to represent the list size so that we no not need to traverse all the nodes to count them. • A nonempty ordered list: • An empty ordered list: my_list 2222 5669 3 my_list 0

  15. Structure Types for a Linked List Implementation of an Order Lists • typedef struct list_node_s • { • int key; • struct list_node_s *restp; • } list_node_t; • typedef struct • { • list_node_t *headp; • int size; • } ordered_list_t;

  16. Maintaining an Ordered List of integers: Implementation • Create an empty ordered list. • for each nonsentinel input key • 3 Insert the key in the ordered list. • Display the ordered list and its size. • for each nonsentinel input key • 6 Delete node marked by key. • 7 if deletion is successful • 8 Display the ordered list and its size. • else • 9 Display error message.

  17. Functions insert, delete, and print_list • List_node_t *insert_in_order(list_node_t *old_listp, • int new_key); • void insert ( ordered_list_t *listp, int key); • int delete (ordered_list_t *listp, int taeget); • void print_list (ordered_list_t list); • Function insert is similar to add_queue in that both the size and pointer components of our ordered list structure require modification. However, function insert differs from our queue function that we must first search the right place to insert.

  18. Algorithm for insert_in_order • if the list is empty // simple case 1 • 2 The new list is just a new node containing the new key and an empty restp component. • else if the key to insert should precede the list’s first node • // simple case 2 • 3 The new list is a new node containing the new key and with the old list as the restp component. • else // recursive step • 4 The new list starts with the first value of the old list. The restp component is the rest of the old list with the new node correctly inserted.

  19. Recursive Function insert_in_order • list_node_t *insert_in_order( list_node_t *old_listp, • int new_key) • { • list_node_t *new_listp; • if (old_listp == NULL) • { • new_listp = (list_node_t *) malloc (sizeof (list_node_t)); • new_listp->key = new_key; • new_listp->restp = NULL; • } Simple Case 1 old_listp new_key new_listp 4 4

  20. Recursive Function insert_in_order • else if (old_listp->key >= new_key) • { • new_listp = (list_node_t *)malloc(sizeof (list_node_t)); • new_listp->key = new_key; • new_listp->restp = old_listp; • } Simple Case 2 old_listp new_key new_listp 4 4 5 8

  21. Recursive Function insert_in_order Recursive Step • else • { • new_listp = old_listp; • new_listp->restp = insert_in_order(old_listp->restp, new_key); • } • return (new_listp); • } new_listp is old_listp with circled component changed to old_listp new_key 6 6 8 which is the result of inserting 6 in order in 5 8 8

  22. Function insert • // insert a node in an ordered list. • void insert (ordered_list_t *listp, //pointer to an ordered list • int key) // input • { • ++(listp->size); • listp->headp = insert_in_order (listp->headp, key); • }

  23. Iterative Function delete • int delete (ordered_list_t *listp, // pointer to ordered list • int target) // key of node to delete • { • list_node_t *to_freep, // pointer to node to delete • *cur_nodep; // pointer used to traverse list // until it points to node // preceding node to delete • int is_deleted; • // if list is empty, deletion is impossible • if (list->size == 0) • is_deleted = 0;

More Related