找到 34423 篇文章,关于编程

Go 语言程序:在链表的最后一个位置插入节点到第 i 个索引处。

Rishikesh Kumar Rishi
更新于 2021年3月18日 12:16:15

109 次浏览

示例示例解决此问题的方法 在线演示package main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node    n.value = value    n.next = next    return &n } func TraverseLinkedList(head *Node){    temp := head    for temp != nil {       fmt.Printf("%d ", temp.value)       temp = temp.next    }    fmt.Println() } func InsertNodeAtIthIndex(head *Node, index, data int) *Node{    if head == nil{       head = NewNode(data, nil)       return head    }    if ... 阅读更多

Go 语言程序:递归实现中序遍历给定树。

Rishikesh Kumar Rishi
更新于 2021年3月18日 12:14:16

281 次浏览

示例假设我们有以下树。中序树遍历输出 - 4 2 5 1 6 3 7解决此问题的方法步骤 1 - 如果给定树的根节点为 nil,则返回;否则,按照以下步骤操作。步骤 2 - 遍历左子树。步骤 3 - 打印根节点数据。步骤 4 - 遍历右子树。示例 在线演示package main import "fmt" type Node struct {    data int    left *Node    right *Node } func (root *Node)InOrderTraversal(){    if root !=nil{       root.left.InOrderTraversal()       fmt.Printf("%d ", root.data)       root.right.InOrderTraversal()   ... 阅读更多

Go 语言程序:在链表超出范围的第 n 个索引处插入节点到第 i 个索引处。

Rishikesh Kumar Rishi
更新于 2021年3月18日 12:12:31

72 次浏览

示例示例 在线演示package main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node    n.value = value    n.next = next    return &n } func TraverseLinkedList(head *Node){    temp := head    for temp != nil {       fmt.Printf("%d ", temp.value)       temp = temp.next    }    fmt.Println() } func InsertNodeAtIthIndex(head *Node, index, data int) *Node{    if head == nil{       head = NewNode(data, nil)       return head    }    if index == 0{   ... 阅读更多

Go 语言程序:在链表中间位置插入节点到第 i 个索引处。

Rishikesh Kumar Rishi
更新于 2021年3月18日 12:07:49

65 次浏览

示例示例 在线演示package main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node    n.value = value    n.next = next    return &n } func TraverseLinkedList(head *Node){    temp := head    for temp != nil {       fmt.Printf("%d ", temp.value)       temp = temp.next    }    fmt.Println() } func InsertNodeAtIthIndex(head *Node, index, data int) *Node{    if head == nil{       head = NewNode(data, nil)       return head    }    if index == 0{   ... 阅读更多

Go 语言程序:在链表的第 0 个位置插入节点到第 i 个索引处。

Rishikesh Kumar Rishi
更新于 2021年3月18日 11:59:36

342 次浏览

示例解决此问题的方法步骤 1 - 定义一个接受链表头的方法。步骤 2 - 如果 head == nil,则创建一个新节点并将其设为 head,并将其作为新的 head 返回。步骤 3 - 当 index == 0 时,更新 head。步骤 4 - 从其 head 迭代给定的链表。此外,初始化 preNode,它将存储前一个节点的地址。步骤 5 - 如果索引 i 与给定索引匹配,则删除该节点.next,中断循环。步骤 6 - 在循环结束时返回。示例 在线演示package main ... 阅读更多

Go 语言程序:删除链表中超出范围的第 i 个索引节点。

Rishikesh Kumar Rishi
更新于 2021年3月18日 11:57:21

160 次浏览

示例解决此问题的方法步骤 1 - 定义一个接受链表头的方法。步骤 2 - 如果 head == nil,则返回 head。步骤 3 - 当 index == 0 时,返回 head.next步骤 4 - 否则,从链表头迭代给定的链表。步骤 5 - 如果索引 i 与给定索引(要删除的索引)匹配,则删除该节点.next,中断循环。步骤 6 - 在循环结束时返回。示例 在线演示package main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n ... 阅读更多

Go 语言程序:删除链表中最后一个索引处的第 i 个索引节点。

Rishikesh Kumar Rishi
更新于 2021年3月18日 11:55:37

65 次浏览

示例解决此问题的方法步骤 1 - 定义一个接受链表头的方法。步骤 2 - 如果 head == nil,则返回 head。步骤 3 - 当 index == 0 时,返回 head.next步骤 4 - 否则,从链表头迭代给定的链表。步骤 5 - 如果索引 i 与给定索引(要删除的索引)匹配,则删除该节点.next,中断循环。步骤 6 - 在循环结束时返回。示例 在线演示package main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n ... 阅读更多

Go 语言程序:删除链表中间级别索引处的第 i 个索引节点。

Rishikesh Kumar Rishi
更新于 2021年3月18日 11:53:28

84 次浏览

示例解决此问题的方法步骤 1 - 定义一个接受链表头的方法。步骤 2 - 如果 head == nil,则返回 head。步骤 3 - 当 index == 0 时,返回 head.next步骤 4 - 否则,从 head 迭代给定的链表。步骤 5 - 如果索引 i 与给定索引(要删除的索引)匹配,则删除该节点.next,中断循环。步骤 6 - 在循环结束时返回。示例 在线演示package main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node ... 阅读更多

Go 语言程序:删除链表第 0 个位置的第 i 个索引节点。

Rishikesh Kumar Rishi
更新于 2021年3月18日 11:50:13

75 次浏览

示例解决此问题的方案步骤 1 - 定义一个接受链表头的方法。步骤 2 - 如果 head == nil,则返回 head。步骤 3 - 当 index == 0 时,返回 head.next步骤 4 - 否则,迭代给定的链表从 head 开始。步骤 5 - 如果索引 i 与给定的索引(要删除的索引)匹配,则删除该节点.next,并中断循环。步骤 6 - 在循环结束时返回。示例 演示程序包 main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node    n.value = value    ... 阅读更多

Go 语言程序:后序遍历给定树(递归)。

Rishikesh Kumar Rishi
更新于 2021-03-18 11:46:29

260 次浏览

示例假设我们有以下二叉树。后序树遍历输出 - 2 4 5 3 6 7 1。解决此问题的方案步骤 1 - 如果给定树的根节点为 nil,则返回;否则,按照以下步骤操作。步骤 2 - 遍历左子树。步骤 3 - 遍历右子树。步骤 4 - 打印根节点数据。示例 演示程序包 main import "fmt" type Node struct {    data int    left *Node    right *Node } func (root *Node)PostOrderTraversal(){    if root !=nil{       root.left.PostOrderTraversal()       root.right.PostOrderTraversal()       fmt.Printf("%d ", root.data) ... 阅读更多

广告
© . All rights reserved.