-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
134 lines (111 loc) · 2.9 KB
/
main.cc
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
130
131
132
133
134
// main.cc
// Andrew Gilpin
// This file contains the example program used in the gdb debugging
// tutorial. The tutorial can be found on the web at
// http://students.cec.wustl.edu/~agg1/tutorial/
#include <iostream.h>
int number_instantiated = 0;
template <class T>
class Node {
public:
Node (const T &value, Node<T> *next = 0) : value_(value), next_(next) {
cout << "Creating Node, "
<< ++number_instantiated
<< " are in existence right now" << endl;
}
~Node () {
cout << "Destroying Node, "
<< --number_instantiated
<< " are in existence right now" << endl;
next_ = 0;
}
Node<T>* next () const { return next_; }
void next (Node<T> *new_next) { next_ = new_next; };
const T& value () const { return value_; }
void value (const T &value) { value_ = value; }
private:
Node ();
T value_;
Node<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 Node<T>(new_item, head_)) != 0) ? 0 : -1;
}
// returns 0 on success, -1 on failure
int remove (const T &item_to_remove) {
Node<T> *marker = head_;
Node<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 Node<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 (void) {
Node<T> *marker = head_;
while (marker != 0) {
cout << marker->value() << endl;
marker = marker->next();
}
}
private:
void delete_nodes (void) {
Node<T> *marker = head_;
while (marker != 0) {
Node<T> *temp = marker;
delete marker;
marker = temp->next();
}
}
Node<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);
cout << "The fully created list is:" << endl;
list->print ();
cout << endl << "Now removing elements:" << endl;
list->remove (4);
list->print ();
cout << endl;
list->remove (1);
list->print ();
cout << endl;
list->remove (2);
list->print ();
cout << endl;
list->remove (3);
list->print ();
delete list;
return 0;
}