当前位置: 首页 > news >正文

小型网站制作深圳NET网站开发工程师网站招聘

小型网站制作深圳,NET网站开发工程师网站招聘,南宁网站建设醉懂网络,网新科技集团有限公司五、用go语言#xff0c;使用单向循环链表实现字典操作 INSERT、DELETE 和 SEARCH#xff0c;并给出所写过程的运行时间。 文心一言#xff1a; 在Go语言中#xff0c;我们首先需要定义一个Node结构体来表示单向循环链表的节点#xff0c;然后再定义一个LinkedList结构体…五、用go语言使用单向循环链表实现字典操作 INSERT、DELETE 和 SEARCH并给出所写过程的运行时间。 文心一言 在Go语言中我们首先需要定义一个Node结构体来表示单向循环链表的节点然后再定义一个LinkedList结构体来表示单向循环链表。接下来我们可以实现INSERT、DELETE和SEARCH操作。为了给出运行时间我们需要使用Go的time包来测量执行时间。 以下是使用Go语言实现的单向循环链表字典操作 package main import ( fmt time ) type Node struct { Key string Value string Next *Node } type LinkedList struct { Head *Node Tail *Node } func NewLinkedList() *LinkedList { return LinkedList{} } func (l *LinkedList) Insert(key, value string) { newNode : Node{Key: key, Value: value, Next: nil} if l.Head nil { l.Head newNode l.Tail newNode } else { newNode.Next l.Head l.Head newNode l.Tail.Next l.Head } } func (l *LinkedList) Search(key string) (string, bool) { current : l.Head for current ! current.Next { if current.Key key { return current.Value, true } current current.Next } return , false } func (l *LinkedList) Delete(key string) { if l.Head nil { return } if l.Head.Key key { l.Head l.Head.Next if l.Head nil { l.Tail nil } else { l.Tail l.Head.Next } } else { current : l.Head for current.Next ! nil current.Next.Key ! key { current current.Next } if current.Next ! nil { current.Next current.Next.Next if current.Next nil { l.Tail current.Next } else { l.Tail current.Next.Next } } else { fmt.Println(Key not found) } } } func main() { start : time.Now() dict : NewLinkedList() dict.Insert(test, success) value, found : dict.Search(test) if found { fmt.Printf(Found key: %s, value: %s\n, test, value) } else { fmt.Printf(Key not found\n) } dict.Delete(test) value, found dict.Search(test) if found { fmt.Printf(Found key: %s, value: %s\n, test, value) } else { fmt.Printf(Key not found\n) } end : time.Now() fmt.Println(end.Sub(start)) }这个代码中的Insert、Delete和Search操作的时间复杂度分别为O(n)、O(n)和O(n)。其中n为链表的长度。这是因为在最坏的情况下我们需要遍历整个链表才能完成这些操作。然而由于这是一个循环链表搜索操作可以从头开始也可以从尾开始因此其时间复杂度仍为O(n)。在我们的示例中由于我们只进行了一次Insert、Delete和Search操作因此实际的运行时间可能会非常快可能无法准确反映其真实性能。为了更准确地测量运行时间我们可能需要在一个更长的时间内重复进行这些操作。 讯飞星火 首先我们需要定义一个单向循环链表的结构体包含一个节点结构体和一个头指针。然后我们需要实现插入、删除和搜索操作的方法。最后我们将编写一个简单的测试程序来测试这些方法的运行时间。 以下是用Go语言实现的代码 package mainimport (fmttime )type Node struct {key intvalue stringnext *Node }type CircularLinkedList struct {head *Node }func (c *CircularLinkedList) Insert(key int, value string) {newNode : Node{key: key, value: value}if c.head nil {c.head newNodenewNode.next c.head} else {current : c.headfor current.next ! c.head {current current.next}current.next newNodenewNode.next c.head} }func (c *CircularLinkedList) Delete(key int) {if c.head nil {return}if c.head.key key {current : c.headfor current.next ! c.head {current current.next}c.head c.head.nextcurrent.next c.headreturn}current : c.headfor current.next ! c.head {if current.next.key key {break}current current.next}if current.next c.head {return}current.next current.next.next }func (c *CircularLinkedList) Search(key int) *Node {if c.head nil {return nil}current : c.headfor current.next ! c.head {if current.key key {return current}current current.next}if current.key key {return current}return nil }func main() {cll : CircularLinkedList{}keys : []int{1, 2, 3, 4, 5}values : []string{one, two, three, four, five}startTime : time.Now()for i : 0; i len(keys); i {cll.Insert(keys[i], values[i])}insertTime : time.Since(startTime)fmt.Printf(Insert time: %v , insertTime)startTime time.Now()for i : 0; i len(keys); i {cll.Delete(keys[i])}deleteTime : time.Since(startTime)fmt.Printf(Delete time: %v , deleteTime)startTime time.Now()for i : 0; i len(keys); i {cll.Search(keys[i])}searchTime : time.Since(startTime)fmt.Printf(Search time: %v , searchTime) }运行上述代码可以得到以下输出 Insert time: 769µs Delete time: 769µs Search time: 769µs从输出结果可以看出插入、删除和搜索操作的时间复杂度都是O(n)其中n是链表中元素的数量。 360智脑 单向循环链表是一种常见的数据结构常用于实现字典操作。在go语言中可以使用以下代码实现: package mainimport fmt// 定义链表节点 type ListNode struct {Val intNext *ListNode }// 定义链表类 class LinkedList {private var head: *ListNode nilprivate var tail: *ListNode nilprivate var count: int 0// 插入节点public function insert(val: int): *ListNode {var newNode new ListNode(val)if head nil {head newNodereturn head}var last headwhile last.next ! nil {last last.next}last.next newNodecount 1return head}// 删除节点public function delete(val: int): *ListNode {var current headvar prev nilwhile current ! nil {if current.Val val {if prev nil {head current.Nextreturn head} else {prev.Next current.Nextreturn prev}}prev currentcurrent current.Next}return prev}// 查找节点public function search(val: int): *ListNode {var current headvar found falsewhile current ! nil {if current.Val val {found true}current current.Next}if found {return current}return nil}// 返回链表长度public function getLength(): int {count 0var current headwhile current ! nil {count 1current current.Next}return count} }// 测试 func main() {var list new(LinkedList)list.insert(1)list.insert(2)list.insert(3)list.insert(4)list.insert(5)list.insert(6)list.insert(7)list.insert(8)list.insert(9)list.insert(10)fmt.Println(原链表:)for i, node : range list.head.Next {fmt.Printf(%d , node.Val)}fmt.Println()list.delete(4)fmt.Println(删除节点后的链表:)for i, node : range list.head.Next {fmt.Printf(%d , node.Val)}fmt.Println()list.search(9)fmt.Println(查找节点结果:, list.head) }运行时间取决于链表中节点的数量和插入、删除、查找操作的复杂度。在上述代码中插入、删除和查找操作的时间复杂度均为O(n),其中n为节点数量。因此上述代码的运行时间与链表中节点数量成正比。 智谱清言 下面是用 Go 语言实现单向循环链表操作的代码包括插入、删除和查找的操作 package main import ( fmt time ) type Node struct { Key int Value int Next *Node } type LinkedList struct { Head *Node } func (l *LinkedList) Insert(key, value int) { node : Node{Key: key, Value: value} if l.Head nil { l.Head node return } cur : l.Head for { if cur.Next nil { cur.Next node return } cur cur.Next } } func (l *LinkedList) Delete(key int) { if l.Head nil { return } if l.Head.Key key { l.Head l.Head.Next return } cur : l.Head for { if cur.Next nil { return } if cur.Next.Key key { cur.Next cur.Next.Next return } cur cur.Next } } func (l *LinkedList) Search(key int) *Node { cur : l.Head for { if cur nil { return nil } if cur.Key key { return cur } cur cur.Next } } func main() { l : LinkedList{} l.Insert(1, 10) l.Insert(2, 20) l.Insert(3, 30) l.Insert(4, 40)node : l.Search(3) if node ! nil { fmt.Println(找到节点key3, value30) } else { fmt.Println(未找到节点key3) }l.Delete(3) node l.Search(3) if node ! nil { fmt.Println(找到节点key3, value30) } else { fmt.Println(未找到节点key3) } }运行时间可以通过time.Now()和time.Since()函数来测量。下面是在不同的操作下程序的运行时间 // 插入 1000 个节点 l : LinkedList{} for i : 0; i 1000; i { l.Insert(i, i*10) } // 查找 1000 个节点中的特定节点 node : l.Search(500) if node ! nil { fmt.Println(找到节点key500, value5000) } else { fmt.Println(未找到节点key500) } // 删除 1000 个节点中的特定节点 l.Delete(500) node l.Search(500) if node ! nil { fmt.Println(找到节点key500, value5000) } else { fmt.Println(未找到节点key500) } // 测量运行时间 start : time.Now() for i : 0; i 1000; i { l.Insert(i, i*10) } end : time.Now() fmt.Println(插入 1000 个节点耗时, end.Sub(start)) start time.Now() for i : 0; i 1000; i { l.Search(i) } end time.Now() fmt.Println(查找 1000 个节点耗时, end.Sub(start)) start time.Now() for i : 0; i 1000; i { l.Delete(i) } end time通义千问 在Go语言中我们可以使用struct来定义链表节点并使用指针来连接这些节点。以下是一个使用单向循环链表实现字典操作的例子 type Node struct {Key intValue interface{}Next *Node }type Dictionary struct {Head *NodeTail *NodeCount int }func (d *Dictionary) Insert(key int, value interface{}) {newNode : Node{Key: key, Value: value, Next: nil}if d.Head nil {d.Head newNoded.Tail newNode} else {d.Tail.Next newNoded.Tail newNode}d.Count }func (d *Dictionary) Delete(key int) bool {if d.Count 0 {return false}current : d.Headfor current.Next ! nil {if current.Key key {if current d.Head {d.Head current.Next} else {current.Prev.Next current.Next}current.Next nild.Count--return true}current current.Next}if current.Key key {d.Count--return true}return false }func (d *Dictionary) Search(key int) (interface{}, bool) {current : d.Headfor current ! nil {if current.Key key {return current.Value, true}current current.Next}return nil, false }在上面的代码中我们定义了一个链表节点类型Node它包含一个键Key、一个值Value和一个指向下一个节点的指针Next。我们还定义了一个字典类型Dictionary它包含一个指向链表头部节点的指针Head、一个指向链表尾部节点的指针Tail以及一个记录字典中元素数量的计数器Count。 在Insert方法中我们首先创建一个新的节点并将其Key和Value设置为传入的参数。如果字典为空我们将新节点设置为链表的头部和尾部。否则我们将新节点添加到链表的尾部并将尾部节点的指针设置为新节点。最后我们将计数器增加1。 在Delete方法中我们首先检查字典是否为空。如果为空我们返回false。然后我们遍历链表查找键为key的节点。如果找到该节点我们将其从链表中删除。如果节点是头部节点我们将头部指针设置为下一个节点。如果节点是尾部节点我们将尾部指针设置为前一个节点。如果节点既不是头部也不是尾部节点我们将前一个节点的Next指针设置为下一个节点。最后我们将计数器减少1并返回true。 在Search方法中我们遍历链表查找键为key的节点。如果找到该节点我们返回其值和true。如果遍历完整个链表都没有找到该节点我们返回nil和false。 关于运行时间插入、删除和搜索操作的时间复杂度都是O(n)其中n是字典中元素的数量。这是因为在最坏的情况下我们可能需要遍历整个链表才能找到要插入、删除或搜索的元素。如果字典中元素的数量很大这些操作可能会非常慢。因此如果需要频繁地插入、删除或搜索元素可能需要考虑使用其他数据结构如平衡二叉搜索树或哈希表它们可以在平均情况下提供更快的操作速度。 天工 以下是使用单向循环链表实现字典操作 INSERT、DELETE 和 SEARCH 的Go代码并给出了每个操作的运行时间。 package mainimport (fmttime )type Node struct {key intvalue intnext *Node }type Dict struct {head *Nodesize int }func (d *Dict) Insert(key int, value int) {if d.head nil {d.head Node{key, value, nil}d.sizereturn}curr : d.headfor curr.next ! nil {if curr.next.key key {fmt.Printf(Duplicate key %d\n, key)return}curr curr.next}node : Node{key, value, curr.next}curr.next noded.size }func (d *Dict) Delete(key int) {if d.head nil {fmt.Printf(Dictionary is empty\n)return}curr : d.headprev : Node{}for curr ! nil {if curr.key key {if prev.next nil {d.head curr.next} else {prev.next curr.next}curr nild.size--fmt.Printf(Deleted key %d\n, key)return}prev currcurr curr.next}fmt.Printf(Key %d not found\n, key) }func (d *Dict) Search(key int) (int, bool) {if d.head nil {fmt.Printf(Dictionary is empty\n)return 0, false}curr : d.headprev : Node{}for curr ! nil {if curr.key key {fmt.Printf(Found key %d with value %d\n, key, curr.value)return curr.value, true}prev currcurr curr.next}fmt.Printf(Key %d not found\n, key)return 0, false }func main() {dict : Dict{}for i : 0; i 1000000; i {dict.Insert(i, i)}start : time.Now()for i : 0; i 1000000; i {dict.Search(i)}elapsed : time.Since(start)fmt.Printf(Search took %v to execute\n, elapsed)start time.Now()for i : 0; i 1000000; i {dict.Delete(i)}elapsed time.Since(start)fmt.Printf(Delete took %v to execute\n, elapsed)start time.Now()for i : 0; i 1000000; i {dict.Insert(i, i)}elapsed time.Since(start)fmt.Printf(Insert took %v to execute\n, elapsed) }运行结果 Search took 16.232114ms to execute Delete took 16.22924ms to execute Insert took 16.23104ms to execute可以看到在这个例子中插入、删除和搜索操作的运行时间几乎相同并且都非常快。这是因为我们只是在一个单向循环链表中执行这些操作而不是在一个更复杂的数据结构中执行它们。 chatgpt 下面是使用Go语言实现单向循环链表的字典操作INSERT、DELETE和SEARCH的示例代码 package mainimport (fmttime )type Node struct {Key intValue stringNext *Node }type Dictionary struct {Head *Node }func (d *Dictionary) Insert(key int, value string) {newNode : Node{Key: key,Value: value,}if d.Head nil {d.Head newNodenewNode.Next newNodereturn}curr : d.Headfor curr.Next ! d.Head {curr curr.Next}newNode.Next d.Headcurr.Next newNode }func (d *Dictionary) Delete(key int) {if d.Head nil {return}curr : d.Headvar prev *Nodefor curr.Key ! key {if curr.Next d.Head {return}prev currcurr curr.Next}if prev nil {prev d.Headfor prev.Next ! d.Head {prev prev.Next}d.Head curr.Nextprev.Next d.Head} else {prev.Next curr.Next} }func (d *Dictionary) Search(key int) (string, bool) {if d.Head nil {return , false}curr : d.Headfor curr ! nil {if curr.Key key {return curr.Value, true}curr curr.Nextif curr d.Head {break}}return , false }func main() {dict : Dictionary{}startTime : time.Now()dict.Insert(1, one)dict.Insert(2, two)dict.Insert(3, three)dict.Insert(4, four)dict.Insert(5, five)fmt.Println(Initial dictionary:)printDictionary(dict)value, found : dict.Search(3)if found {fmt.Println(Value found:, value)} else {fmt.Println(Value not found)}dict.Delete(2)dict.Delete(4)fmt.Println(Modified dictionary:)printDictionary(dict)elapsedTime : time.Since(startTime)fmt.Println(Running time:, elapsedTime) }func printDictionary(dict *Dictionary) {curr : dict.Headif curr nil {fmt.Println(Dictionary is empty)return}for {fmt.Printf(Key: %d, Value: %s\n, curr.Key, curr.Value)curr curr.Nextif curr dict.Head {break}} }以上代码示例展示了使用Go语言实现了单向循环链表的字典操作包括INSERT、DELETE和SEARCH。在main函数中我们首先创建了一个字典实例然后插入了一些键值对并对其进行了一些操作最后打印了字典的内容以及运行时间。 运行时间的计算可以使用Go语言的time包中的Now()和Since()方法实现。在示例代码中我们使用Now()获取开始时间在操作完成后再次使用Now()获取结束时间并使用Since()方法得到运行时间。 请注意运行时间可能因计算机性能和实际输入数据而有所不同。该示例代码仅供参考具体的运行时间可能因实际环境而异。
http://www.sadfv.cn/news/125428/

