summaryrefslogtreecommitdiffstats
path: root/linkedlist.cpp
blob: 929852127e7208ba40c5b4cb790aba01982d438b (plain)
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
#include "linkedlist.h"

LinkedList::LinkedList() : Collection(), head(NULL), tail(NULL)
{
}

LinkedList::LinkedList(const LinkedList& list) : Collection(list), head(NULL), tail(NULL)
{
    Node *iter = list.tail;
    while (iter) {
        this->add(iter->v);
        iter = iter->prev;
    }
}

LinkedList& LinkedList::operator=(const Collection& rhs)
{
    if (this == &rhs)
        return *this;

    this->cleanup();
    this->size_ = 0;

    Node *iter = dynamic_cast<const LinkedList *>(&rhs)->head; /* it's ok g++ */
    while (iter) {
        this->add(iter->v);
        iter = iter->next;
    }

    return *this;
}

LinkedList::~LinkedList()
{
    this->cleanup();
}

void LinkedList::cleanup(void)
{
    Node *iter, *prev;
    iter = prev = this->head;
    while (iter) {
        prev = iter;
        iter = iter->next;
        delete prev;
    }
}

void LinkedList::add(int n)
{
    Node *node = new Node();
    node->v = n;
    node->next = this->head;     /* link next */
    if (!this->head)             /* first node */
        this->tail = node;       /* this where tail gets assigned */
    else
        this->head->prev = node; /* link prev */

    this->head = node;
    this->size_++;
}

bool LinkedList::remove(int n)
{
    if (!this->head)
        return false;

    if (this->head->v == n) { /* deleting first element or head */
        Node *first = this->head;
        this->head = this->head->next;
        if (this->get_size() == 1)
            this->tail = NULL; /* tail gets nuked here */
        delete first;
        return true;
    }
    else if (this->tail->v == n) { /* deleting last element or tail */
        Node *last = this->tail;
        this->tail = this->tail->prev;
        this->tail->next = NULL;
        delete last;
        return true;
    }

    Node *iter = this->head;
    while (iter) {
        if (iter->v == n) {
            iter->prev->next = iter->next;
            delete iter;
            this->size_--;
            return true;
        }
        iter = iter->next;
    }

    return false;
}

/* assumes first element is at index of zero */
int LinkedList::operator[](int i)
{
    /* invalid accesses */
    if (!this->size_ || i < 0 || i+1 > this->size_)
        return -1;

    Node *iter = this->head;
    int cnt = this->size_ - i - 1;
    while (cnt--) {
        iter = iter->next;
    }

    return iter->v;
}

LinkedList *LinkedList::copy(void)
{
    LinkedList *ret = new LinkedList(*this);
    return ret;
}

void LinkedList::iterate(void (*modifier)(int *))
{
    Node *iter = this->head;
    while (iter) {
        modifier(&iter->v); /* missing smalltalk? No! */
        iter = iter->next;
    }
}

bool LinkedList::contains(int i) const
{
    Node *iter;
    for (iter = this->head; iter != NULL && iter->v != i; iter = iter->next)
        ;

    return iter ? true : false;
}

void LinkedList::print_helper(std::stringstream& sstm, Node *n) const
{
    if (!n)
        return;

    print_helper(sstm, n->next);
    sstm << n->v << " ";
}

/* does a recursive print since ll is inverted with respect to array */
/* could just use tail and iterate through prev pointers */
std::string LinkedList::print() const
{
    std::stringstream sstm;
    print_helper(sstm, this->head);

    return sstm.str();
}