【发布时间】:2014-08-12 20:10:32
【问题描述】:
我试图在下面的代码中输出我的链表中的字符,但我在printList 函数中的cout 不会打印任何内容。我无法准确了解为什么以及如何在链接列表中打印我的字符。
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct linkedListNode{
char obj;
linkedListNode *next;
}node;
void insertLinkedList(char *p,node *head){
node *end = head;
while(*p != '\0'){
node *temp = (node *)malloc(sizeof(node));
temp -> obj = *p;
end -> next = temp;
end = temp;
p++;
}
}
void printList(node *head){
node *temp = head;
while(temp){
cout << temp->obj << ",";
temp = temp -> next;
}
}
int main() {
node *HEAD = (node *)malloc(sizeof(node));
char p[] = "nitin";
insertLinkedList(p,HEAD);
printList(HEAD);
return 0;
}
如果我的调试技能没有让我失望,那么列表确实会被填充。请帮忙。
谢谢!
【问题讨论】: