1 / 9

Department of Computer and Information Science, School of Science, IUPUI

Department of Computer and Information Science, School of Science, IUPUI. CSCI 240. Abstract Data Types Queues. Dale Roberts, Lecturer IUPUI droberts@cs.iupui.edu. Queues. Queue Similar to a supermarket checkout line

charity
Download Presentation

Department of Computer and Information Science, School of Science, IUPUI

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. Department of Computer and Information Science,School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer IUPUI droberts@cs.iupui.edu

  2. Queues • Queue • Similar to a supermarket checkout line • First-in, first-out (FIFO) • Nodes are removed only from the head • Nodes are inserted only at the tail • Insert and remove operations • Enqueue (insert) and dequeue (remove) • Queue Model • queue is a list, with insertion done only at one end and deletion done at the other end. • Linked list implementation of queues • operating as a list • constant time for enqueue & dequeue (keeping pointer to both the head and tail of the list)

  3. top front element link element link           NULL NULL (a) Linked Stack (b) Linked queue rear DYNAMICALLY LINKED STACKS AND QUEUES *Figure 4.10: Linked Stack and queue (p.147)

  4. FIFO queue ADT interface template <class Item> class QUEUE { private: // Implementation-dependent code public: QUEUE(int); int empty(); void put(Item); Item get(); };

  5. FIFO queue linked-list implementation template <class Item> class QUEUE { private: struct node { Item item; node* next; node(Item x) { item = x; next = 0; } }; typedef node *link; link head, tail; public: QUEUE(int) { head = 0; } int empty() const { return head == 0; } void put(Item x) { link t = tail; tail = new node(x); if (head == 0) head = tail; else t->next = tail; } Item get() { Item v = head->item; link t = head->next; delete head; head = t; return v; } };

  6. FIFO queue array implementation We can implement get and put operations for the FIFO queue ADT in constant time, using either arrays or linked-lists. template <class Item> class QUEUE { private: Item *q; int N, head, tail; public: QUEUE(int maxN) { q = new Item[maxN+1]; N = maxN+1; head = N; tail = 0; } int empty() const { return head % N == tail; } void put(Item item) { q[tail++] = item; tail = tail % N; } Item get() { head = head % N; return q[head++]; } }; If head = tail, then empty; if put would make them equal, then full. Array is 1 larger to allow checks.

  7. First-class ADT Our Fraction ADT is a first-class ADT. • Sedgewick Definition 4.4: A first-class data type is one for which we can have potentially many different instances, and which we can assign to variables which we declare to hold the instances.

  8. First-class Queue ADT template <class Item> class QUEUE { private: // Implementation-dependent code public: QUEUE(int); QUEUE(const QUEUE&); QUEUE& operator=(const QUEUE&); ~QUEUE(); int empty() const; void put(Item); Item get(); }; Notice how each and every interface operations now includes a references to a particular Q. We can create as many queues as we need.

  9. Acknowledgements • All of this code is from Horowitz, Sahni, and Anderson-Freed, Fundamentals of Data Structures in C. • Some slides were originally developed by Chen, Hsin-His.

More Related