+ All Categories
Home > Documents > Queue Example: A line of customers waiting at a checkout counter. Queue => a list data structure in...

Queue Example: A line of customers waiting at a checkout counter. Queue => a list data structure in...

Date post: 21-Dec-2015
Category:
View: 220 times
Download: 0 times
Share this document with a friend
24
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.
Transcript
Page 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.

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.

Page 2: 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.

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.

Page 3: 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.

A queue of passengers in a ticket line

Brown

E(conomy)

2

Watson

B(usiness)

1

2

q.frontp

q.sizeq.rearp

Page 4: 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.

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;

Page 5: 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.

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);

}

Page 6: 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.

Addition of one passenger to a Queue• Add Carson:

Carson

E(irstClass)

2

Watson

B(usines)

1

Brown

E(conomy)

2q.frontp

q.sizeq.rearp

3

add_to_q(&q, next_pass);

After

Brown

E(conomy)

2

Watson

B(usiness)

1

2

q.frontp

q.sizeq.rearp

Before

Page 7: 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.

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); }

Page 8: 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.

Removal of one passenger from a Queue• Remove Brown:

Carson

E(irstClass)

2

Watson

B(usines)

1

Brown

E(conomy)

2q.frontp

q.sizeq.rearp

3Carson

E(irstClass)

2

Watson

B(usines)

1

Brown

E(conomy)

2q.frontp

q.sizeq.rearp

3 During function Call

Before

to_freep

Page 9: 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.

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);

Page 10: 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.

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;

Page 11: 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.

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;

Page 12: 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.

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);

}

Page 13: 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.

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.

Page 14: 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.

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

Page 15: 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.

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;

Page 16: 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.

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.

Page 17: 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.

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.

Page 18: 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.

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.

Page 19: 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.

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

4

old_listp new_key new_listp

4

Page 20: 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.

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;

}

4

old_listp new_key new_listp

4

5 8

Simple Case 2

Page 21: 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.

Recursive Function insert_in_order else

{

new_listp = old_listp;

new_listp->restp = insert_in_order(old_listp->restp, new_key);

}

return (new_listp);

}

6 6

85

old_listp new_key

new_listp is old_listp with circled component changed to

8

8

which is the result of inserting 6 in order in

Recursive Step

Page 22: 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.

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); }

Page 23: 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.

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;

Page 24: 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.

Recommended