我有一个链表之后,我需要的头之后创建一个节点..添加一个节点到链表头
这意味着我有这样的事情:
node *head = NULL;
,并在结束我的链表应该是这样的:
head -> node -> NULL
...
但是当我使用一个正常的ADDNODE福nction,它给了我一个运行时错误(不知道哪个,我调试有问题)...
这是我写的:
void addNode(node *head)
{
node *temp = head; // a temp to not move the head
node *newNode = (node*)malloc(sizeof(node)); // The new node
while (temp -> next != NULL)
{
temp = temp -> next // Getting to the last node
}
temp -> next= newNode; // Adding the new node into the linked list insted of the NULL
newNode -> next = NULL; // Adding the NULL after the new node
}
此代码的伟大工程给我时,我有一个链表已经有1个或更多的节点,但如果链表只有一个头,它会对我造成问题......我该如何解决这个问题?
(如果你不明白我的问题 - 随着ADDNODE功能我写到这里,我得到一个运行时错误添加一个新的节点进入指向已经NULL头)..
谢谢,阿米特:)
那么如何添加头后的新节点,如果头为空? – AmitM9S6
非常感谢,我可以在head后添加一个新节点,当head = null时,使用这两行代码: 'head =(node *)malloc(sizeof(node)); head - > next = null',它现在可以工作:) – AmitM9S6
没问题。我的荣幸:)如果它解决了您的问题,请点击帖子下的标记。人们会知道问题解决了。 – Blood