从单链表中删除节点
我需要从单链表中删除一个节点。我知道这是一件简单的事情,但我的大脑一片空白,我搜索了 Google 和 Stackoverflow,但我真的没有找到任何可以帮助我的东西。
基本上,节点列表包含在一个桶中;像这样:
struct node{
unsigned char id[20];
struct node *next;
};
struct bucket{
unsigned char id;
struct node *nodes;
};
我有一个函数
struct bucket *dht_bucketfind(unsigned char *id); // return bucket with id[20]
可以找到正确的存储桶。所以我知道如何找到正确的存储桶,但我不知道如何删除给定的节点。我想通过 nodeid 删除节点(我想,我还没有真正编写调用删除函数的代码;)但我想如果有必要我可以修改代码)。我认为这就是解决这个问题所需要的一切。提前致谢。
I need to remove a node from a singly linked list. I know this is a simple thing to do, but my mind is blank and I've searched both Google and Stackoverflow, but I seriously haven't found anything that will help me.
basically the list of nodes is contained in a bucket; like this:
struct node{
unsigned char id[20];
struct node *next;
};
struct bucket{
unsigned char id;
struct node *nodes;
};
and I have a function
struct bucket *dht_bucketfind(unsigned char *id); // return bucket with id[20]
to find the correct bucket. So I know how to find the correct bucket, but I don't know how to remove a given node. I would like to remove the node by nodeid (I think, I haven't really written the code that will call the remove function yet ;) but I think I'll be able to modify the code if necessary). I think that's all that's needed to solve this. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您知道要删除的项目,则必须执行两件事:
next
成员。这将是前一项的next
指针,或列表bucket.nodes
的头部。一旦您了解了自己在做什么,操作链表的代码实际上并不那么棘手。
If you know the item you want to remove, you must do two things:
next
member. This will be the preceding item'snext
pointer, or the head of the listbucket.nodes
.The code for manipulating a linked list is really not that tricky, once you understand what you are doing.
您的节点除了 id 之外没有任何有效负载,因此,根据节点的数据有效负载,您实际上可能不需要以标准方式迭代列表。如果删除者只想知道他们想要删除的节点的地址,这非常有用。
如果您的有效负载是指向其他数据的指针:
那么您可以通过窃取下一个节点的有效负载,然后取消链接下一个节点来“删除”节点:
Your nodes don't have any payload other than an id, so, depending on the data payload of a node, you might not actually need to iterate the list in the standard way. This is useful if deleters are going to know the address of only the node they want to delete.
If your payload is a pointer to other data:
Then you could "delete" a node by stealing the payload of the next node, and then delinking the next node:
自从我使用 C 以来已经很久了,但这应该是编译干净的。
基本上,您需要在迭代链表时跟踪前一个指针。当找到要删除的节点时,只需更改前一个指针即可跳过删除节点。
该函数删除所有具有 id 的节点(查找)。如果只想删除第一个匹配项,请在 free 语句后添加 return。
Its been a long time ago since I worked with C, but this should be compile clean.
Basically, you need to keep track of the previous pointer while you iterate through the linked list. When you find the node to delete, just change the previous pointer to skip the delete node.
This function deletes all nodes with id (find). If you want to delete only the first occurrence, then put a return after the free statement.
以下不包含任何错误检查,仅从列表中删除当前节点...
您的偏好可能会有所不同,但我倾向于将链表节点放在结构的开头(只要可行)。
我发现这通常有助于简化指针运算,并在需要时允许简单的类型转换。
The following does not contain any error checking and only removes the current node from the list ...
Your preferences may vary, but I tend to put my linked list node at the start of the structure (whenever practical).
I find this generally helps to simplify pointer arithmetic and allows simple typecasting when needed.
这将删除给定地址的节点;您可以修改它以删除给定 id 的节点,但您尚未指定 id 的形式 - 它是一个以 NUL 结尾的字符串,还是 20 个字节?
或者,更紧凑的是,
This removes a node given its address; you can modify it to remove a node given its id, but you haven't specified the form of an id -- is it a NUL-terminated string, or is it 20 bytes?
Or, more compactly,