//链表
//并发安全单链表
type SingleNode struct {
Data interface{}
Next *SingleNode
}
type SingleList struct {
mutex *sync.RWMutex //读写锁
Head *SingleNode //头节点
Tail *SingleNode //尾节点
Size uint //总数
}
func NewList() *SingleList {
return &SingleList{
Size: 0,
Head: nil,
Tail: nil,
mutex: new(sync.RWMutex),
}
}
//后插元素
func (List *SingleList) LastAppend(node *SingleNode) bool {
if node == nil {
return false
}
List.mutex.Lock()
defer List.mutex.Unlock()
if List.Size == 0 {
List.Head = node
List.Tail = node
List.Size = 1
return true
}
tail := List.Tail
tail.Next = node
List.Tail = node
List.Size += 1
return true
}
//删除链表中的节点
//删除链表中的节点
func (List *SingleList) DelNode(client_id string) bool {
if List.Head == nil{
return false
}
head := List.Head
if head.Data.client_id == client_id{
head = head.Next
}
current := head
for current.Next != nil && current.Data != client_id{
current = current.Next
}
if current.Next != nil{
current.Next = current.Next.Next
}
return true
}
//链表使用
//建立账号和连接的绑定关系,并作为链表的一个节点
item := ChatWebSocketConn{
user_conn : tools.WebSocketConnMapOnce.ConnMap[client_id],
client_id : *client_id,
user_type : (*user_info).Flag,
user_id : (*user_info).Id,
user_nickname : (*user_info).NickName,
}
linked_node := tools.SingleNode{Data:item}
linked_list.LastAppend(&linked_node)
//ChatWebSocketConn定义
type ChatWebSocketConn struct{
user_conn *tools.WebSocketConn //已建立的客户端连接,lu_guild_account_list表id
client_id string //生成的唯一id,标识一个客户端
user_type int //用户类型,1:会员 2:主播
user_id int64 //用户id
user_nickname string //用户昵称
}
我在实现单链表的删除节点功能,函数定义DelNode,添加节点是通过LastAppend将ChatWebSocketConn做为一个Node节点添加进去的,删除的时候需要遍历该节点存储的client_id并进行比对,但编译的时候报错:head.Data.client_id undefined (type interface{} has no field or method client_id)
golang链表节点删除
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
关注让 “紫雾凌寒” 来帮你回答,本回答结合 DeepSeek
如果还有问题,请评论或点击头像私信,如果有帮助到您, 请关注、采纳、点赞
- 关键点分析:
- 要实现Go语言中链表节点的删除操作,需要明确链表的结构(这里已经定义了单链表节点结构SingleNode)。
- 考虑并发安全,因为定义的是并发安全单链表,所以在删除节点时要确保操作的原子性,避免数据竞争。 - 解决方案:
- 假设要删除链表中值为特定值的节点。可以通过遍历链表找到该节点,然后调整其前驱节点的Next指针来实现删除。
- 示例代码如下:
package main import ( "fmt" ) //并发安全单链表 type SingleNode struct { Data interface{} Next *SingleNode } // 删除链表中值为target的节点 func DeleteNode(head *SingleNode, target interface{}) *SingleNode { if head == nil { return nil } // 处理头节点 if head.Data == target { return head.Next } current := head // 遍历链表找到要删除节点的前驱节点 for current.Next != nil && current.Next.Data != target { current = current.Next } if current.Next != nil { current.Next = current.Next.Next } return head }- 多种解决方案及优缺点:
- 方案一:上述遍历查找删除- 优点:逻辑清晰,直观易懂,适用于各种链表操作场景。
- 缺点:需要遍历链表,时间复杂度为O(n),如果链表很长,效率会比较低。
- 方案二:使用哈希表辅助
- 优点:可以将查找时间复杂度降低到O(1),通过哈希表快速定位要删除节点的前驱节点。
- 缺点:需要额外的空间来存储哈希表,增加了空间复杂度。示例代码如下:
package main import ( "fmt" ) //并发安全单链表 type SingleNode struct { Data interface{} Next *SingleNode } // 使用哈希表辅助删除链表中值为target的节点 func DeleteNodeWithHash(head *SingleNode, target interface{}) *SingleNode { if head == nil { return nil } hashMap := make(map[interface{}]*SingleNode) current := head hashMap[head.Data] = nil // 构建哈希表存储前驱节点 for current.Next != nil { hashMap[current.Next.Data] = current current = current.Next } if node := hashMap[target]; node != nil { node.Next = node.Next.Next } return head }- 总结:
- 实现Go语言链表节点删除操作,遍历查找删除是基本方法,适用于一般情况。
- 当对效率要求较高时,可以使用哈希表辅助删除,能显著提高查找效率,但会增加空间开销。
希望这些解答对你有所帮助。如果还有其他需求,请随时告诉我。
希望以上解答对您有所帮助。如果您有任何疑问,欢迎在评论区提出。
解决 无用评论 打赏 举报- 关键点分析: