summaryrefslogtreecommitdiff
path: root/src/user/include/cxx/structs/dllist.h
blob: 57833644a0b0d7f0d0c41f353f342d8031d8099a (plain) (blame)
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
#ifndef STRUCTS_DLLIST_H
#define STRUCTS_DLLIST_H

template<class data>
class dllist {
public:
  class node {
  public:
    node *next;
    node *prev;
    data d;
    node(node *next, node *prev, data d)
      : next(next), prev(prev), d(d) {}
  };
  node *first;

  dllist() : first(0) {}

  void add_front(data d) {
    node *const n = new node(first, 0, d);
    if (first)
      first->prev = n;
    first = n;
  }

  //return previous, or zero if this is the first
  node *remove_in_place(node *n) {
    if (n == first)
      first = n->next;
    if (n->next)
      n->next->prev = n->prev;
    if (n->prev)
      n->prev->next = n->next;
    delete n;
    return n->prev;
  }
};

#endif