1 /*
2 * =====================================================================================
3 *
4 * Filename: linklist.h
5 *
6 * Description: Link List Exercise with MYSQL Hooks
7 *
8 * Version: 1.0
9 * Created: 03/21/2011 12:42:28 PM
10 * Revision: none
11 * Compiler: gcc
12 *
13 * Author: Ruben Safir,
14 * Company: NYLXS Workshops
15 *
16 * =====================================================================================
17 */
18 #ifndef LINKLIST_H
19 #define LINKLIST_H
20 #endif /* LINKLIST_H */
21
22 #include<iostream>
23
24
25 namespace chainlist {
26
27
28 /*
29 * =====================================================================================
30 * Class: Node
31 * Description: This is a node in the "List" Link List
32 * =====================================================================================
33 */
34
35 template < class unk >
36 class Node
37 {
38 public:
39
40 // ==================== LIFECYCLE =======================================
41 /* constructor */
42 Node<unk>( unk value, Node<unk> *item_to_link_to = 0);
43 Node ( const Node &other ); /* copy constructor - Need to write this! */
44 ~Node<unk> (); /* destructor */
45
46
47 /* ==================== ACCESSORS ======================================= */
48 inline unk* value ()const{return value_; }
49 inline Node<unk> * next()const {return next_;}
50
51 inline void value(unk *);
52 inline void value(unk);
53 inline void next(Node<unk> *);
54
55
56
57
58
59 /* ==================== MUTATORS ======================================= */
60
61 /* ==================== OPERATORS ======================================= */
62
63 Node& operator=( const Node &other ); // assignment operator
64 unk& operator*(); //gain access to real value
65 unk& operator*(unk);
66
67 protected:
68 /* ==================== DATA MEMBERS ======================================= */
69
70
71
72 private:
73 /* ==================== DATA MEMBERS ======================================= */
74
75 unk * value_ ;
76 Node * next_;
77
78
79
80 }; /* ----- end of template class Node ----- */
81
82 template<class unk>
83 Node<unk>::Node(unk val, Node<unk> *item_to_link_to){
84 value(val);
85 if(!item_to_link_to){
86 next_ = 0;
87 }
88
89 else{
90 next(item_to_link_to->next());
91 item_to_link_to->next(this);
92 }
93 }
94
95 template<class unk>
96 Node<unk>::~Node(){
97 delete value_;
98 }
99
100 template <typename unk>
101 void Node<unk>::next(Node * next_node){
102 next_ = next_node;
103 }
104
105 template<class unk>
106 void Node<unk>::value(unk val){
107 value_ = new unk(val);
108 }
109
110 template<class unk>
111 void Node<unk>::value(unk * val){
112 value_ = val;
113 }
114
115
116
117
118
119 /* ===================================================================================== */
120
121 template < class T >
122 class List
123 {
124 public:
125
126 /* ==================== LIFECYCLE ======================================= */
127 List<T>(): front_(0),end_(0), cursor_(0), size_(0){} /* constructor */
128
129 /* ==================== ACCESSORS ======================================= */
130 int size()const { return size_; }
131 inline Node<T>* const &front()const{ return front_;}
132 inline Node<T>* &end(){return end_;}
133
134 inline Node<T> *& cursor(){return cursor_;}
135
136 inline void cursor(Node<T> * a){ cursor_ = a; }
137
138 inline void end(Node<T> *a){ end_ = a; }
139 inline void front(Node<T> *a){ front_ = a; }
140 inline void display();
141
142
143
144 /* ==================== MUTATORS ======================================= */
145
146 inline void sizeup(){ ++size_; }
147 inline void sizedown(){ --size_; }
148 void insert_front( T value); //add a node to the front
149 Node<T> insert(T value, Node<T> place_after_this); //add a new node after the Node given and return the new node
150 void insert_end(T value); // Add a new node to the end
151
152 Node<T> find_value(T value); //find a value from it's first occurance in the list
153 Node<T> find_next_value(T value, Node<T> last_node_searched); //find a value in the list after the
154 //current node which is not searched
155 void remove_node_by_value(T value);
156 void remove_node_by_node(Node<T> cur);
157 void remove_node_front();
158 void remove_node_end();
159 void clear_list();
160
161
162
163 /* ==================== OPERATORS ======================================= */
164
165 protected:
166 /* ==================== DATA MEMBERS ======================================= */
167
168 private:
169 /* ==================== DATA MEMBERS ======================================= */
170 Node<T> * front_;
171 Node<T> * end_;
172 Node<T> * cursor_;
173 int size_;
174
175 }; /* ---------- end of template class Lists ---------- */
176
177 template <class T>
178 void List<T>::insert_front ( T value )
179 {
180 Node<T> *tmp = new Node<T>(value);
181 if(front() == 0){
182 front(tmp);
183 end(tmp);
184 cursor(tmp);
185 tmp->next(0);
186 }
187 else{
188 tmp->next(front());
189 front(tmp);
190 cursor(tmp); //curosr moved to the front
191 }
192
193 sizeup();
194
195 return ;
196 } /* ----- end of template function insert_front ----- */
197
198 template < class T >
199 void List<T>::display ()
200 {
201 std::cout << "*********DISPLAYING ENTIRE LIST************" << std::endl;
202 if(end() == 0){
203 std::cout << "Empty List" << std::endl;
204 return;
205 }
206
207 cursor() = front();
208 while(cursor() != end() ){
209 std::cout << *(cursor()->value()) << std::endl;
210 cursor(cursor()->next());
211 }
212 std::cout << *(cursor()->value()) << std::endl; //display the end
213
214
215 return ;
216 } /* ----- end of method List<T>::display ----- */
217
218
219
220
221
222
223
224
225
226 } /* ----- end of namespace chainlist ----- */
227
228