1 | /* |
---|
2 | * Abuse - dark 2D side-scrolling platform game |
---|
3 | * Copyright (c) 1995 Crack dot Com |
---|
4 | * Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | * |
---|
6 | * This software was released into the Public Domain. As with most public |
---|
7 | * domain software, no warranty is made or implied by Crack dot Com, by |
---|
8 | * Jonathan Clark, or by Sam Hocevar. |
---|
9 | */ |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <stdio.h> |
---|
16 | #include <stdlib.h> |
---|
17 | #include <string.h> |
---|
18 | |
---|
19 | #include "linked.h" |
---|
20 | |
---|
21 | linked_list::linked_list() |
---|
22 | : m_first(NULL), |
---|
23 | m_count(0) |
---|
24 | { |
---|
25 | ; |
---|
26 | } |
---|
27 | |
---|
28 | // |
---|
29 | // Clear all the nodes in a linked list and dispose of the |
---|
30 | // memory for each one by calling its destructor |
---|
31 | // |
---|
32 | linked_list::~linked_list() |
---|
33 | { |
---|
34 | m_count = 0; // ensure destructors calling unlink() won't mess with us |
---|
35 | |
---|
36 | if (m_first) |
---|
37 | m_first->Prev()->SetNext(NULL); // set the prev nodes next to NULL |
---|
38 | // so we can go until we hit NULL |
---|
39 | while (m_first != NULL) |
---|
40 | { |
---|
41 | linked_node *tmp = m_first->Next(); |
---|
42 | delete m_first; |
---|
43 | m_first = tmp; |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | // |
---|
48 | // Take a node out of the linked list, but do not dispose of the memory |
---|
49 | // |
---|
50 | int linked_list::unlink(linked_node *p) |
---|
51 | { |
---|
52 | if (!m_count) |
---|
53 | return 0; |
---|
54 | |
---|
55 | linked_node *q = m_first; |
---|
56 | |
---|
57 | while (q != p) // find the node in the list |
---|
58 | { |
---|
59 | q = q->Next(); |
---|
60 | if (q == m_first) |
---|
61 | return 0; // not in the list! |
---|
62 | } |
---|
63 | |
---|
64 | q->Prev()->SetNext(q->Next()); |
---|
65 | q->Next()->SetPrev(q->Prev()); |
---|
66 | |
---|
67 | if (p == m_first) // if they want to unlink the first node |
---|
68 | m_first = (m_first->Next() == m_first) ? NULL : p->Next(); |
---|
69 | |
---|
70 | m_count--; |
---|
71 | return 1; |
---|
72 | } |
---|
73 | |
---|
74 | // |
---|
75 | // Add a node to the end of a linked_list |
---|
76 | // |
---|
77 | void linked_list::add_end(class linked_node *p) |
---|
78 | { |
---|
79 | if (m_first) |
---|
80 | p->SetPrev(m_first->Prev()); |
---|
81 | else |
---|
82 | m_first = p; |
---|
83 | p->SetNext(m_first); |
---|
84 | |
---|
85 | if (m_first != p) |
---|
86 | m_first->Prev()->SetNext(p); |
---|
87 | m_first->SetPrev(p); |
---|
88 | |
---|
89 | m_count++; |
---|
90 | } |
---|
91 | |
---|
92 | // Add a node at the front of the list: just add it at the end and set |
---|
93 | // the first pointer to it |
---|
94 | void linked_list::add_front(class linked_node *p) |
---|
95 | { |
---|
96 | add_end(p); |
---|
97 | m_first = p; |
---|
98 | } |
---|
99 | |
---|