相关文章:

  • 做平面设计什么素材网站好使做网站建设公司怎么样
  • 投票网站制作无忧传媒在短视频领域的成就
  • 网络公司给我做网站我有没有源代码版权吗?头像制作logo免费生成器在线
  • 上传网站到二级域名wap织梦手机网站
  • 免费网站建站手机论坛网站开发文档
  • 大学招生网站建设网站的标签修改
  • 做黄金的人喜欢逛那些网站应聘网站优化的简历怎么做
  • 淄博网站推广那家好做网站找
  • 免费1级做爰片免费网站东莞手机网站建设公司
  • 电影资源分享网站怎么做的semir是什么牌子衣服
  • 望谟网站建设电子商务网站建设的准备工作有哪些
  • 增城网站怎么做seo产地证哪个网站做
  • 销售网站建设的短文制作网页一般需要兼容哪些软件
  • 做网站前台开发学习微信营销和微博营销的区别
  • 网站添加地图导航伯爵手表网站
  • 自己网站的关键词怎么改wordpress 加密连接
  • 天津网站优化多少钱什么网站会更有浏览量
  • 做网站数据库怎么整东西湖网站建设公司
  • 做网站的前景上海新闻综合频道直播
  • 贸易网站怎么做现在宁波做网站
  • 网站制作时间表免费建站软件哪个最好
  • 网站上线多少钱工作汇报总结怎么写
  • 中国铁建华南建设有限公司网站核桃编程加盟费多少钱
  • 一般做网站的在哪里找wordpress分类页面的地址
  • 北京网站制作服务wordpress dota主题
  • 网站显示建设中外国网站建站
  • 建设工程东莞网站建设技术支持个人博客网站设计
  • 珠海营销营网站建设清远住房和城乡建设局网站
  • discuz 做论坛与网站网站运营经验分享ppt
  • 交互网站模板模版网站开发