我刷了我的C++编程技能的采访,我想了解在这个代码我写了一个链表有些事情正在发生可怕的错误与我的链表代码
struct Node
{
int data;
Node *next;
} *Head ;
void ListInit()
{
Head = NULL;
}
void addfront(Node *Head, int data)
{
Node *newnode = new Node;
newnode->data = data;
newnode->next = Head;
Head = newnode;
}
void displaylist(Node *Head)
{
Node *cur;
cur = Head;
if(cur==NULL)
{
cout<<"List is Empty ! ";
}
while(cur->next!=NULL)
{
cout<<" "<<cur->data<<" ";
cur = cur->next;
}
}
int main()
{
ListInit();
addfront(Head,5);
addfront(Head,6);
addfront(Head,8);
addfront(Head,1);
addfront(Head,9);
displaylist(Head);
return 0;
}
错误当我运行这个代码块时,代码块崩溃,所以我猜测它是一个段错误。但我无法弄清楚为什么它潜入此。
除了新的代替malloc和COUT代替了printf,这是C. – 2014-09-23 13:42:34
在'addfront'你只修改'Head'的本地副本。 – 2014-09-23 13:43:14
@NeilKirk:那么?这意味着它仍然是C++。 – Deduplicator 2014-09-23 13:45:23