summaryrefslogtreecommitdiffstats
path: root/linkedlist.h
blob: 336e131adbf79e204672df378fac0c6503d681c0 (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
#ifndef _LINKEDLIST_H_
#define _LINKEDLIST_H_

#include "collection.h"
#include <string>
#include <sstream>

class LinkedList : public Collection
{
  private:
    class Node {
      public:
        Node() : v(-1), next(NULL), prev(NULL) {}
        int v;
        Node *next;
        Node *prev;
    };

  public:
    LinkedList();
    LinkedList(const LinkedList&);
    /* we can override the return type and still preserve dynamic binding! */
    LinkedList& operator=(const Collection&);
    ~LinkedList();

    virtual void add(int);
    virtual bool remove(int);
    virtual int operator[](const int);
    virtual LinkedList *copy(void);

    /* override these methods that had empty definitions in the interface */
    void iterate(void (*)(int *));
    bool contains(int) const;

    std::string print(void) const;

  protected:
  private:
    void print_helper(std::stringstream&, Node *) const;
    void cleanup(void);
    Node *head;
    Node *tail;
};

#endif