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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
#include <iostream>
#include <string>
using namespace std;
typedef string T; // list element type
//typedef double T; // list element type
const int SIZE = 11; // maximum size of List
struct Node; // forward reference
template <class T>
class LinkList
{
protected : // private data members
int length; // number of elements in list
Node * head; // pointer to data array
Node * currPos; // current position in list
void inFront(T item); // insert item at head of list
void append(T item); // insert item at tail of list
public : // public member functions
// Pre: None
// Post: a list exist with room for SIZE elements of type T
LinkList(); // default ctor
// Pre: list is created
// Post: list is destructed, dynamic memory is freed
~LinkList(); // dtor
// Pre: None
// Post: List is empty
void makeMT();
// Pre: Key member of item is initialized
// Post: If found, itemÕs key matches an elementÕs key in the list and a copy of
// that element has been stored in item; otherwise, item is unchanged
void retrieve(T item, bool & found);
// Pre: List is initialized; List is not full; item is not in list
// Post: item is in list
void insert(T item);
// Pre: an element in the list has a key that matches itemÕs
// Post: no element in the list has a key that matches itemÕs
void remove(T item);
// Pre: List is initialized
// Post: Current position is prior to first element in list
void reset();
// Pre: List is initialized; current position is defined;
// element at current position is not last in list
// Post: current position is updated to next position;
// item is a copy of element at current position
void getNextItem(T & item);
// Pre: List has been initialized
// Post: Function value == (list is full)
bool isFull() const;
// Pre: List has been initialized
// Post: Function value == (number of elements in list)
int getLength() const; // returns length of list
// Pre: List has been initialized
// Post: Function value == (number of elements in list)
void prnt() const; // prints the linked list
};
struct Node
{
T data; // Node data of type T
Node * next; // link to next Node
Node() : data(0), next(NULL)
{ } // default c-tor for Node (numeric)
Node(const T & e, Node * nx = NULL) : data(e), next(nx)
{ } // parameterized c-tor for Node
}; // end Node
// list.cpp - implementation file - array-based list
//#include list.h
const char FRONT = 'f'; // 'f' for insert at front
const char APPEND = 'b'; // 'b' for append or insert at back
static char fORb = APPEND; // 'f' for insert at front; 'b' for back
template <class T>
LinkList<T>::LinkList() : length(0), head(NULL)
{ } // end ctor
template <class T>
LinkList<T>::~LinkList()
{
Node * upOne;
Node * ptr = head;
while (ptr != NULL) {
head = ptr->next; // traverse the list,
delete ptr; // deleting each node
ptr = head;
} // endwhile
} // end dtor
template <class T>
void LinkList<T>::makeMT()
{
length = 0;
head = NULL;
LinkList::~LinkList();
} // end makeMT()
template <class T>
void LinkList<T>::insert(T item)
{
if (fORb == FRONT)
inFront(item);
else
append(item);
++length;
} // end insert()
template <class T>
void LinkList<T>::inFront(T item)
{
Node * ptr = new Node(item, head);
ptr->next = head;
head = ptr;
} // end inFront()
template <class T>
void LinkList<T>::append(T item)
{
Node * ptr = new Node(item); // allocate new node with item
Node * tail; // ptr for traversing list
if (!head) // no nodes in list, insert as first node
head = ptr;
else { // insert new Node at end
tail = head;
while (tail->next) // travel to last node in the list
tail = tail->next;
tail->next = ptr; // make new Node the last node
} // endif
} // end append()
template <class T>
int LinkList<T>::getLength() const
{
return length;
} // end getLength()
template <class T>
bool LinkList<T>::isFull() const
{
return false;
} // end isFull()
template <class T>
void LinkList<T>::retrieve(T item, bool & found)
{
Node * loc = head;
found = false;
while (loc && !found) {
if (item == loc->data) {
found = true;
item = loc->data;
} // endif-thenpart
else {
loc = loc->next;
} // endif-elsepart
} // endwhile
} // end retrieve()
template <class T>
void LinkList<T>::remove(T item)
{
Node * prev;
Node * loc = head;
if (!loc) // if MT list, we are done
return;
if (loc->data == item) { // remove first node
head = loc->next; // new head pointer
delete loc; // remove first node
} // endif-thenpart
else {
while (loc && loc->data != item) {
prev = loc;
loc = loc->next;
} // endwhile
if (loc) { // if not at end of list,
prev->next = loc->next; // link previous node to one after
delete loc; // delete node
} // endif
} // endif-elsepart
--length; // decrement length
} // end remove()
template <class T>
void LinkList<T>::reset()
{
currPos = NULL;
} // reset()
template <class T>
void LinkList<T>::getNextItem(T & item)
{
if (!currPos) {
currPos = head;
//cout << endl << "head: " << head << '\t' << "curr: " << currPos << '\t';
item = currPos->data;
currPos = currPos->next;
//cout << endl << "head: " << head << '\t' << "curr: " << currPos << '\t';
} // endif-thenpart
else {
//cout << '\n' << "curr: " << currPos << '\t';
if (currPos) {
item = currPos->data;
currPos = currPos->next;
//cout << endl << "head: " << head << '\t' << "curr: " << currPos << '\t';
} // endif
//cout << '\t' << currPos->data << '\t';
} // endif-elsepart
} // getNextItem()
template <class T>
void LinkList<T>::prnt() const
{
Node * ptr = head;
cout << "\nThe linked list: ";
while (ptr) {
cout << ptr->data << '\t';
ptr = ptr->next;
}// endwhile
}
// print the list
void prnt(LinkList & al);
int main()
{
LinkList frnds;
bool isThere = false;
prnt(frnds);
frnds.retrieve("Joan", isThere);
if (isThere)
cout << "\n \"Joan\" already in list";
else
frnds.insert("Joan");
prnt(frnds);
frnds.insert("Brandi");
frnds.insert("Sami");
frnds.insert("Jill");
frnds.insert("Sally");
prnt(frnds);
frnds.retrieve("Brandy", isThere);
if (isThere)
frnds.remove("Brandy");
else
cout << "\n \"Brandy\" not found";
prnt(frnds);
frnds.retrieve("Brandi", isThere);
if (isThere)
frnds.remove("Brandi");
else
cout << "\n \"Brandi\" not found";
prnt(frnds);
cout << "\n -- Done --\n";
return 0;
}
/*int main()
{
LinkList dbls;
bool isThere = false;
prnt(dbls);
dbls.retrieve(-44.5, isThere);
if (isThere)
cout << "\n \"-44.5\" already in list";
else
dbls.insert(-44.5);
dbls.insert(32.23);
dbls.insert(17.92);
dbls.insert(-65.72);
dbls.insert(98.6);
dbls.insert(45.36);
prnt(dbls);
dbls.retrieve(17.92, isThere);
if (isThere)
dbls.remove(17.92);
else
cout << "\n 17.92 not found";
prnt(dbls);
cout << "\n -- Done --\n";
return 0;
}*/
void prnt(LinkList & al)
{
al.reset();
T val;
if (al.getLength() > 0) {
cout << "\nPrinting the list...";
for (int k = 0; k < al.getLength(); ++k) {
al.getNextItem(val);
cout << " " << val;
} // endfor
} // endif-thenpart
else
cout << "\nThe list is MT...";
} // end prnt()
|