singlylinkedlist

package module
v0.0.0-...-a261c28 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2018 License: MIT Imports: 0 Imported by: 0

README

Singly Linked List Implementation in Go

GoDoc

Overview

A working implementation of a singly-linked list in Go. Please issue a pull request if you want to improve the code in this repository.

License

MIT license, for more information see the LICENSE file.

Documentation

Overview

Package singlylinkedlist implements a singly-linked list in Go without third-party libraries. Supported methods are 'PushFront' and 'PushBack'. The list keeps track of its length. Deletion of elements is implemented as well.

Code example:

package main

import (
	"fmt"

	sll "github.com/DanielSchuette/singlylinkedlist"
)

func main() {
	// create a new list
	list := sll.New()

	// add some nodes/elements to the list
	list.PushBack(1)
	list.PushBack(2)
	list.PushBack(3)
	list.PushBack(4)
	list.PushBack(11)
	list.PushBack(7)
	list.PushFront(-1)
	list.PushFront(-2)
	list.PushFront(-3)

	// delete an existing and non-existing item from the list
	del := list.Delete(11)
	fmt.Printf("node deleted: %v\n", del)
	del = list.Delete(-12)
	fmt.Printf("node deleted: %v\n", del)

	// print length of list
	fmt.Printf("list length: %v\n", list.Length())

	// print list elements
	for n := list.Head(); n != nil; n = n.Next() {
		fmt.Println(n.Value())
	}
}

`main/main.go` contains this working example. It produces the following output:

node deleted: true
node deleted: false
list length: 8
-3
-2
-1
1
2
3
4
7

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type List

type List struct {
	// contains filtered or unexported fields
}

List holds a pointer to the first node in a singly-linked list with a length of zero or greater and List keeps track of the current number of nodes that is also always zero or greater

func New

func New() *List

New returns a new singly-linked list

func (*List) Delete

func (l *List) Delete(item interface{}) bool

Delete deletes the first node that contains a particular item Delete returns true if a node was deleted, otherwise it returns false

func (*List) Head

func (l *List) Head() *Node

Head returns a pointer to the first of the singly-linked list

func (*List) Length

func (l *List) Length() int64

Length returns the length of a singly-linked list

func (*List) PushBack

func (l *List) PushBack(data interface{})

PushBack adds a node to the front of a singly-linked list

func (*List) PushFront

func (l *List) PushFront(data interface{})

PushFront adds a node to the front of a singly-linked list

func (*List) Tail

func (l *List) Tail() *Node

Tail returns a pointer to the last element of the singly-linked list

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node holds some data and a pointer to the next node or nil if Node is the last element in this particular singly-linked list

func (*Node) Next

func (n *Node) Next() *Node

Next returns the next pointer of a particular node

func (*Node) Value

func (n *Node) Value() interface{}

Value returns the data element of a particular node

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL