![](/img/trans.png)
[英]In the following function, I want to reverse the linkedlist, but I am wondering where can I delete node?
[英]Where is the Error in following code snippet, I have made a linkedlist implementation and I am adding elemnts at tail of the LinkedList
我做了一个简单的链表实现,当我在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
}
Insert(1,2)//this won't work as insert()
//expects node* type and not an integer as first argument.
您应该将指针传递给head而不是值。
您将1,2,3和4传递给Node *参数。 您必须通过实际的头节点并分配结果,
head = Insert( head, value );
那就是您如何使用该功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.