Go语言实现常见数据结构

1. 栈(Stack)

go 复制代码
package main

import "fmt"

// Stack structure using a slice
type Stack struct {
	items []int
}

// Push an item onto the stack
func (s *Stack) Push(item int) {
	s.items = append(s.items, item)
}

// Pop an item from the stack
func (s *Stack) Pop() int {
	if len(s.items) == 0 {
		return -1 // Stack is empty
	}
	top := s.items[len(s.items)-1]
	s.items = s.items[:len(s.items)-1]
	return top
}

func main() {
	stack := Stack{}
	stack.Push(1)
	stack.Push(2)
	fmt.Println(stack.Pop()) // 2
	fmt.Println(stack.Pop()) // 1
}

2. 队列(Queue)

go 复制代码
package main

import "fmt"

// Queue structure using a slice
type Queue struct {
	items []int
}

// Enqueue an item to the queue
func (q *Queue) Enqueue(item int) {
	q.items = append(q.items, item)
}

// Dequeue an item from the queue
func (q *Queue) Dequeue() int {
	if len(q.items) == 0 {
		return -1 // Queue is empty
	}
	front := q.items[0]
	q.items = q.items[1:]
	return front
}

func main() {
	queue := Queue{}
	queue.Enqueue(1)
	queue.Enqueue(2)
	fmt.Println(queue.Dequeue()) // 1
	fmt.Println(queue.Dequeue()) // 2
}

3. 链表(LinkedList)

go 复制代码
package main

import "fmt"

// Node represents a node in the linked list
type Node struct {
	value int
	next  *Node
}

// LinkedList structure
type LinkedList struct {
	head *Node
}

// Insert a value at the end of the linked list
func (l *LinkedList) Insert(value int) {
	newNode := &Node{value: value}
	if l.head == nil {
		l.head = newNode
	} else {
		current := l.head
		for current.next != nil {
			current = current.next
		}
		current.next = newNode
	}
}

// Print the linked list
func (l *LinkedList) Print() {
	current := l.head
	for current != nil {
		fmt.Print(current.value, " -> ")
		current = current.next
	}
	fmt.Println("nil")
}

func main() {
	list := LinkedList{}
	list.Insert(1)
	list.Insert(2)
	list.Insert(3)
	list.Print() // 1 -> 2 -> 3 -> nil
}

4. 二叉树(Binary Tree)

go 复制代码
package main

import "fmt"

// TreeNode represents a node in the binary tree
type TreeNode struct {
	value int
	left  *TreeNode
	right *TreeNode
}

// BinaryTree structure
type BinaryTree struct {
	root *TreeNode
}

// Insert a value in the binary tree
func (t *BinaryTree) Insert(value int) {
	if t.root == nil {
		t.root = &TreeNode{value: value}
	} else {
		insertNode(t.root, value)
	}
}

func insertNode(node *TreeNode, value int) {
	if value < node.value {
		if node.left == nil {
			node.left = &TreeNode{value: value}
		} else {
			insertNode(node.left, value)
		}
	} else {
		if node.right == nil {
			node.right = &TreeNode{value: value}
		} else {
			insertNode(node.right, value)
		}
	}
}

// InOrder traversal of the binary tree
func (t *BinaryTree) InOrder() {
	inOrderTraversal(t.root)
}

func inOrderTraversal(node *TreeNode) {
	if node != nil {
		inOrderTraversal(node.left)
		fmt.Print(node.value, " ")
		inOrderTraversal(node.right)
	}
}

func main() {
	tree := BinaryTree{}
	tree.Insert(5)
	tree.Insert(3)
	tree.Insert(7)
	tree.Insert(2)
	tree.Insert(4)
	tree.InOrder() // 2 3 4 5 7
}

5. 散列表(HashMap)

go 复制代码
package main

import "fmt"

// HashMap structure using a map
type HashMap struct {
	items map[string]int
}

// Initialize a new hash map
func (h *HashMap) Init() {
	h.items = make(map[string]int)
}

// Insert a key-value pair into the hash map
func (h *HashMap) Insert(key string, value int) {
	h.items[key] = value
}

// Get a value by key from the hash map
func (h *HashMap) Get(key string) int {
	if value, found := h.items[key]; found {
		return value
	}
	return -1 // Key not found
}

// Delete a key-value pair from the hash map
func (h *HashMap) Delete(key string) {
	delete(h.items, key)
}

func main() {
	hashMap := HashMap{}
	hashMap.Init()
	hashMap.Insert("a", 1)
	hashMap.Insert("b", 2)
	fmt.Println(hashMap.Get("a")) // 1
	fmt.Println(hashMap.Get("b")) // 2
	hashMap.Delete("a")
	fmt.Println(hashMap.Get("a")) // -1
}
相关推荐
艾莉丝努力练剑23 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
_殊途2 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
倔强青铜34 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian5 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
珊瑚里的鱼5 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上5 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang6 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab
Risehuxyc6 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
AI视觉网奇6 小时前
git 访问 github
运维·开发语言·docker
不知道叫什么呀6 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc