我做了一个简单的链表实现,当我在Main()的Insert()函数调用中运行代码时,我收到此错误,我不明白为什么这样。
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node* head;
int main(){
head=NULL;
Insert(1,2);
Insert(2,1);
Insert(3,1);
Insert(4,1);
}
void Print(){
struct Node* temp;
temp=head;
while(temp!=NULL){
printf("%d ",tenp->data);
temp=temp->next;
}
printf("\n");
}
Node* Insert(Node *head,int data)
{
Node *node = new Node();
node->data = data; // ASSIGNING VALUES TO NEW NODE
node->next = NULL; // ALSO AS TO ACT AS THE TAIL NODE ADDING THE null VALUE AT THE NEXT POINTER
if(head == NULL) // CASE: WHEN LIST IS EMPTY
return node;
Node *temp = head; // dereferencing value for TEMP, assignning it the HEAD values, HEAD is the current tail node
while(temp->next != NULL) // Traversing till 1 before
temp = temp->next;
temp->next = node; // making the last move, assigning the LINK to new node.
return head; // returinng the funn call with VALUE
}
答案 0 :(得分:0)
Insert(1,2)//this won't work as insert()
//expects node* type and not an integer as first argument.
您应该将指针传递给head而不是值。
答案 1 :(得分:0)
您正在将1,2,3和4传递给Node *参数。 您必须传递实际的头节点并分配结果
head = Insert( head, value );
这就是你如何使用这个功能。