Download presentation
Presentation is loading. Please wait.
Published byJulian Singleton Modified over 9 years ago
1
Generic Positional Containers and Double-Ended Queues
2
Generic Positional Containers A generic container C which is Organized and accessed by position vector[i] = ‘a’; list.insert(I, ‘a’); Can insert any T object at any position in C Can remove any T object at any position in C Can support push_front(), pop_front(), front() push_back(), pop_back(), back() Examples: List, Deque
3
Double-Ended Queue Deque (pronounced ‘Deck’) Deque operations Push/Pop at either end Retrieve data from either end Proper type
4
Specifying Deque Requirements for Deque O(1) average runtime, O(size()) space push_front(t), pop_front(), front() push_back(t), pop_back(), back() O(1) time and space for iterator operations Random access iterator ([])
5
Deque Implementation Plan Circular array Protected array content of size content_size Content wraps around the end of the array to the beginning. Illusion: content[content_size] == content[0]
6
Deque D Illustrated content_size = 8 D.empty() == true 01234567 beginend content
7
Deque D Illustrated (2) content_size = 8 D.push_back(‘M’) 01234567 M beginend content
8
Deque D Illustrated (3) content_size = 8 D.push_back(‘e’) 01234567 Me beginend content
9
Deque D Illustrated (4) content_size = 8 D.push_back(‘r’) 01234567 Mer beginend content
10
Deque D Illustrated (5) content_size = 8 D.push_back(‘r’) 01234567 Merr beginend content
11
Deque D Illustrated (6) content_size = 8 D.push_back(‘y’) 01234567 Merry beginend content
12
Deque D Illustrated (7) content_size = 8 D.pop_front() O(1) 01234567 erry beginend content
13
Deque D Illustrated (8) content_size = 8 D.pop_front() 01234567 rry beginend content
14
Deque D Illustrated (9) content_size = 8 D.push_back(‘G’) 01234567 rryG beginend content
15
Deque D Illustrated (10) content_size = 8 D.push_back(‘o’) D.size() == (7 – 2 + 8) % 8 01234567 rryGo beginend content
16
Deque D Illustrated (11) content_size = 8 D.push_back(‘A’) D.size() = (0 – 2 + 8) % 8 01234567 rryGoA beginend content
17
Deque D Illustrated (12) content_size = 8 D.push_back(‘r’) D.size() = (1 – 2 + 8) % 8 01234567 rrryGoA beginend content
18
Deque D Illustrated (13) D.size() == content_size – 1 Now what? Return full or Double the capacity (as with Vector). 01234567 rrryGoA beginend content
19
Deque Implementation Plan (2) Relative Indexing Protected integers begin, end Bracket [] Operator Similar to Vector Element position relative to begin Front element is content[begin] Back element is content[end – 1] Size is (end – begin + content_size) % content_size
20
Deque ::iterator Implementation Plan Public interface Start with the public interface of List ::iterator Add bracket operator
21
Defining Deque template class Deque { public: typedef T value_type; // type definitions typedef DequeIterator iterator; Deque(); // constructors and deconstructor Deque(size_t, const T&); Deque(const Deque &); ~Deque(); // display functions void Display(ostream& os, char ofc = ‘\0’) const; void Dump(ostream& os) const;
22
Defining Deque (2) int empty() const; // container read-only routines size_t size() const; T& front() const; T& back() const; T& operator[] (size_t) const; int push_front(const T&); // container write routines int pop_front(); int push_back(const T&); int pop_back(); Deque & operator=(const Deque &);
23
Defining Deque (3) friend class DequeIterator ; // iterator support iterator begin() const; iterator end() const; protected: T* content; size_t content_size, begin, end; };
24
Defining Deque (4) // operator overloads (friend status not required) template ostream& operator & a); template int operator==(const Deque &, const Deque &); template int operator!=(const Deque &, const Deque &);
25
Defining DequeIterator template class DequeIterator { friend class Deque ; public: typedef T value_type; // terminology support DequeIterator(); // constructors DequeIterator(const Deque & I); DequeIterator(const DequeIterator & I); DequeIterator(const size_t& i); T& retrieve() const; // return ptr to current Tval int valid() const; // cursor is valid element
26
Defining DequeIterator (2) // various operators int operator==(const DequeIterator & I2) const; int operator!=(const DequeIterator & I2) const; T& operator*() const; // return reference to current Tval T& operator[] (size_t i) const; //return reference to Tval at index+i DequeIterator & operator=(const DequeIterator & I); DequeIterator & operator++(); // prefix DequeIterator operator++(int); // postfix DequeIterator & operator--(); // prefix DequeIterator operator--(int); // postfix
27
Defining DequeIterator (3) // pointer arithmetic long operator-(const DequeIterator & I2) const; DequeIterator & operator+=(long n); DequeIterator & operator-=(long n); DequeIterator operator+(long n) const; DequeIterator & operator+=(int n); DequeIterator & operator-=(int n); DequeIterator operator+(int n) const; DequeIterator & operator+=(unsigned long n); DequeIterator & operator-=(unsigned long n); DequeIterator operator+(unsigned long n) const; DequeIterator & operator+=(unsigned int n); DequeIterator & operator-=(unsigned int n); DequeIterator operator+(unsigned int n) const;
28
Defining DequeIterator (3) protected: const Deque * Qptr; size_t index; };
29
Implementing Deque Default constructor template Deque ::Deque() : content(0), begin(0), end(0), content_size(0) { content = new T[default_content_size]; if (content == 0) { // error } content_size = default_content_size; }
30
Implementing Deque (2) Copy constructor template Deque ::Deque(const Deque & Q) : content_size(Q.content_size), begin(Q.begin), end(Q.end) { content = new T[content_size]; if (content == 0) { // error } for (size_t j = 0; j < content_size; j++) { content[j] = Q.content[j]; }
31
Implementing Deque (3) Read-only functions template size_t Deque ::size() const { return (end – begin + content_size) % content_size; } template T& Deque ::operator[] (size_t i) const { if (size() <= i) { // error } return content[(i + begin) % content_size]; }
32
Implementing Deque (4) Display functions template void Deque ::Display(ostream& os, char ofc) const { for (size_t j = 0; j < size(); ++j) { os << operator[](j); if (ofc != ‘\0’) { os << ofc; } template void Deque ::Dump(ostream& os) const { for (size_t j = 0; j < content_size; ++j) { // print } }
33
Implementing Deque (5) Read-only operator overloads template ostream operator & Q) { Q.Display(os); return(os); } template int operator==(const Deque & Q1, const Deque & Q2) { if (Q1.size() != Q2.size()) { return 0; } for (size_t j = 0; j < Q1.size(); ++j) { if (Q1[j] != Q2[j]) { return 0; } } return 1; }
34
Implementing Deque (6) Read-only operator overloads template int operator!=(const Deque & Q1, const Deque & Q2) { return !(Q1 == Q2); }
35
Implementing Deque (7) Read-only functions template int Deque ::empty() const { return begin == end; } template void Deque ::Clear() { begin = end = 0; }
36
Implementing Deque (8) Read-only functions template T& Deque ::front() const { // check for empty Tdeque… return content[begin]; } template T& Deque ::back() const { // check for empty Tdeque… if (end == 0) return content[content_size - 1]; return content[end - 1]; }
37
Implementing Deque (9) Iterator support template DequeIterator Deque ::begin() const { Deque ::iterator I; I.Qptr = this; I.index = 0; return I; } template DequeIterator Deque ::end() const { Deque ::iterator I; I.Qptr = this; I.index = size(); return I; }
38
Implementing Deque (10) Assignment template Deque & Deque ::operator=(const Deque & Q) { if (this != &Q) { T* newcontent = new T[Q.content_size]; // check for allocation delete[] content; content = newcontent; content_size = Q.content_size; begin = Q.begin; end = Q.end; // copy queue elements } return *this; }
39
Implementing Deque (11) push_back template int Deque ::push_back(const T& Tval) { if (size() + 1 >= content_size) { // deque is full unsigned j, k; size_t newcontent_size = 2 * content_size; if (content_size == 0) newcontent_size = 2; T* newcontent = new T[newcontent_size]; // check for allocation error for (j = k = begin; j != end; j = (j + 1) % content_size, ++k) { newcontent[k] = content[j]; }
40
Implementing Deque (12) push_back if (end < begin) {end += content_size; } delete[] content; content = newcontent; content_size = newcontent_size; } content[end] = Tval; end = (end + 1) % content_size; return 1; }
41
Implementing Deque (13) push_front template int Deque ::push_front(const T& Tval) { if (size() + 1 >= content_size) { // deque is full unsigned j, k; size_t newcontent_size = 2 * content_size; if (content_size == 0) newcontent_size = 2; T* newcontent = new T[newcontent_size]; // check for allocation error for (j = k = begin; j != end; j = (j + 1) % content_size, ++k) { newcontent[k] = content[j]; }
42
Implementing Deque (14) push_front if (begin < end) { begin += content_size; } delete[] content; content = newcontent; content_size = newcontent_size; } begin = (begin – 1 + content_size) % content_size; content[begin] = Tval; return 1; }
43
Implementing Deque (15) Pop routines template int Deque ::pop_front() { if (begin == end) return 0; begin = (begin + 1) % content_size; return 1; } template int Deque ::pop_back() { if (begin == end) return 0; end = (end – 1 + content_size) % content_size; return 1; }
44
Implementing DequeIterator Constructors template DequeIterator ::DequeIterator() : Qptr(0), index(0) { } template DequeIterator ::DequeIterator(const Deque & Q) : Qptr(&Q), index(0) { } template DequeIterator ::DequeIterator(const DequeIterator & I) : Qptr(I.Qptr), index(I.index) { }
45
Implementing DequeIterator (2) Initialization routines template void DequeIterator ::Initialize(const Deque & Q) { Qptr = &Q; index = 0; } template void DequeIterator ::rInitialize(const Deque & Q) { Qptr = &Q; index = Q.size() – 1; }
46
Implementing DequeIterator (3) Helper functions template int DequeIterator ::valid() const { if (Qptr == 0) return 0; if (index >= Qptr->size()) return 0; return 1; } template T& DequeIterator ::operator[] (size_t i) const { if (!Qptr) { // error } return Qptr->operator[](index + i); }
47
Implementing DequeIterator (4) Helper functions template T& DequeIterator ::retrieve() const { // check for validity return Qptr->operator[](index); } template T& DequeIterator ::operator* () const { if (Qptr == 0) { // error } if (Qptr->size() == 0) { // error } return Qptr->operator[](index); }
48
Implementing DequeIterator (5) Comparators template int DequeIterator ::operator==(const DequeIterator & I2) const { if (Qptr != I2.Qptr) return 0; if (index != I2.index) return 0; return 1; } template int DequeIterator ::operator!=(const DequeIterator & I2) const { return !(*this == I2); }
49
Implementing DequeIterator (6) Assignment template DequeIterator & DequeIterator ::operator=(const DequeIterator & I) { if (this != &I) { Qptr = I.Qptr; index = I.index; } return *this; }
50
Implementing DequeIterator (7) Various operators template DequeIterator & DequeIterator ::operator++() { ++index; return *this; } template DequeIterator DequeIterator ::operator++(int) { DequeIterator I(*this); operator ++(); return I; }
51
Implementing DequeIterator (8) Various operators template DequeIterator & DequeIterator ::operator--() { --index; return *this; } template DequeIterator DequeIterator ::operator--(int) { DequeIterator I(*this); operator --(); return I; }
52
Implementing DequeIterator (9) Various operators template long DequeIterator ::operator-(const DequeIterator & I2) const { return index – I2.index; } template DequeIterator DequeIterator ::operator+(long n) const { DequeIterator I(*this); return I += n; }
53
Implementing DequeIterator (10) Various operators template DequeIterator & DequeIterator ::operator+=(long n) { index += n; return *this; } template DequeIterator & DequeIterator ::operator-=(long n) { index -= n; return *this; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.