【问题标题】:Printing Linked List(prints more that it should)打印链接列表(打印更多应有的内容)
【发布时间】:2012-12-16 21:45:49
【问题描述】:

这些是函数

//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;
}

这些是主要的:

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);

这是输出...

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 

我的问题是……这个“p”是从哪里来的?!

【问题讨论】:

    标签: c list output


    【解决方案1】:

    列表头节点。

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

    你在这里创建一个头节点,然后在头节点之后添加每个字母。头节点有一个未初始化的->letter 字段。可以是任何东西;它恰好是p

    【讨论】:

    • 天哪,你是对的!非常感谢!!那个'p'真的让我震惊!再次感谢:)
    猜你喜欢
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    相关资源
    最近更新 更多