comparison rogue4/list.c @ 12:9535a08ddc39

Import Rogue 5.2 from the Roguelike Restoration Project (r1490)
author edwarj4
date Sat, 24 Oct 2009 16:52:52 +0000
parents
children 1b73a8641b37
comparison
equal deleted inserted replaced
11:949d558c2162 12:9535a08ddc39
1 /*
2 * Functions for dealing with linked lists of goodies
3 *
4 * @(#)list.c 4.7 (Berkeley) 12/19/81
5 *
6 * Rogue: Exploring the Dungeons of Doom
7 * Copyright (C) 1980, 1981, 1982 Michael Toy, Ken Arnold and Glenn Wichman
8 * All rights reserved.
9 *
10 * See the file LICENSE.TXT for full copyright and licensing information.
11 */
12
13 #include <stdlib.h>
14 #include <curses.h>
15 #include "rogue.h"
16
17 /*
18 * detach:
19 * Takes an item out of whatever linked list it might be in
20 */
21 _detach(list, item)
22 register THING **list, *item;
23 {
24 if (*list == item)
25 *list = next(item);
26 if (prev(item) != NULL) item->l_prev->l_next = next(item);
27 if (next(item) != NULL) item->l_next->l_prev = prev(item);
28 item->l_next = NULL;
29 item->l_prev = NULL;
30 }
31
32 /*
33 * _attach:
34 * add an item to the head of a list
35 */
36 _attach(list, item)
37 register THING **list, *item;
38 {
39 if (*list != NULL)
40 {
41 item->l_next = *list;
42 (*list)->l_prev = item;
43 item->l_prev = NULL;
44 }
45 else
46 {
47 item->l_next = NULL;
48 item->l_prev = NULL;
49 }
50 *list = item;
51 }
52
53 /*
54 * _free_list:
55 * Throw the whole blamed thing away
56 */
57 _free_list(ptr)
58 register THING **ptr;
59 {
60 register THING *item;
61
62 while (*ptr != NULL)
63 {
64 item = *ptr;
65 *ptr = next(item);
66 discard(item);
67 }
68 }
69
70 /*
71 * discard:
72 * Free up an item
73 */
74 discard(item)
75 register THING *item;
76 {
77 total--;
78 free((char *) item);
79 }
80
81 /*
82 * new_item
83 * Get a new item with a specified size
84 */
85 THING *
86 new_item()
87 {
88 register THING *item;
89
90 if ((item = calloc(1, sizeof *item)) == NULL)
91 msg("ran out of memory after %d items", total);
92 else
93 total++;
94 item->l_next = item->l_prev = NULL;
95 return item;
96 }