1 / 14

Queues

Queues. CS-2301 System Programming Concepts Hugh C. Lauer (Slides include materials from The C Programming Language , 2 nd edition, by Kernighan and Ritchie, and from C: How to Program , 5 th and 6 th editions, by Deitel and Deitel). Definition – Linked List.

starbuck
Download Presentation

Queues

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. Queues CS-2301 System Programming ConceptsHugh C. Lauer (Slides include materials fromThe C Programming Language, 2nd edition, by Kernighan and Ritchie,and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) Queues

  2. Definition – Linked List • A linear data structure in which each element contains a pointer to the next element • (Usually) has a beginning and an end • Items can (sometimes) be added anywhere • Beginning • Middle • End • Items can be removed from anywhere Queues

  3. Note • Array is also a linear data structure with a beginning and an end • No pointers to next item • Implicit in ++ and -- operators • Very difficult to add items to beginning or middle! Queues

  4. More Definitions • Queue:– a linear data structure in which items are always added to end and removed from beginning • FIFO:– First-in, First-out • Stack:– a linear data structure in which items are always added to the beginning and also removed from beginning • LIFO:– Last-in, First-out • Neither stacks nor queues need to be implemented via linked lists Queues

  5. Programming Assignment #5(CS2301, B-term 11) • Provide two different implementations of a queue • Linked list • Fixed-size array • Note that fixed-size array cannot hold an arbitrarily long queue Queues

  6. Queue.h #ifndef QUEUE_H #define QUEUE_H #include "Simulation.h" #if defined QUEUE_ELEMENT /* The actual interface definition for the queue begins here and uses QUEUE_ELEMENT as a type */ intqueueEmpty(void); /* returns TRUE if the queue is empty, FALSE if not */ intenqueue(QUEUE_ELEMENT e); /* Adds a copy of the argument to the tail of the queue. Returns TRUE if successful, FALSE if not */ intdequeue(QUEUE_ELEMENT *e); /* Removes the first element from the queue and stores it into the location pointed to by e. Returns TRUE if successful, FALSE if not */ intmaxQueueLength(void); /* Returns the maximum number of elements that have been stored in the queue at one time */ intcurrentQueueLength(void); /* Returns the number of elements currently in the queue */ // End of interface definition #endif#endif Queues

  7. QUEUE_ELEMENT is defined in Simulation.h as typedefstruct customer Customer; #define QUEUE_ELEMENT Customer Queue.h #ifndef QUEUE_H #define QUEUE_H #include "Simulation.h" #if defined QUEUE_ELEMENT /* The actual interface definition for the queue begins here and uses QUEUE_ELEMENT as a type */ intqueueEmpty(void); /* returns TRUE if the queue is empty, FALSE if not */ intenqueue(QUEUE_ELEMENT e); /* Adds a copy of the argument to the tail of the queue. Returns TRUE if successful, FALSE if not */ intdequeue(QUEUE_ELEMENT *e); /* Removes the first element from the queue and stores it into the location pointed to by e. Returns TRUE if successful, FALSE if not */ intmaxQueueLength(void); /* Returns the maximum number of elements that have been stored in the queue at one time */ intcurrentQueueLength(void); /* Returns the number of elements currently in the queue */ // End of interface definition #endif#endif Queues

  8. structlistItem {typeQUEUE_ELEMENT;structlistItem *next; }; structlistItem *head, *tail; • QUEUE_ELEMENT • QUEUE_ELEMENT • QUEUE_ELEMENT QUEUE_ELEMENT next next next next Linked List Implementation Classroom exercise:– Add an item to tail ofnon-empty queue Classroom exercise:– Add an item to tail ofempty queue Lists and Trees

  9. structlistItem {typeQUEUE_ELEMENT;structlistItem *next; }; structlistItem *head, *tail; • QUEUE_ELEMENT • QUEUE_ELEMENT • QUEUE_ELEMENT QUEUE_ELEMENT next next next next Linked List Implementation Classroom exercise:– Remove an item fromnon-empty queue Lists and Trees

  10. Questions about Linked List Implementation? Queues

  11. Fixed Array Implementation • If maximum queue length is always less than N, then we can implement the queue with an array of size N • How? Queues

  12. Fixed Array Implementation (continued) … QUEUE_ELEMENT QUEUE_ELEMENT QUEUE_ELEMENT QUEUE_ELEMENT QUEUE_ELEMENT QUEUE_ELEMENT QUEUE_ELEMENT QUEUE_ELEMENT • Classroom exercises:– • How to represent head of queue? • How to represent tail of queue? N Queues

  13. Fixed Array Implementation (continued) … QUEUE_ELEMENT QUEUE_ELEMENT QUEUE_ELEMENT QUEUE_ELEMENT QUEUE_ELEMENT QUEUE_ELEMENT QUEUE_ELEMENT QUEUE_ELEMENT • Classroom exercises (continued):– • How to add an item to the queue? • How to remove an item from the queue? • What error checking is needed? N Queues

  14. Questions? Queues

More Related