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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
#include "List.h"
//------------------------------------------------
// class Link implementation
//------------------------------------------------
List::Link::Link( int val, Link* nxt) : value(val), next(nxt) {}
List::Link::Link(const Link& source) : value(source.value),next(source.next) {}
//--------------------------------------------
// class List implementation
//--------------------------------------------
List::List(): head(NULL)
{
//cout << "CTOR\n";
}
// Copy constructor
List::List(const List &l)
{
//cout << "Copy CTOR\n";
Link *src, *trg;
if(l.head == NULL)
head = NULL;
else
{ // copy the list
head = new Link((l.head)->value, NULL);
src = l.head;
trg = head;
while(src->next != NULL)
{
trg->next = new Link((src->next)->value, NULL);
src = src->next;
trg = trg->next;
}
}
}
void List::operator=(const List& l)
{
if(&l != this)
{
this->clear();
Link *src, *trg;
if(l.head == NULL)
head = NULL;
else
{ // copy the list
head = new Link((l.head)->value, NULL);
src = l.head;
trg = head;
while(src->next != NULL)
{
trg->next = new Link((src->next)->value, NULL);
src = src->next;
trg = trg->next;
}
}
}
}
// Destructor
List::~List()
{
//cout << "DTOR: ";
//print();
clear();
}
void List::clear()
{
// empty all elements from the List
Link* next;
for (Link *p = head; p != NULL; p = next)
{
// delete the element pointed to by p
next = p->next;
p->next = NULL;
delete p;
}
// mark that the List contains no elements
head=NULL;
}
// test to see if the List is empty
// List is empty if the pointer to the head
// Link is null
bool List::isEmpty() const
{
return head == NULL;
}
//Add a new value to the front of a Linked List
void List::add(int val)
{
head = new Link(val, head);
if(head == NULL)
throw "failed in memory allocation";
}
// return first value in List
int List::firstElement() const
{
if (isEmpty())
throw "the List is empty, no first Element";
return head->value;
}
bool List::search(const int &val) const
{
// loop to test each element
for (Link* p = head; p != NULL ; p = p->next)
if (val == p->value)
return true;
// not found
return false;
}
void List::print() const
{
for (Link* p = head; p != NULL ; p = p->next)
cout << p->value << ((p->next != NULL) ? " -> " : "\n");
}
void List::removeFirst()
{
// make sure there is a first element
if(isEmpty())
throw "the List is empty, no Elements to remove";
// save pointer to the removed node
Link* p = head;
// reassign the first node
head = p->next;
p->next = NULL;
// recover memory used by the first element
delete p;
}
void List::reverse()//revesing the list, in case each time the bigger element is first.
{
List temp;
while (!isEmpty())
{
temp.add(firstElement());
removeFirst();
}
*this=temp;
}
ostream& operator<<(ostream& out, List& l)
{
List::Link*p = l.head;
if (l.head != NULL)
{
do
{
out << p->value << " ";
p = p->next;
} while (p != NULL);
}
return out;
}
istream &operator>>(istream& in, List& l)
{
int help = 0;
in >> help;
if (l.head == NULL)
{
l.add(help);
in >> help;
}
do
{
l.add(help);
in >> help;
} while (l.head->value == help || l.head->value<help);
l.reverse();//because the last element was the bigger, we need to reverse.
return in;
}
void List::insert(int key)//inserting a new element, Keeps ascending
{
List::Link *first = head;//i will use it as the first element in the new list during insertion
if (head != NULL)
{
List::Link *second = head->next; //will use it as second element during insertion
if (head->value >= key)//extreme case if it will needed to be insert to the first place
{
add(key);
return;
}
do//to the middle...
{
if (second->value>=key)
{
first->next = new Link(key, second);
return;
}
first = second;
second = first->next;
} while (second != NULL);
first->next = new Link(key, NULL);//if not first and not to the middle..
}
}
void List::remove(int key)//remove element by value
{
if (!search(key))
throw "value not found";
List::Link *first = head;
if (head != NULL)
{
if (head->value == key)//extreme case if it the first element
{
removeFirst();
}
else
{
List::Link *second = first->next;//will use it as second element during insertion
do
{
if (second->value == key)
{
first->next = second->next;
}
first = second;
second = first->next;
} while (second != NULL);
}
}
}
List merge(List lst1, List lst2)
{
List temp; //the new sorting list
List::Link *list1 = lst1.head; //list a
List::Link *list2 = lst2.head; //list b
while (list1 != NULL && list2 != NULL)
{
if (list1->value < list2->value) //cheking if the value of the first smaller then the second and coefficient on
{
temp.add(list1->value);
list1 = list1->next;
}
else // else copy from list b and coefficient on
{
temp.add(list2->value);
list2 = list2->next;
}
}
//one of the lists must be empy (less they are equal..)
while (list1!=NULL)
{
temp.add(list1->value);
list1 = list1->next;
}
while (list2!=NULL)
{
temp.add(list2->value);
list2 = list2->next;
}
temp.reverse();//becuse the bigger element was the last..
return temp;
}
List makeSet(List& lst) //remove duplicate elements.
{
List temp; //new list
List::Link *list = lst.head;
int flag = list->value;//save the value of the last copied element.
temp.add(list->value);
while (list != NULL)
{
if (list->value != flag)
{
temp.add(list->value);
flag = list->value;
}
if (list != NULL)
list = list->next;
}
temp.reverse();
lst = temp;
return lst;
}
List.h:
[code]
#pragma once
#include <iostream>
using namespace std;
//------------------------------------------------
// class List
// arbitrary size Lists
// permits insertion and removal
// only from the front of the List
//------------------------------------------------
class List
{
private:
//--------------------------------------------
// inner class link
// a single element for the linked List
//--------------------------------------------
class Link
{
public:
// constructor
Link(int linkValue, Link *nextPtr);
Link (const Link &);
// data areas
int value;
Link * next;
};
public:
Link* head;
// constructors
List();
List(const List&);
~List();
// operations
void add( int value);
int firstElement() const;
bool search(const int &value) const;
bool isEmpty() const;
void removeFirst();
void print() const;
void operator=(const List& other);
void clear();
void insert(int);
void remove(int);
void reverse();
friend ostream &operator<<(ostream &out, List &myList);
friend istream &operator>>(istream &in, List &myList);
friend List merge(List, List);
friend List makeSet(List&);
};
|