-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdebug_me.cc
More file actions
129 lines (105 loc) · 2.75 KB
/
Copy pathdebug_me.cc
File metadata and controls
129 lines (105 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// compile with: g++ -Wall -ggdb -o debug_me debug_me.cc
#include <iostream>
using namespace std;
int number_instantiated = 0;
template <class T> class Item {
public:
Item (const T &value, Item<T> *next = 0) : value_(value), next_(next) {
cout << "Creating Item, "
<< ++number_instantiated
<< " currently in existence." << endl;
}
~Item () {
cout << "Destroying Item, "
<< --number_instantiated
<< " currently in existence." << endl;
next_ = 0;
}
Item<T>* next () const { return next_; }
void next (Item<T> *new_next) { next_ = new_next; };
const T& value () const { return value_; }
void value (const T &value) { value_ = value; }
private:
Item ();
T value_;
Item<T> *next_;
};
template <class T> class LinkedList {
public:
LinkedList () : head_(0) {};
~LinkedList () { delete_nodes (); };
// returns 0 on success, -1 on failure
int insert (const T &new_item) {
return ((head_ = new Item<T>(new_item, head_)) != 0) ? 0 : -1;
}
// returns 0 on success, -1 on failure
int remove (const T &item_to_remove) {
Item<T> *marker = head_;
Item<T> *temp = 0; // temp points to one behind as we iterate
while (marker != 0) {
if (marker->value() == item_to_remove) {
if (temp == 0) { // marker is the first element in the list
if (marker->next() == 0) {
head_ = 0;
delete marker; // marker is the only element in the list
marker = 0;
} else {
head_ = new Item<T>(marker->value(), marker->next());
delete marker;
marker = 0;
}
return 0;
} else {
temp->next (marker->next());
delete temp;
temp = 0;
return 0;
}
}
marker = 0; // reset the marker
temp = marker;
marker = marker->next();
}
return -1; // failure
}
void print() {
Item<T> *marker = head_;
cout << "Current state of list:" << endl;
do {
cout << marker->value() << endl;
marker = marker->next();
} while (marker != 0);
cout << endl;
}
private:
void delete_nodes() {
Item<T> *marker = head_;
while (marker != 0) {
Item<T> *temp = marker;
delete marker;
marker = temp->next();
}
}
Item<T> *head_;
};
int main(int argc, char* argv[]) {
LinkedList<int> *list = new LinkedList<int> ();
list->insert (1);
list->insert (2);
list->insert (3);
list->insert (4);
list->print();
cout << endl << "Now removing elements:" << endl;
list->remove(2);
list->print();
list->remove(4);
list->print();
list->remove(1);
list->print();
list->remove(2);
list->print();
list->remove(3);
list->print();
delete list;
return 0;
}