listNode.c 4.04 KB
Newer Older
1
/*
Mike Hibler's avatar
Mike Hibler committed
2
 * Copyright (c) 2005 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
 */

/**
 * @file listNode.c
 *
 * Implementation for the list node functions.
 */

#include "config.h"

#include <assert.h>
#include <stdio.h>
#include <string.h>

#include "listNode.h"

void lnRemove(struct lnNode *node)
{
    node->ln_Pred->ln_Succ = node->ln_Succ;
    node->ln_Succ->ln_Pred = node->ln_Pred;
}

void lnNewList(struct lnList *list)
{
    list->lh_Head = (struct lnNode *)&(list->lh_Tail);
    list->lh_Tail = 0;
    list->lh_TailPred = (struct lnNode *)list;
}

void lnMoveList(struct lnList *dest, struct lnList *src)
{
    if( lnEmptyList(src) )
    {
	lnNewList(dest);
    }
    else
    {
	dest->lh_Tail = NULL;
	dest->lh_Head = src->lh_Head;
	dest->lh_TailPred = src->lh_TailPred;
	dest->lh_Head->ln_Pred = (struct lnNode *)dest;
	dest->lh_TailPred->ln_Succ = (struct lnNode *)&dest->lh_Tail;
	lnNewList(src);
    }
}

void lnAppendList(struct lnList *dest, struct lnList *src)
{
    if( !lnEmptyList(src) )
    {
	dest->lh_TailPred->ln_Succ = src->lh_Head;
	src->lh_Head->ln_Pred = dest->lh_TailPred;
	dest->lh_TailPred = src->lh_TailPred;
	dest->lh_TailPred->ln_Succ =
	    (struct lnNode *)&dest->lh_Tail;
	lnNewList(src);
    }
}

void lnAddHead(struct lnList *list, struct lnNode *node)
{
    node->ln_Succ = list->lh_Head;
    node->ln_Pred = (struct lnNode *)list;
    list->lh_Head->ln_Pred = node;
    list->lh_Head = node;
}

void lnAddTail(struct lnList *list, struct lnNode *node)
{
    list->lh_TailPred->ln_Succ = node;
    node->ln_Pred = list->lh_TailPred;
    list->lh_TailPred = node;
    node->ln_Succ = (struct lnNode *)&(list->lh_Tail);
}

struct lnNode *lnRemHead(struct lnList *list)
{
    struct lnNode *remnode = 0;
    
    if( list->lh_Head->ln_Succ )
    {
	remnode = list->lh_Head;
	list->lh_Head = remnode->ln_Succ;
	list->lh_Head->ln_Pred = (struct lnNode *)list;
    }
    return( remnode );
}

struct lnNode *lnRemTail(struct lnList *list )
{
    struct lnNode *remnode = 0;
    
    if( list->lh_TailPred->ln_Pred )
    {
	remnode = list->lh_TailPred;
	list->lh_TailPred = remnode->ln_Pred;
	list->lh_TailPred->ln_Succ =
	    (struct lnNode *)&(list->lh_Tail);
    }
    return( remnode);
}

void lnInsert(struct lnNode *pred, struct lnNode *node)
{
    node->ln_Succ = pred->ln_Succ;
    pred->ln_Succ = node;
    node->ln_Pred = pred;
    node->ln_Succ->ln_Pred = node;
}

void lnEnqueue(struct lnList *list, struct lnNode *node)
{
    struct lnNode *curr;
    int done = 0;
    
    curr = list->lh_Head;
    while( curr->ln_Succ && !done )
    {
	if( node->ln_Pri > curr->ln_Pri )
	{
	    lnInsert((struct lnNode *)curr->ln_Pred,
		     (struct lnNode *)node);
	    done = 1;
	}
	curr = curr->ln_Succ;
    }
    if( !done )
	lnAddTail((struct lnList *)list, (struct lnNode *)node);
}

struct lnNode *lnFindName(struct lnList *list, const char *name)
{
    struct lnNode *curr, *retval = NULL;
    
    curr = list->lh_Head;
    while( (curr->ln_Succ != NULL) && (retval == NULL) )
    {
	if( strcmp(curr->ln_Name, name) == 0 )
	{
	    retval = curr;
	}
	curr = curr->ln_Succ;
    }
    return( retval );
}

unsigned int lnCountNodes(struct lnList *list)
{
    unsigned int retval = 0;
    struct lnNode *curr;

    curr = list->lh_Head;
    while( curr->ln_Succ != NULL )
    {
	retval += 1;
	curr = curr->ln_Succ;
    }
    return( retval );
}