George F-bot
George F-bot

Reputation: 11

Printing Linked List(prints more that it should)

these are the functions

//printList for Debugging
void printList(letterListT *head){
    letterListT *temp = head;
    while(temp != NULL){
        printf("%c ", temp->letter);
    temp = temp->nxt;
    }
}  

//Add the Specified Letter by Creating a New Node in the Letter List defined
void addLetter(letterListT *letListHead, char letter){
    letterListT *newNode;
    newNode = (letterListT *)malloc(sizeof(letterListT));

    newNode->letter = letter;

    newNode->nxt = letListHead->nxt;
    letListHead->nxt = newNode;
}

and these are in main:

unusedLetList = (letterListT *)malloc(sizeof(letterListT));
unusedLetList->nxt = NULL;

for(i=122; i>=97; i--){ //ascii codes for z to a
addLetter(unusedLetList, i);
}

//printlists Test
printList(unusedLetList);

and this is the output...

p a b c d e f g h i j k l m n o p q r s t u v w x y z 

my question is... where does this 'p' come from?!

Upvotes: 0

Views: 65

Answers (1)

John Kugelman
John Kugelman

Reputation: 362167

The list head node.

unusedLetList = (letterListT *)malloc(sizeof(letterListT));
unusedLetList->nxt = NULL;

You create a head node here, and then add each letter after the head node. The head node has an uninitialized ->letter field. It could be anything; it happens to be p.

Upvotes: 5

Related Questions