go 单链表 增删改查

package main

import "fmt"

//链表和数据结构
type Node struct{
	data string
	next *Node
}

type List struct{
	Head *Node
}

//链表是否为空
func (this *List) IsEmpty() bool{
	if this.Head == nil {
		return true
	}

	return false
}

//链表长度
func (this *List) Length() int{
	if this.IsEmpty() {
		return 0
	}

	node := this.Head
	count := 0
	for node != nil{
		node = node.next
		count ++
	}
	return count
}

//从头部添加元素
func (this *List) Add(data string){
	node := &Node{data:data}
	node.next = this.Head
	fmt.Println(this.Head)
	this.Head = node
	fmt.Println(this.Head)

}

//从尾部添加元素
func (this *List) Append(data string){
	//先储存数据
	node := &Node{data:data}

	//判断是否为空
	if this.IsEmpty() {
		this.Head = node
	}else{
		list := this.Head
		for list.next != nil{
			list = list.next
		}
		list.next = node
	}
}

//插入到指定位置
func (this *List) InsertNode(index int , data string) bool{
	if index < 0 {
		this.Add(data)
	}else{
		pre := this.Head
		count := 0
		for count < (index -1) {
			pre = pre.next
			count++
		}

		//把pre的下个节点指针给了node
		node := &Node{data:data}
		node.next=pre.next
		pre.next = node
	}
	return true
}

//删除节点中所有相等的数据
func (this *List) removeNode(data string) bool{
	list := this.Head
	if list.data == data {
		this.Head = list.next
	}else{
		for list.next != nil {
			if list.next.data == data{
				list.next = list.next.next
			}else{
				list = list.next
			}
		}
	}

	return true
}


//查找
func (this *List) findNode(data string) int{

	list := this.Head

	var count = 0
	for list != nil {
		count ++
		if list.data == data {
			return count
		}
		list = list.next
	}
	return count
}

//循环链表
func (this *List) foreach(){

	node := this.Head

	for node != nil {
		fmt.Println(node.data)
		node = node.next
	}
}

//链表反转
func (this *List) reseve() *Node{
	node := this.Head
	var pre *Node
	for node !=nil{
		node.next, pre, node = pre, node, node.next
	}

	return pre
}

//打印指定链表
func printNode(node *Node){
	for node != nil{
		fmt.Println(node.data)
		node = node.next
	}
}

func main(){

	list := &List{}
	list.Add("data")
	list.Append("hello")

	list.Append("world")
	list.foreach()
	pos := list.findNode("hello")
	fmt.Println(pos)
	newList := list.reseve()
	printNode(newList)
}

 

当使用Golang进行增删改查操作时,通常需要使用数据库驱动程序来连接数据库。下面是一个使用Golang进行增删改查的示例: 首先,你需要导入所需的包: ```go import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" ) ``` 然后,你需要建立数据库连接: ```go db, err := sql.Open("mysql", "用户名:密码@tcp(localhost:3306)/数据库名") if err != nil { // 处理连接错误 } defer db.Close() ``` 接下来,我们可以进行增加、删除、修改和查询操作。 1. 增加数据: ```go stmt, err := db.Prepare("INSERT INTO 表名 (字段1, 字段2) VALUES (?, ?)") if err != nil { // 处理准备语句错误 } defer stmt.Close() _, err = stmt.Exec(值1, 值2) if err != nil { // 处理执行语句错误 } ``` 2. 删除数据: ```go stmt, err := db.Prepare("DELETE FROM 表名 WHERE 条件") if err != nil { // 处理准备语句错误 } defer stmt.Close() _, err = stmt.Exec(参数) if err != nil { // 处理执行语句错误 } ``` 3. 修改数据: ```go stmt, err := db.Prepare("UPDATE 表名 SET 字段1 = ?, 字段2 = ? WHERE 条件") if err != nil { // 处理准备语句错误 } defer stmt.Close() _, err = stmt.Exec(值1, 值2, 参数) if err != nil { // 处理执行语句错误 } ``` 4. 查询数据: ```go rows, err := db.Query("SELECT 字段1, 字段2 FROM 表名 WHERE 条件", 参数) if err != nil { // 处理查询错误 } defer rows.Close() for rows.Next() { var 字段1 类型1 var 字段2 类型2 err := rows.Scan(&字段1, &字段2) if err != nil { // 处理扫描错误 } // 处理查询结果 fmt.Println(字段1, 字段2) } ``` 这些示例代码可以帮助你开始在Golang中进行增删改查操作。请根据你的具体需求进行相应的调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值