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
| class node
{
public:
int data;
node* next;
node(node* N=NULL):next(N) {}
node(int value, node* N=NULL):data(value),next(N){}
~node(){}
};
class iterator
{
public:
node* current_node;
iterator(node* N=NULL):current_node(N){}
~iterator(){}
iterator& operator++();
iterator operator++(int);
int& operator*()const;
bool operator==(const iterator& It) const;
bool operator!=(const iterator& It) const;
};
class list
{
node* head;
public:
list():head(NULL){}
list(int value)
{
node* first_node=new node;
first_node->data=value;
first_node->next=NULL;
head=first_node;
}
size_t length()const;
iterator begin()const;
iterator<T> end()const;
void insert_front(int input)
{
node<T>* to_be_added=new node<T>;
to_be_added->data=input;
to_be_added->next=head;
head=to_be_added;
}
void insert_back(int input)
{
if (head==NULL)
{
node<T>* to_be_added=new node<T>;
to_be_added->data=input;
to_be_added->next=NULL;
head=to_be_added;
return;
}
//looking for the last element
node<T>* current=head;
while (current->next!=NULL)
{
current=current->next;
}
//now current is the last element of the list
node<T>* to_be_added=new node<T>;
to_be_added->data=input;
to_be_added->next=NULL;
current->next=to_be_added;
}
~list()
{
for(iterator it=begin();it!=end();)
{
iterator tmp=it++;
delete tmp.current_node;
}
}
}; |
Partager