Queu e • 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.
24
Embed
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.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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.
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.
A queue of passengers in a ticket line
Brown
E(conomy)
2
Watson
B(usiness)
1
2
q.frontp
q.sizeq.rearp
Structure Types for a Linked List Implementation of a Queue
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); }
Removal of one passenger from a Queue• Remove Brown:
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.
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:
12343
2222 5669
0
my_list
my_list
Structure Types for a Linked List Implementation of an Order Lists
typedef struct { list_node_t *headp; int size; } ordered_list_t;
Maintaining an Ordered List of integers: Implementation
1 Create an empty ordered list.2 for each nonsentinel input key 3 Insert the key in the ordered list.4 Display the ordered list and its size.5 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.
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.
Algorithm for insert_in_order1 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.
Recursive Function insert_in_order list_node_t *insert_in_order( list_node_t *old_listp,
new_listp is old_listp with circled component changed to
8
8
which is the result of inserting 6 in order in
Recursive Step
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); }
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;