Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer

Similar presentations


Presentation on theme: "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer"— Presentation transcript:

1 Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer IUPUIdroberts@cs.iupui.edu

2 Dale Roberts 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 Dale Roberts DYNAMICALLY LINKED STACKS AND QUEUES   NULL    top element link (a) Linked Stack   NULL    front element link (b) Linked queue rear *Figure 4.10: Linked Stack and queue (p.147)

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

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

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

7 Dale Roberts 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. 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. Our Fraction ADT is a first-class ADT.

8 Dale Roberts First-class Queue ADT template template class QUEUE { private: private: // Implementation-dependent code // Implementation-dependent code public: public: QUEUE(int); QUEUE(int); QUEUE(const QUEUE&); QUEUE(const QUEUE&); QUEUE& operator=(const QUEUE&); QUEUE& operator=(const QUEUE&); ~QUEUE(); ~QUEUE(); int empty() const; int empty() const; void put(Item); void put(Item); Item get(); 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 Dale Roberts 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.


Download ppt "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer"

Similar presentations


Ads by Google