在链表中的第n个位置插入元素-C
我的程序显示列表为空。我想我在将节点重新链接到头部时犯了错误。帮我弄清楚在链表中的第n个位置插入元素-C,c,linked-list,C,Linked List,我的程序显示列表为空。我想我在将节点重新链接到头部时犯了错误。帮我弄清楚 void insert(struct node** headRef, int index, int Data) { int i, distanceFromHead = 1; struct node* head = *headRef; struct node* temp1 = (struct node*)malloc(sizeof(struct node)); //node to be inserted. t
void insert(struct node** headRef, int index, int Data)
{
int i, distanceFromHead = 1;
struct node* head = *headRef;
struct node* temp1 = (struct node*)malloc(sizeof(struct node)); //node to be inserted.
temp1->data = Data;
if(index == 0)
{
temp1->next = head;
head = temp1;
return;
}
while(head != NULL)
{
if(distanceFromHead == index)
{
temp1->next = head->next;
head->next = temp1;
*headRef = head;
return;
}
head = head->next;
distanceFromHead++;
}
}
您正在使用
head
遍历链表,如果索引与距离匹配,则更新headref
问题是,..if
在
if(index==0)
中,将temp1
分配给*headref
,即*headref=temp1
使用head
遍历链接列表,如果索引与距离匹配,则更新headref
问题是,..if
在
if(index==0)
中,将temp1
分配给*headref
,即*headref=temp1
您有两个条件:
- 查找要插入到链接列表中的位置
- 不会从链表的末尾掉下来
*headRef
,而不是某些局部指针变量。在你绝对确定你真的需要内存之前,你不应该给malloc打电话
可以在单个循环中组合这两个条件:
你不需要与headvarable的距离;您也可以减小索引:
void insert2(struct node **headRef, int index, int Data)
{
struct node *temp1;
for( ; *head; head = &(*head)->next) {
if(!index) break;
index--;
}
if (index) return; // index not found: list too short
temp1 = malloc(sizeof *temp1); //node to be inserted.
temp1->data = Data;
temp1->next = *head;
*head = temp1;
}
现在,测试索引=循环后重复0
。通过在循环内移动插入,然后跳出,可以避免这种情况:
void insert3(struct node **headRef, int index, int Data)
{
for( ; *head; head = &(*head)->next) {
struct node *temp1;
if(index--) continue;
temp1 = malloc(sizeof *temp1); //node to be inserted.
temp1->data = Data;
temp1->next = *head;
*head = temp1;
break; // or : return;
}
return;
}
你有两个条件:
- 查找要插入到链接列表中的位置
- 不会从链表的末尾掉下来
*headRef
,而不是某些局部指针变量。在你绝对确定你真的需要内存之前,你不应该给malloc打电话
可以在单个循环中组合这两个条件:
你不需要与headvarable的距离;您也可以减小索引:
void insert2(struct node **headRef, int index, int Data)
{
struct node *temp1;
for( ; *head; head = &(*head)->next) {
if(!index) break;
index--;
}
if (index) return; // index not found: list too short
temp1 = malloc(sizeof *temp1); //node to be inserted.
temp1->data = Data;
temp1->next = *head;
*head = temp1;
}
现在,测试索引=循环后重复0
。通过在循环内移动插入,然后跳出,可以避免这种情况:
void insert3(struct node **headRef, int index, int Data)
{
for( ; *head; head = &(*head)->next) {
struct node *temp1;
if(index--) continue;
temp1 = malloc(sizeof *temp1); //node to be inserted.
temp1->data = Data;
temp1->next = *head;
*head = temp1;
break; // or : return;
}
return;
}
你认为head=temp1does?
temp1
的@pat地址被分配给head
,然后head
会发生什么?head
是函数的局部变量,当函数返回时会消失。您应该分配给*headRef
注意,如果要求您在包含3个节点的列表中的位置10添加一个节点,则代码会泄漏新分配的节点。您认为head=temp1
does?temp1
的@pat地址被分配给head
,然后head
会发生什么?head
是函数的局部变量,当函数返回时会消失。您应该分配给*headRef
注意,如果要求您在一个包含3个节点的列表中的位置10添加一个节点,代码将泄漏新分配的节点。该行*headRef=head代码>没有任何区别,我想。。如果我删除它,问题仍然是一样的。。当我使用签名功能时struct Node*insert(Node*head,int index,int data)
code工作正常。。当我把头当作眼睛时,我有问题,这就是重点。您正在更新头引用,并在插入头引用第n个节点或NULL@JonathanLeffler如何防止该程序内存泄漏?@Surjit如果您在Linux上运行该应用程序,可以使用Valgrind帮助您查找内存泄漏。对于windows,您可以看一看讨论。windows呢?那一行*headRef=head代码>没有任何区别,我想。。如果我删除它,问题仍然是一样的。。当我使用签名功能时struct Node*insert(Node*head,int index,int data)
code工作正常。。当我把头当作眼睛时,我有问题,这就是重点。您正在更新头引用,并在插入头引用第n个节点或NULL@JonathanLeffler如何防止该程序内存泄漏?@Surjit如果您在Linux上运行该应用程序,可以使用Valgrind帮助您查找内存泄漏。对于windows,您可以查看讨论。windows呢??