exam 3 definitions and code questions Flashcards

(78 cards)

1
Q

what are the private variables of the Itr class?

A

dnode<T>* current</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what are the public members of the dnode class used in the Itr class?

A

T data;
dnode<T>* next;
dnode<T>* prev;</T></T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is the relationship of the List class to the iterator class?

A

the List class is declared a friend class of Itr in its public section

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

write the in-line definition of the default constructor for the Itr class

A

Itr(): current(0) {};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

write the in-line definition of the constructor that takes a dnode parameter for the Itr class

A

Itr(dnode<T> *ptr): current(ptr) {};</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

write the in-line non-const operator* (functions as the dereference operator) for the Itr class

A

T& operator*() {return current->data;};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

write the in-line const operator* (functions as the dereference operator) for the Itr class

A

T operator*() {return current->data;};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is the difference in the function definitions for ++i and i++ (also –i and i–) for the Itr class?

A

i++ and i– have an int dummy parameter to let the compiler know it is ++i and not i++. Also, all the pre-increments return a reference, and all the post-increments do not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

write the in-line definition for the pre-increment operator for the Itr class

A

Itr<T>& operator++() {
if(current) current = current ->next;
return *this;
}</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

write the in-line definition for the post-increment operator for the Itr class

A

Itr<T> operator++(int){
Itr<T> result = current;
if(current) current = current->next;
return result;
}</T></T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

write the in-line definition for the pre-decrement operator for the Itr class

A

Itr<T>& operator--(){
if(current) current = current->prev;
return *this;
}</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

write the in-line definition for the post-decrement operator for the Itr class

A

Itr<T> operator--(int){
Itr<T> result = current;
if(current) current = current->prev;
return result;
}</T></T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what requirements need to be met in order to overload the points at (->) operator?

A

in order to overload, the operator must be a method, not a free function, and must return a pointer or object of that class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

write the in-line definition for the non-const points to operator (operator->) for the Itr class

A

dnode<T>* operator->() {return current;};</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

write the in-line definition for the const points to operator (operator->) for the Itr class

A

const dnode<T>* operator->() {return current;};</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

write the in-line definition of the operator== for the Itr class

A

bool operator==(Itr<T> rhs) const {return current == rhs.current;};</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

write the in-line definition of the operator!= for the Itr class

A

