libsaria: Add items to a List
I free the memory in the destructor, too! Be careful, though, the copy constructor isn't actually a copy constructor. Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
parent
da776eeebf
commit
34c13bc93e
|
@ -30,7 +30,11 @@ namespace libsaria
|
|||
|
||||
public:
|
||||
List();
|
||||
List(const List<T> &);
|
||||
~List();
|
||||
|
||||
ListNode<T> *push_back(T);
|
||||
void erase(ListNode<T> *);
|
||||
};
|
||||
|
||||
};
|
||||
|
|
|
@ -76,6 +76,7 @@ void read_path(ifstream &stream)
|
|||
for (unsigned int i = 0; i < size; i++) {
|
||||
path_ptr->tracks.push_back(libsaria::Track(stream, path_ptr, version));
|
||||
track = &path_ptr->tracks.back();
|
||||
path_ptr->_tracks.push_back(*track);
|
||||
if (track->get_banned())
|
||||
banned.push_back(track);
|
||||
else
|
||||
|
|
|
@ -3,6 +3,9 @@
|
|||
#define LIBSARIA_LIST_CPP
|
||||
|
||||
#include <libsaria/list.h>
|
||||
#include <libsaria/print.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
|
@ -11,14 +14,53 @@ namespace libsaria
|
|||
List<T>::List()
|
||||
{
|
||||
count = 0;
|
||||
head.prev = &head;
|
||||
head.next = &head;
|
||||
head.list = this;
|
||||
}
|
||||
|
||||
/*
|
||||
* This just claims to be a copy constructor.
|
||||
* A real one should be implemented later if it's needed
|
||||
*/
|
||||
template <class T>
|
||||
List<T>::List(const List<T> &orig)
|
||||
{
|
||||
count = 0;
|
||||
head.list = this;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
List<T>::~List()
|
||||
{
|
||||
while (count > 0)
|
||||
erase(head.next);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
ListNode<T> *List<T>::push_back(T item)
|
||||
{
|
||||
ListNode<T> *node = new ListNode<T>(item, this);
|
||||
ListNode<T> *last = head.prev;
|
||||
|
||||
last->next = node;
|
||||
node->prev = last;
|
||||
node->next = &head;
|
||||
head.prev = node;
|
||||
|
||||
count++;
|
||||
return node;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void List<T>::erase(ListNode<T> *node)
|
||||
{
|
||||
ListNode<T> *prev = node->prev;
|
||||
ListNode<T> *next = node->next;
|
||||
|
||||
prev->next = next;
|
||||
next->prev = prev;
|
||||
|
||||
count--;
|
||||
delete node;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,15 +11,13 @@ namespace libsaria
|
|||
template <class T>
|
||||
ListNode<T>::ListNode()
|
||||
{
|
||||
prev = NULL;
|
||||
next = NULL;
|
||||
prev = this;
|
||||
next = this;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
ListNode<T>::ListNode(T val, List<T> *lst)
|
||||
{
|
||||
prev = NULL;
|
||||
next = NULL;
|
||||
list = lst;
|
||||
value = val;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue