Algoritma dan Pemograman dengan Bahasa Pemograman Go : LInked List

mplementasi dari struktur data Linked List dalam bahasa pemrograman Go. Linked List adalah struktur data yang terdiri dari serangkaian elemen yang disebut node, di mana setiap node memiliki dua atribut: nilai (property) dan pointer (nextNode) yang menunjuk ke node berikutnya. Dalam kode ini, terdapat dua struktur utama: Node, yang merepresentasikan elemen individual dalam linked list, dan LinkedList, yang merepresentasikan keseluruhan linked list itu sendiri dengan atribut headNode yang menunjuk ke node pertama.

Terdapat beberapa metode dalam kelas LinkedList, termasuk AddToHead, yang menambahkan node baru di awal list, dan AddToEnd, yang menambahkan node di akhir list. Metode NodeWithValue digunakan untuk mencari node berdasarkan nilai tertentu, sedangkan AddAfter memungkinkan penambahan node baru setelah node yang sudah ada. Metode LastNode mengembalikan node terakhir dalam list, dan IterateList mencetak semua nilai dari node yang ada dalam linked list. Fungsi main berfungsi sebagai titik masuk program, di mana sebuah instance dari LinkedList dibuat, beberapa node ditambahkan, dan kemudian seluruh isi linked list dicetak ke layar. Implementasi ini menunjukkan bagaimana linked list dapat digunakan untuk menyimpan dan mengelola data secara dinamis dalam memori.

package main

import (
	"fmt"
)

//Node class
type Node struct {
	property int
	nextNode *Node
}

// LinkedList class
type LinkedList struct {
	headNode *Node
}

//AddToHead method of LinkedList class
func (linkedList *LinkedList) AddToHead(property int) {
	var node = &Node{}
	node.property = property
	node.nextNode = nil

	if linkedList.headNode != nil {
		//fmt.Println(node.property)
		node.nextNode = linkedList.headNode
	}

	linkedList.headNode = node

}

//NodeWithValue method returns Node given parameter property
func (linkedList *LinkedList) NodeWithValue(property int) *Node {
	var node *Node
	var nodeWith *Node
	for node = linkedList.headNode; node != nil; node = node.nextNode {
		if node.property == property {
			nodeWith = node
			break
		}
	}
	return nodeWith
}

//AddAfter method  adds a node with nodeProperty after node with property
func (linkedList *LinkedList) AddAfter(nodeProperty int, property int) {
	var node = &Node{}
	node.property = property
	node.nextNode = nil

	var nodeWith *Node

	nodeWith = linkedList.NodeWithValue(nodeProperty)
	if nodeWith != nil {
		//fmt.Println(node.property)
		node.nextNode = nodeWith.nextNode
		nodeWith.nextNode = node
	}

}

//LastNode method returns the last Node
func (linkedList *LinkedList) LastNode() *Node {
	var node *Node
	var lastNode *Node
	for node = linkedList.headNode; node != nil; node = node.nextNode {
		if node.nextNode == nil {
			lastNode = node
		}
	}
	return lastNode
}

//AddToEnd method adds the node with property to the end
func (linkedList *LinkedList) AddToEnd(property int) {
	var node = &Node{}
	node.property = property
	node.nextNode = nil

	var lastNode *Node

	lastNode = linkedList.LastNode()

	if lastNode != nil {
		lastNode.nextNode = node
	}

}

//IterateList method iterates over LinkedList
func (linkedList *LinkedList) IterateList() {

	var node *Node
	for node = linkedList.headNode; node != nil; node = node.nextNode {
		fmt.Println(node.property)

	}
}

// main method
func main() {

	var linkedList LinkedList

	linkedList = LinkedList{}

	linkedList.AddToHead(1)
	linkedList.AddToHead(3)
	linkedList.AddToEnd(5)
	linkedList.AddAfter(1, 7)
	//fmt.Println(linkedList.headNode.property)

	linkedList.IterateList()

}

Berikut adalah output jika codingan diatas di running