[56] | 1 | /* |
---|
| 2 | * Abuse - dark 2D side-scrolling platform game |
---|
| 3 | * Copyright (c) 1995 Crack dot Com |
---|
[494] | 4 | * Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
[56] | 5 | * |
---|
| 6 | * This software was released into the Public Domain. As with most public |
---|
[555] | 7 | * domain software, no warranty is made or implied by Crack dot Com, by |
---|
| 8 | * Jonathan Clark, or by Sam Hocevar. |
---|
[56] | 9 | */ |
---|
| 10 | |
---|
[555] | 11 | #if defined HAVE_CONFIG_H |
---|
| 12 | # include "config.h" |
---|
| 13 | #endif |
---|
[56] | 14 | |
---|
[2] | 15 | #include <stdio.h> |
---|
| 16 | #include <stdlib.h> |
---|
| 17 | #include <string.h> |
---|
| 18 | |
---|
[481] | 19 | #include "linked.h" |
---|
[2] | 20 | |
---|
[534] | 21 | linked_list::linked_list() |
---|
| 22 | : m_first(NULL), |
---|
| 23 | m_count(0) |
---|
[2] | 24 | { |
---|
[534] | 25 | ; |
---|
[2] | 26 | } |
---|
| 27 | |
---|
[112] | 28 | // |
---|
[534] | 29 | // Clear all the nodes in a linked list and dispose of the |
---|
[112] | 30 | // memory for each one by calling its destructor |
---|
| 31 | // |
---|
[2] | 32 | linked_list::~linked_list() |
---|
| 33 | { |
---|
[535] | 34 | m_count = 0; // ensure destructors calling unlink() won't mess with us |
---|
| 35 | |
---|
[534] | 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 | } |
---|
[2] | 45 | } |
---|
| 46 | |
---|
[112] | 47 | // |
---|
[534] | 48 | // Take a node out of the linked list, but do not dispose of the memory |
---|
[112] | 49 | // |
---|
[534] | 50 | int linked_list::unlink(linked_node *p) |
---|
[2] | 51 | { |
---|
[534] | 52 | if (!m_count) |
---|
| 53 | return 0; |
---|
[2] | 54 | |
---|
[534] | 55 | linked_node *q = m_first; |
---|
[2] | 56 | |
---|
[534] | 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; |
---|
[2] | 72 | } |
---|
| 73 | |
---|
[534] | 74 | // |
---|
| 75 | // Add a node to the end of a linked_list |
---|
| 76 | // |
---|
[2] | 77 | void linked_list::add_end(class linked_node *p) |
---|
| 78 | { |
---|
[534] | 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++; |
---|
[2] | 90 | } |
---|
| 91 | |
---|
[534] | 92 | // Add a node at the front of the list: just add it at the end and set |
---|
[2] | 93 | // the first pointer to it |
---|
| 94 | void linked_list::add_front(class linked_node *p) |
---|
| 95 | { |
---|
| 96 | add_end(p); |
---|
[534] | 97 | m_first = p; |
---|
[2] | 98 | } |
---|
| 99 | |
---|