[BACK]Return to list.c CVS log [TXT][DIR] Up to [contributed] / early-roguelike / rogue4

Annotation of early-roguelike/rogue4/list.c, Revision 1.1

1.1     ! rubenllo    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: void
        !            22: _detach(THING **list, THING *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: void
        !            37: _attach(THING **list, THING *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: void
        !            58: _free_list(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: void
        !            75: discard(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(void)
        !            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: }

CVSweb