主函数里也不能访问,感觉是链表没有接上,能否帮我看下,谢谢!
//--------------------------------------------------------------------------------------
#include <iostream>
struct Node
{
float value;
Node * next;
};
void InitList(Node * head, int size/*大于0*/)
{
head = (Node *)malloc(sizeof(Node));
head->value = 0.0;
Node * pnode = head->next;
for(int i = 0; i < size - 1; i++)
{
pnode = (Node *)malloc(sizeof(Node));
pnode->value = 0.0;
pnode = pnode->next;
}
pnode = NULL;//设置空的尾指针
std::cout<<head->next->next;//在这里验证就有问题了,链表没有接上
}
float VisitElem(Node * head, int index)
{
Node * pnode = head;
for(int i = 0; i < index; i++)
{
pnode = pnode->next;
}
return pnode->value;
}
void DestroyList(Node * head)
{
Node * pnode = head;
while(pnode != NULL)
{
Node * pnext = pnode->next;
free(pnode);
pnode = pnext;
}
}
int main()
{
Node * linkedlist = NULL;
::InitList(linkedlist, 5);
//std::cout<<linkedlist->next<<std::endl;//报错
//std::cout<<linkedlist->value<<std::endl;//报错
::DestroyList(linkedlist);
return 0;
}
//--------------------------------------------------------------------------------------
#include <iostream>
struct Node
{
float value;
Node * next;
};
void InitList(Node * head, int size/*大于0*/)
{
head = (Node *)malloc(sizeof(Node));
head->value = 0.0;
Node * pnode = head->next;
for(int i = 0; i < size - 1; i++)
{
pnode = (Node *)malloc(sizeof(Node));
pnode->value = 0.0;
pnode = pnode->next;
}
pnode = NULL;//设置空的尾指针
std::cout<<head->next->next;//在这里验证就有问题了,链表没有接上
}
float VisitElem(Node * head, int index)
{
Node * pnode = head;
for(int i = 0; i < index; i++)
{
pnode = pnode->next;
}
return pnode->value;
}
void DestroyList(Node * head)
{
Node * pnode = head;
while(pnode != NULL)
{
Node * pnext = pnode->next;
free(pnode);
pnode = pnext;
}
}
int main()
{
Node * linkedlist = NULL;
::InitList(linkedlist, 5);
//std::cout<<linkedlist->next<<std::endl;//报错
//std::cout<<linkedlist->value<<std::endl;//报错
::DestroyList(linkedlist);
return 0;
}