如何删除单链表中的任意节点
这是我的代码:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int x;
struct node *next;
};
struct node *addNode(struct node *head, int y)
{
struct node *traverser;
traverser = head;
if(traverser!=NULL)
{
while(traverser->next!=NULL)
traverser=traverser->next;
traverser -> next = malloc(sizeof(struct node));
traverser -> next -> x = y;
traverser -> next -> next = NULL;
}
else
{
head= malloc(sizeof(struct node));
head -> x=y;
head -> next = NULL;
}
return head;
}
void display(struct node *head)
{
struct node *traverser;
traverser = head;
while(traverser!=NULL)
{
printf("%d\n",traverser->x);
traverser=traverser->next;
}
}
struct node *InitializeList(void)
{
return NULL;
}
int main()
{
struct node *head;
head = InitializeList();
head = addNode(head,2);
head = addNode(head,15);
head = addNode(head,5);
head = addNode(head,8);
display(head);
free(head);
getchar();
}
我需要像这样删除 main 中的一个节点
struct node *head;
head = InitializeList();
head = addNode(head,2);
head = addNode(head,15);
head = addNode(head,5);
head = addNode(head,8);
display(head);
removenode(5);
display(head);
removenode(8);
display(head);
free(head);
这是我在删除程序中的特定节点时的 main() 代码。
但我该怎么办呢? removenode() 只是函数名称我应该使用什么算法?或者什么或如何删除它?
Here's my code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int x;
struct node *next;
};
struct node *addNode(struct node *head, int y)
{
struct node *traverser;
traverser = head;
if(traverser!=NULL)
{
while(traverser->next!=NULL)
traverser=traverser->next;
traverser -> next = malloc(sizeof(struct node));
traverser -> next -> x = y;
traverser -> next -> next = NULL;
}
else
{
head= malloc(sizeof(struct node));
head -> x=y;
head -> next = NULL;
}
return head;
}
void display(struct node *head)
{
struct node *traverser;
traverser = head;
while(traverser!=NULL)
{
printf("%d\n",traverser->x);
traverser=traverser->next;
}
}
struct node *InitializeList(void)
{
return NULL;
}
int main()
{
struct node *head;
head = InitializeList();
head = addNode(head,2);
head = addNode(head,15);
head = addNode(head,5);
head = addNode(head,8);
display(head);
free(head);
getchar();
}
I need to remove a node in main like this
struct node *head;
head = InitializeList();
head = addNode(head,2);
head = addNode(head,15);
head = addNode(head,5);
head = addNode(head,8);
display(head);
removenode(5);
display(head);
removenode(8);
display(head);
free(head);
That's my code for main() when it comes to delete specific node in my program.
But how can I do it? The removenode() is just function name what algorithm should I use? Or what or how to remove it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不是一个完整的答案,只是一些一般建议
为了弄清楚这种事情,通常只需解决三种情况就足够了
您将遇到的最大麻烦来源是
请注意,有一个递归实现(例如
n.next = remove(n.next,val)
)使这两个问题变得相同,并且您可以将其主要转换为循环防止很长的列表上的堆栈溢出。可能出现的一个子问题是在列表中找到要删除的节点。您可以通过将查找目标节点的部分与
remove(node* head, node* target)
分开来让您的生活变得更轻松吗?Not quite a answer, just some general advice
For the purposes of figuring this kind of thing out, it is generally sufficient to solve exactly three cases
The the big sources of trouble you will encounter are
Note that there is a recursive implementation (think
n.next = remove(n.next,val)
) that makes these two problems one and the same, and that you can convert it mostly to a loop to prevent stack overflows on very long lists.A sub-problem that may crop up is that of finding the node to be removed in the list. Can you makae you life easier by separating there part about finding the target node from a
remove(node* head, node* target)
?算法原型需要是:
因为如果删除第一项,原始的“头”指针将不再有效。
该算法只是逐步浏览列表,记住前一项(和头部),然后查找数据。找到后,将前一项的下一个指针设置为当前项的下一项。
The algorithm prototype needs to be:
Since if you're removing the first item, the original "head" pointer will no longer be valid.
The algorithm is simply to step through the list, remembering the previous item (and the head), and looking for the data. When found, set previous item's next pointer to be the current item's next.
从较高层次来看,要删除任何节点,您需要做的是:
1) 指向指向您要删除的节点的项目。
2) 将要删除的项目的引用设置为要删除的项目的下一个项目。
3) 删除要删除的项目。
这样,您的链就得到了维护,并且您已经从内存中释放了该元素。
就像这样:
如果你想删除 Item2,你可以这样做:
将 Item1 的旁边设置为 Item2 的下一个,然后删除 Item2。
编辑:删除项目或项目3:
将头部重新指向项目2,然后删除项目1:
或
将头部重新指向项目2,然后删除项目1:
At a high level, to remove any node, what you need to do is:
1) Point to the item that is pointing to the node you want to delete.
2) Set the reference to the item you want to delete to the item you want to delete's next item.
3) Delete the item you want to delete.
That way, you chain is maintained and you've freed that element from memory.
Like so:
If you want to delete Item2, you go like this:
Set Item1's next to Item2's next, then delete Item2.
EDIT: Deleting Item or Item3:
Repoint head to Item2, then delete Item1:
OR
Repoint head to Item2, then delete Item1:
看看这是否适合你,也许存储在我的电脑中的某个地方,用于这样的情况:
它总是最重要的。
See if this works out for you or not, perhaps was stored in my PC somewhere for a situation like this:
It always the Head that counts.