user3380932
user3380932

Reputation: 9

How to test linked list ADT?

I am trying to test the linked list ADT which I have created. However, whenever I try to test my ADT, it isn't creating a list, I don't understand why?, Currently, we are using the dummyNode method rather than having the first element as the head of the list. Does it have something to do with my ADT?

node*createList()
{
    node*dummyNode; 

    dummyNode = malloc(sizeof(node)*101);

    dummyNode->next = NULL;

    return dummyNode; 
}
node*addFront(node*list, int toBeAdded)
{
    node*newNode;

    newNode = initNode(toBeAdded);

    newNode->next = list->next;

    list->next = newNode;

    return newNode;
}

void printList(node*theHead)
{
    node*currentPosition;

    currentPosition = theHead->next;

    while(currentPosition != NULL)
    {
        printf("This is the value of the current node : %d\n",currentPosition->nodeValue);
        currentPosition= currentPosition->next;
    }
}
node*initNode(int getValue)
{
    node*newNode;

    newNode = malloc(sizeof(node)*101);

    newNode->nodeValue = getValue;

    newNode->next = NULL;

    return newNode;
}
void destroyList(node*theList)
{
    node*temp;

    theList->nodeValue = 0;

    while(theList->next != NULL)
    {
        temp = theList->next;
        free(theList);
        theList=temp;
    }
}
node*removeFromFront(node*theList) 
{
    node*temp;

    if(theList==NULL)
    {
        return NULL;
    }
    temp=theList->next;

    theList->next=NULL;

    return temp;
}
int listLength(node*theHead)
{
    int length;

    length=0;

    while(theHead != NULL)
    {
        length++;
        theHead = theHead ->next;
    }
    return length;
}

and this is my test.c 
node*testData;
int length,i;
int myArray[5] = {1,2,3,4,5};

testData = createList();

length = listLength(testData);

for(i=0; i<length; i++)
{
    testData = addFront(testData, myArray[i]);
    printList(testData);

}
This is the struct 
 struct listNode 
{
    int nodeValue;
    struct listNode * next;
};

typedef struct listNode node;

node*createList();
node*addFront(node*theHead, int toBeAdded);
void printList(node*theHead);
node*initNode(int getValue);
void destroyList(node*theList);
node*removeFromFront(node*theList);
int listLength(node*theHead);

Upvotes: 0

Views: 133

Answers (2)

akrabi
akrabi

Reputation: 4284

Your implemention is fine, the problem is your test.c

First, as noted before, change your allocation to this:

malloc(sizeof(node));

Next your test.c should be:

int main() {

node*testData;
int length,i;
int myArray[5] = {1,2,3,4,5};

testData = createList();

for(i=0; i<5; i++)
{
    addFront(testData, myArray[i]);
}

printList(testData);

destroyList(testData);

return 0;
}

This should print the expected result:

This is the value of the current node : 5
This is the value of the current node : 4
This is the value of the current node : 3
This is the value of the current node : 2
This is the value of the current node : 1

Upvotes: 1

EyeOfTheHawks
EyeOfTheHawks

Reputation: 576

Trace through your functions using the test you've created. You should discover a problem within your addToFront() function. Primarily with the line newNode->next = list->next; .

Once you've discovered the issue with that and resolved it, try your tests again. Your pointer logic is just a bit off.

Also, as an aside, malloc(sizeof(node)*101) is allocating more memory than needed, its much better to just malloc(sizeof(node))

Upvotes: 0

Related Questions