C:\Users\brock\Documents\CSCI 255\NetBeans\RL Assign\RLvartab.c
/* This file contains a section for modification */

/*
 * ADT-like implementation for symbol table for RL variables
 *
 * Copyright J. Dean Brock, 2010
 * This file released under terms of the GNU General Public License
 *
 */

#include "RLvartab.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

RLVariableTable RLVTcreate(void) {
    int i ;
    /* Ain't it odd you can take the size of something that ain't */
    RLVariableTable r = (RLVariableTable)malloc(sizeof *r) ;
    r->numberItems = 0 ;
    for(i=0; i<26; ++i) {
        r->Heads[i].next     = &r->Heads[i] ;
        r->Heads[i].variable = (struct RLAssignmentNode *) NULL ;
        r->Heads[i].value    = - 255 * 209 ;
    }
    return r ;
}

struct RLVariableNode *RLVTinsert(
            RLVariableTable t,
            struct RLAssignmentNode *p) {
    int lastCmp ;
    char *SearchVar ;
    struct RLVariableNode *dummyNode, *prevNode, *newNode ;
    SearchVar = p->value.Target ;

    /* Find dummy for the head of appropriate list */
    dummyNode = &t->Heads[SearchVar[0]-'A'] ;

    /* Find "previous" node for Search Var */
    prevNode  = dummyNode ;
    while (prevNode->next != dummyNode
            && (lastCmp=(strncmp(SearchVar,
                                 prevNode->next->variable->value.Target,
                                 RLVARSIZE)))<0) {
        prevNode = prevNode->next ;
    }
    if (prevNode->next != dummyNode && lastCmp == 0)
        /* Returned the discovered variable node */
        return prevNode->next ;
    else {
        newNode = (struct RLVariableNode *)malloc(sizeof *newNode) ;
        newNode->variable = p ;
        /* RL Modification B -- part 1:
         *
         * The new node is created (as newNode) and its variable
         * field has been initialized to point to p.
         * 
         * Your *only* job is to insert it into the list
         * right after prevNode.
         * 
         * Only TWO lines of code are required for this!
         */

        ++(t->numberItems) ;
        return newNode;
    }

}

struct RLVariableNode *RLVTlookup(RLVariableTable t, char *SearchVar) {
    struct RLVariableNode *dummyNode, *presNode ;
    int lastCmp ;       /* Used to store result of last comparison */
    /* Find dummy for the head of appropriate chain */
    dummyNode = &t->Heads[SearchVar[0]-'A'] ;
    presNode  = dummyNode->next ;

    /* RL Modification B -- part 2:
     *
     * Write a little loop that moves presNode through the
     * chain until either searchVar is found in the Target
     * field (using strncmp to test) or dummyNode is reached.
     *
     * The while loop is similar to the one found in RLVTinsert;
     * but it is much simpler.  Since you are not inserting, you don't
     * have to do that prevNode->next stuff.
     *
     * Return NULL if you don't find the node.
     *
     * A single while loop and an if-else was removed from here.
     *
     * The return of NULL that remains should be the else of your if-else.
     */

        return (struct RLVariableNode *)NULL ;
}

int RLVTgetItemCount (RLVariableTable t) {
    return t->numberItems ;
}