bool operator!=(Itr<T> rhs) const {return !(current == rhs.current);</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

what are the private variables of the List class?

A

dnode<T>* beginning;
dnode<T>* ending;</T></T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

write the in-line default constructor for the List class

A

List(): beginning(0), ending(0) {};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

write the in-line operator= for the List class

A

operator= (List<T> rhs) {swap(rhs); return *this;};</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

write the in-line definition for the function isEmpty for the List class

A

bool isEmpty() const {return beginning == 0;};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

write the non-const in-line definition for the function begin for the List class that returns an iterator pointing to the beginning of the list.

A

Itr<T> begin() {return Itr<T>(beginning);};</T></T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

write the const in-line definition for the function begin for the List class that returns an iterator pointing to the beginning of the list.

A

const Itr<T> begin() const {return Itr<T>(beginning);};</T></T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

write the non-const in-line definition for the function end for the List class that returns an iterator pointing to the ending of the list.

A

Itr<T> end() {return Itr<T>(ending);};</T></T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
write the const in-line definition for the function begin for the List class that returns an iterator pointing to the ending of the list.
const Itr end() const {return Itr(ending);};
26
write the non-const in-line definition for the function front for the List class that returns the front element of the list
T& front() {return beginning->data;};
27
write the const in-line definition for the function begin for the List class that returns the front element of the list.
const T front() {return beginning->data;};
28
write the non-const in-line definition for the function back for the List class that returns the last element of the list.
T& back() {return ending->data;};
29
write the const in-line definition for the function back for the List class that returns the last element of the list.
const T back() {return ending->data;};
30
write the insertAfter method for the List class
template void List::insertAfter(Itr ptr, const T& item){ dnode *temp = new dnode(item); if(beginning == 0){ //list is empty beginning = temp; ending = temp; } else if(ptr == ending){ //add to the end of the list temp->prev = ending; ending->next = temp; ending = temp; } else{ //add to the middle of the list temp->next = ptr->next; temp->prev = ptr.current; ptr->next->prev = temp; ptr->next = temp; } }
31
write the insertBefore method for the List class
template void List::insertBefore(Itr ptr, const T& item){ dnode *temp = new dnode(item); if(beginning == 0){ beginning = temp; ending = temp; } else if(ptr == beginning){ //insert before beginning beginning->prev = temp; temp->next = beginning; beginning = temp; } else{ temp->next = ptr.current //point to the same thing ptr points to temp->prev = ptr->prev; ptr->prev->next = temp; ptr->prev = temp; } }
32
write the remove method for the List class. include requires and ensures
//REQUIRES: !isEmpty() && ptr->x2 && beg -> x1 <-> x2 <-> x3 <- end //ENSURES: beg -> x1 <-> x3 <- end template void List::remove(Itr ptr){ if(isEmpty()) return; if(ptr == 0) return; if(ptr == beginning) beginning = beginning->next; else ptr->prev->next = ptr->next; if(ptr == ending) ending = ending->prev; else ptr->next->prev = ptr->prev; delete ptr.current; }
33
write the length method for the List class
template int List::length() const{ int result = 0; Itr i = begin(); while(i != 0){ ++result; ++i; } return result; }
34
write the destructor for the List class
template List::~List(){ dnode *temp; while(beginning != 0){ temp = beginning; beginning = beginning->next; delete temp; } }
35
write the copy constructor for the List class
template List::List(const List& actual): List(){ if(actual.beginning == 0) return; beginning = new dnode(actual.beginning->data); ending = beginning; Itr = actual.beginning->next; while(i != 0){ ending->next = new dnode(i->data); ending->next->prev = ending; ending = ending->next; ++i; } }
36
make the swap method for the List class
template void List::swap(List rhs){ dnode *temp = beginning; beginning = rhs.beginning; rhs.beginning = temp; temp = ending; ending = rhs.ending; rhs.ending = temp; }
37
write the find method for the List class
template const Itr List::find(const T& key) const{ Itr result = begin(); while(result != 0){ if(result->data == key){ return result; } ++result; } return Itr(0) //like returning a -1 }
38
overload operator[] as a method for the List class
Itr List::operator[](int n){ Itr result = begin(); int i = 0; while(i < n && result != 0){ ++i; ++result; } return result; //will either be a zero or pointing to the nth item in the list }
39
what are the public members of class bnode?
bnode *left; bnode *right; T data;
40
the bnode class goes with what other class?
the btree class
41
write the in-line default constructor for the bnode class
bnode() {};
42
write the in-line constructor for the bnode class that takes a T item as a parameter
bnode(const T& item): data(item) {};
43
what are the private variables of the btree class?
T data; bool empty; btree *left; btree *right;
44
write the in-line default constructor for the btree class
btree(): empty(true), left(0), right(0) {};
45
write the in-line constructor for the btree class that takes a T item as a parameter
btree(const T& item): data(item), empty(false), left(0), right(0) {};
46
write the in-line overload for operator= for the btree class
btree& operator=(btree rhs) {swap(rhs); return *this;};
47
write the in-line method isEmpty for the btree class
bool isEmpty() const {return empty;};
48
write the bfind method for the btree class
template bool btree::find(const T& key) const{ if(key == data) return true; if(key < data){ if(left) return left->bfind(key); else return false; } else{ if(right) return right->bfind(key); else return false; } }
49
write the binsert method for the btree class
template void btree::binsert(const T& x){ if(empty){ data = x; empty = false; } else if(x < data){ //put in left subtree if(left) left->binsert(x); else left = new btree(x); } else { //put in right subtree if(right) right->binsert(x); else right = new btree(x); } }
50
write the inorder method for the btree class
template void btree::inorder(std::ostream& out) const{ if(!empty){ if(left) left->inorder(out); out << data; if(right) right->inorder(out); }
51
write the preorder method for the btree class
template void btree::preorder(std::ostream& out) const{ if(!empty){ out << data; if(left) left->inorder(out); if(right) right->inorder(out); }
52
write the postorder method for the btree class
template void btree::postorder(std::ostream& out) const{ if(!empty){ if(left) left->inorder(out); if(right) right->inorder(out); out << data; }
53
write the copy constructor for the btree class
template btree::btree(const btree& actual): btree(){ //do a preorder traversal data = actual.data; empty = actual.empty; if(actual.left){ left = new btree(*(actual.left)); //need a btree, not a pointer to one } if(actual.right){ right = new btree(*(actual.right)); } }
54
write the destructor for the btree class
template btree::~btree(){ //do a postorder traversal if(left) delete left; //recursively calls dtor if(right) delete right; //recursively calls dtor }
55
write the swap method for the btree class
template void btree::swap(btree& rhs){ btree* ptr = left; left = rhs.left; rhs.left = ptr; ptr = right; right = rhs.right; rhs.right = ptr; T temp = data; data = rhs.data; rhs.data = temp; bool tmp = empty; empty = rhs.empty; rhs.empty = tmp; }
56
write the predecessor function for the btree class
template T btree::predecessor(){ //predecessor will either be a leaf or have a left subtree if(right) return right->predecessor(); return data; }
57
what are the private members of the bnode2 class?
T data; bnode *left; bnode *right;
58
the bnode2 class goes with what other class?
the btree2 class
59
write the in-line default constructor for the bnode2 class
bnode(): left(0), right(0) {};
60
write the constructor for the bnode2 class that takes a T item as a parameter
bnode(const T& x): data(x), left(0), right(0) {};
61
write the find method for the bnode2 class
template bool bnode::find(const T& key){ if(key == data) return true; if(key < data){ if(left) return left->find(key); else return false; } else{ if(right) return right->find(key); else return false; } }
62
write the insert method for the bnode2 class
template void bnode::insert(const T& x){ if(x == data) return; //no duplicates if(x < data){ if(left) left->insert(x); else left = new bnode(x); } else{ if(right) right->insert(x); else right = new bnode(x); } }
63
write the destructor for the bnode2 class
template bnode::~bnode(){ if(left) delete left; if(right) delete right; }
64
write the copy constructor for the bnode2 class
template bnode::bnode(const bnode& actual): bnode(){ data = actual.data; if(actual.left) left = new bnode(*(actual.left)); if(actual.right) right = new bnode(*(actual.right)); }
65
write the remove method for the bnode2 class
template bnode* bnode::remove(const T& x){ if(x == data){ //found node to remove if(!left && !right){ //has no children delete this; return 0; } if(!left && right){ //has only right child bnode *temp = right; right = 0; delete this; return temp; } if(left && !right){ //has only left child bnode *temp = left; left = 0; delete this; return temp; } //two children data = left->predecessor(); left = left->remove(data); return this; } else if(x < data){ //remove from left subtree left = left->remove(x); } else{ remove from right subtree right = right->remove(x); } return this; }
66
what are the private variables of the btree2 class?
bnode2 *root;
67
write the in-line default constructor for the btree2 class
btree(): root(0) {};
68
write the in-line definition for the overloaded operator= as a method for the btree2 class
btree& operator=(btree& rhs) {swap(rhs); return *this;};
69
write the in-line definition for the isEmpty method for the btree2 class
bool isEmpty() const {return root == 0;};
70
write the find method for the btree2 class
template bool btree::find(const T& key){ if(!root) return false; return root->find(key) //calls bnode2 find method
71
write the insert method for the btree2 class
template void btree::insert(const T& x){ if(root == 0) { //insert at root if root empty root = new bnode(x); } else{ root->insert(x); } }
72
write the destructor for the btree2 class
template btree::~btree(){ delete root; }
73
write the copy constructor for the btree2 class
template btree::btree(const btree& actual){ if(actual.root){ root = new bnode(*(actual.root)); } }
74
write the swap method for the btree2 class
template void btree::swap(btree& rhs){ bnode *temp = root; root = rhs.root; rhs.root = temp; }
75
write the remove method for the btree2 class
template void btree::remove(const T& x){ if(find(x)) root = root->remove(x); }
76
with inheritance, if you have a virtual method what else do you need in the base class?
a virtual destructor, even if it does nothing
77
how do you declare a child class?
class ChildName: public ParentName { ... };
78
what does the protected keyword do?
it allows the children of a parent class to use the protected variable, but the variable is private to any other class or function