1 #include "linklist.h"
  2 #include <iostream>
  3
  4 using namespace std;
  5
  6 template<class unk>
  7 NODE<unk>::~NODE<unk>(){
  8         //cout << "List Item Destroyed" << endl;
  9         
 10 }
 11
 12
 13
 14
 15 template<class unk>
 16 void LIST<unk>::remove_front(){
 17         if(_at_front == 0){
 18                 cout << "No List" << endl;
 19                 return;
 20         }
 21         NODE<unk> * tmp = _at_front;
 22         down_size();
 23         _at_front = tmp->next();
 24         if(tmp->next() == NULL)
 25                  _at_end = 0; //need to clear _at_end as well once you delete the last node
 26         
 27         
 28         delete tmp;
 29         return;
 30 }
 31
 32 template<class unk>
 33 void LIST<unk>::remove_all(){
 34         cout << "********CLEANING OUT LIST*********" << endl;
 35
 36         while(_at_front){
 37 //              cout << "***node to remove***\nNode==> " << _at_front << "\tValue==> " << _at_front->value() << endl;
 38                 remove_front();
 39 //              cout << "Done with remove_all\n";
 40         }
 41 }
 42
 43 template<class unk>
 44 NODE<unk> * LIST<unk>::find(unk search){
 45         NODE<unk> * index;
 46         if(_at_front == NULL)
 47                 return NULL;
 48         index = _at_front;
 49         
 50         while(index){
 51                 if( search == index->value())
 52                         return index;
 53                 index= index->next();
 54         }
 55         return NULL;
 56 }
 57
 58 template<class unk>
 59 NODE<unk> * LIST<unk>::find_all(unk search, NODE<unk> * last){
 60
 61 //find_all returns a NULL when it reaches the end of a list and the search node is not found
 62 //find_all starts a fresh search from the beginning of a list if a Null is sent as a node value
 63 //except for the NULL node, it begins searching with the NEXT node after the one sent to it (for convience of while loops)
 64 //
 65 //syntax is find_all(int, LIST<unk> *);
 66 //
 67 //
 68         if(last == NULL){
 69                 last = find(search);
 70                 return last;
 71         }
 72         last = last->next(); //search forward
 73         if( last == NULL){
 74                 return last;//past the end
 75         }
 76 //      cout << "Not NULL: Last ==>" << last << " last_value()==> "<< last->value() << " Search==>" << search << endl;
 77         while( last->value() != search){
 78                 if (last == _at_end)
 79                         return last->next();
 80                 last = last->next();
 81         }
 82         return last;
 83
 84         //if(last == _at_end){
 85         //             cout << "found end node.  last=> " << last << " value=>" << last->value() << "\n\n";
 86         //             cout << "last->next()==> " << last->next() << " which is the end and should be null\n";
 87         //     return ((last->value() == search) ? last : last->next());
 88 //              if(last->value() == search)
 89 //                      return last;
 90 //      
 91 //              cout << last->next() << " which is the end and should be null\n";
 92 //                      return last->next(); //which should be null
 93 //      }
 94         //cout << "Last ==>" << last << "is not at end==> " << _at_end << " and last value=> " << last->value() << "\n\n";
 95 //      return last;
 96
 97 }
 98                         
 99 template<class unk>
100 void LIST<unk>::remove_item(unk search){
101         NODE<unk>* node = find(search);
102         remove_item(node);
103 }
104
105
106 template<class unk>
107 void LIST<unk>::remove_item(NODE<unk> * node){
108         if(node == NULL)
109                 return;
110         NODE<unk> * prev, *current;
111         if( _at_front == node){
112                 remove_front();
113                 return;
114         }
115 //Walk from the beginning in order to popular prev      
116         prev = _at_front;
117         current = prev->next();
118         while (current != NULL){
119                 if(node == current){
120                         prev->next(current->next());
121                         if(current == _at_end)
122                                 _at_end=prev;
123                         delete current;
124                         down_size();
125                         return;
126                 }
127                 prev = current;
128                 current = current->next();
129         }
130         return;
131 }
132
133
134
135
136
137
138
139
140