Deque Flashcards
1
Q
Deque pushBack
A
void pushBack(T value) { if (isFull()) { cout << "Deque is full." << endl; return; } else if (isEmpty()) { frontIndex = 0; backIndex = 0; } else { backIndex = (backIndex + 1) % MAXSIZE; } deque[backIndex] = value; }
2
Q
Deque pushFront
A
void pushFront(T value) { if (isFull()) { cout << "Deque is full" << endl; return; } else if (isEmpty()) { frontIndex = 0; backIndex = 0; } else { frontIndex = (frontIndex - 1 + MAXSIZE) % MAXSIZE; } deque[frontIndex] = value; }
3
Q
Deque popFront
A
void popFront() { if (isEmpty()) { cout << "Deque is empty." << endl; return; } else if (frontIndex == backIndex) { frontIndex = -1; backIndex = -1; } else { frontIndex = (frontIndex + 1) % MAXSIZE; } }
4
Q
Deque popBack
A
void popBack() { if (isEmpty()) { cout << "Deque is empty " << endl; return; } else if (frontIndex == backIndex) { frontIndex = -1; backIndex = -1; } else { backIndex = (backIndex - 1 + MAXSIZE) % MAXSIZE; } }
5
Q
Deque peekFront
A
T front() const { if (isEmpty()) { cout << "Deque is empty." << endl; return T(); } return deque[frontIndex]; }
6
Q
Deque peekBack
A
T back() const { if (isEmpty()) { cout << "Deque is empty." << endl; return T(); } return deque[backIndex]; }
7
Q
Deque isEmpty
A
bool isEmpty() const { return frontIndex == -1; }
8
Q
Deque isFull
A
bool isFull() const { return (backIndex + 1) % MAXSIZE == frontIndex; }
9
Q
Deque clear
A
void clear() { frontIndex = -1; backIndex = -1; }
10
Q
Deque display
A
template <typename T> void display(const Deque<T>& d) { if (d.isEmpty()) { cout << "Deque is empty" << endl; return; } Deque<T> temp = d; while (!temp.isEmpty()) { cout << temp.front() << " "; temp.popFront(); } cout << endl; }
11
Q
Deque reverse
A
template <typename T> void reverse(Deque<T>& d) { if (d.isEmpty()) { cout << "Deque is empty" << endl; return; } stack<T> s; while (!d.isEmpty()) { s.push(d.front()); d.popFront(); } while (!s.empty()) { d.pushBack(s.top()); s.pop(); } }