I am trying to understand user defined Linked Lists in C++. I am trying to work through a lab exercise with class definitions and implementations posted by the instructor. Things were a little easier to follow when they were using the STD version. I need to store 10 strings in alphabetical order and then print them in descending order. I created the the list and stored the strings. II am trying to figure out how to print them in descending order but can't get out the starting gate. I was checking out the web for tutorials and how to but too much information out there. Could some kind soul lend some assistance? Here's what I got.
#include <cassert>
#include <iostream>
#include <string>
using namespace std;
class Node {
public:
Node(string s);
private:
string data;
Node* previous;
Node* next;
friend class List;
friend class Iterator;
};
class Iterator {
public:
Iterator();
string get() const;
void next();
void previous();
bool equals(Iterator b) const;
private:
Node* position;
Node* last;
friend class List;
};
class List {
public:
List();
void push_back(string s);
void insert(Iterator pos, string s);
Iterator erase(Iterator pos);
Iterator begin();
Iterator end();
private:
Node* first;
Node* last;
};
Node::Node(string s) {
data = s;
previous = NULL;
next= NULL;
};
Iterator::Iterator():posit
ion(NULL),
last(NULL)
{ }
string Iterator::get() const {
assert(position != NULL);
return position->data;
}
void Iterator::next() {
assert(position != NULL);
position = position->next;
}
void Iterator::previous() {
if (position == NULL)
position = last;
else
position = position->previous;
assert(position != NULL);
}
bool Iterator::equals(Iterator b) const {
return position == b.position;
}
List::List() {
first = NULL;
last = NULL;
}
void List::push_back(string s) {
Node* newnode = new Node(s);
if (last == NULL){
first = newnode;
last = newnode;
}
else {
newnode->previous = last;
last->next = newnode;
last = newnode;
}
}
void List::insert(Iterator iter, string s) {
if (iter.position == NULL) {
push_back(s);
return;
}
Node* after = iter.position;
Node* before = after->previous;
Node* newnode = new Node(s);
newnode->previous = before;
newnode->next = after;
after->previous = newnode;
if (before == NULL)
first = newnode;
else
before->next = newnode;
}
Iterator List::erase(Iterator i) {
Iterator iter = i;
assert(iter.position != NULL);
Node* remove = iter.position;
Node* before = remove->previous;
Node* after = remove->next;
if (remove == first)
first = after;
else
before->next = after;
if (remove == last)
last = before;
else
after->previous = before;
iter.position = after;
delete remove;
return iter;
}
Iterator List::begin() {
Iterator iter;
iter.position = first;
iter.last = last;
return iter;
}
Iterator List::end() {
Iterator iter;
iter.position = NULL;
iter.last = last;
return iter;
}
int main()
{
List soldier;
soldier.push_back("Allen")
;
soldier.push_back("Christ"
);
soldier.push_back("Dog");
soldier.push_back("Fiona")
;
soldier.push_back("Jerry")
;
soldier.push_back("John");
soldier.push_back("Lion");
soldier.push_back("Tiger")
;
soldier.push_back("Tom");
soldier.push_back("Wolf");
cout << soldier.begin();
system("pause");
return 0;
}