首页 > 其他 > 详细

Golang实现简单的链表

时间:2021-01-22 17:00:37      阅读:26      评论:0      收藏:0      [点我收藏+]

直接代码吧,都是我自己写的测试代码

package main

import "fmt"

type NodeList struct {
	data int
	next *NodeList
}

func ShowNode(node *NodeList)  {
	for node !=nil {
		//fmt.Printf("type :%T ,value:%v\n",*node,*node)
		//fmt.Printf("data:%v,next:%v--->",node.data,node.next)
		fmt.Printf("%v--->",node.data)
		node = node.next //移动指针
	}
}


func InsertHeadData(){
	var head = new(NodeList)
	head.data = 0
	var tail *NodeList
	tail = head
	for i :=1;i<10;i++{
		var node = NodeList{data: i}
		node.next = tail
		tail = &node
	}
	ShowNode(tail)
}

func InsertTailData()  {
	var head = new(NodeList)
	head.data = 0
	var newList *NodeList
	newList = head
	for i :=1;i<10;i++{
		var node = NodeList{data: i}
		(*newList).next = &node
		newList = &node
	}
	ShowNode(head)
}

func main() {
	//node1 := new(NodeList)
	//node2 := new(NodeList)
	//node1.data = 1
	//node2.data = 2
	//
	//node1.next = node2
	//
	//node3 := new(NodeList)
	//node2.next = node3
	//
	//ShowNode(node1)
	//InsertHeadData()
	InsertTailData()
}

  

Golang实现简单的链表

原文:https://www.cnblogs.com/pebblecome/p/14312429.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!