You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

github.com/m-simeonov/go-data-structures

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/m-simeonov/go-data-structures

v0.0.0-20241111135551-ff78c61faabb
Source
Go
Version published
Created
Source

Data Structures

install package

go get github.com/m-simeonov/go-data-structures

LIFO Stack (Last In First Out)

Stack is a linear data structure which follows a particular order in which the operations are performed. The order is Last In First Out.

Init Stack

stack := stack.New()

Push Element

stack.Push("one")

Pop Element

val := stack.Pop()

Get Length

len := stack.GetLength()

Example LIFO Stack

lifoStack := stack.New()
// Push Element to LifoStack
lifoStack.Push("one")
lifoStack.Push("two")

fmt.Println(lifoStack.GetLength()) // will print `2`

element, err := lifoStack.Pop()
if err == nil {
    fmt.Println(element) // will print `two`
}

Linked List

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. A linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list

Init linked list

linkedList := list.New()

Add Value to linked list

linkedList.Add("one")

Delete node

linkedList.Delete(head)

Convert linked list to slice

data := linkedList.ToSlice()

Get Head

tail := linkedList.GetHead()

Get Tail

tail := linkedList.GetTail()

Get Length

len := linkedList.GetLength()

Example linked list

linkedList := list.New()
linkedList.Add("one")
linkedList.Add("two")
linkedList.Add("three")

data := linkedList.ToSlice()
fmt.Println(data) // will print [one two three]

len := linkedList.GetLength()
fmt.Println(len) // will print 3

head := linkedList.GetHead()
fmt.Println(head.Data) // will print `one`
linkedList.Delete(head)

data = linkedList.ToSlice()
fmt.Println(data) // will print [two three]

FAQs

Package last updated on 11 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts