Documentation
¶
Overview ¶
Package linkedlist demonstrates different implementations on linkedlists.
Index ¶
- func JosephusProblem(cl *Cyclic[int], k int) int
- type Cyclic
- type Doubly
- func (ll *Doubly[T]) AddAtBeg(val T)
- func (ll *Doubly[T]) AddAtEnd(val T)
- func (ll *Doubly[T]) Back() *Node[T]
- func (ll *Doubly[T]) Count() int
- func (ll *Doubly[T]) DelAtBeg() (T, bool)
- func (ll *Doubly[T]) DelAtEnd() (T, bool)
- func (ll *Doubly[T]) DelByPos(pos int) (T, bool)
- func (ll *Doubly[T]) Display()
- func (ll *Doubly[T]) DisplayReverse()
- func (ll *Doubly[T]) Front() *Node[T]
- func (ll *Doubly[T]) Init() *Doubly[T]
- func (ll *Doubly[T]) MoveToBack(n *Node[T])
- func (ll *Doubly[T]) Remove(n *Node[T]) T
- func (ll *Doubly[T]) Reverse()
- type Node
- type Singly
- func (ll *Singly[T]) AddAtBeg(val T)
- func (ll *Singly[T]) AddAtEnd(val T)
- func (ll *Singly[T]) CheckRangeFromIndex(left, right int) error
- func (ll *Singly[T]) Count() int
- func (ll *Singly[T]) DelAtBeg() (T, bool)
- func (ll *Singly[T]) DelAtEnd() (T, bool)
- func (ll *Singly[T]) DelByPos(pos int) (T, bool)
- func (ll *Singly[T]) Display()
- func (ll *Singly[T]) Reverse()
- func (ll *Singly[T]) ReversePartition(left, right int) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JosephusProblem ¶
https://en.wikipedia.org/wiki/Josephus_problem This is a struct-based solution for Josephus problem.
Types ¶
type Cyclic ¶
Cyclic Struct which cycles the linked list in this implementation.
func (*Cyclic[T]) Add ¶
func (cl *Cyclic[T]) Add(val T)
Inserting the first node is a special case. It will point to itself. For other cases, the node will be added to the end of the list. End of the list is Prev field of current item. Complexity O(1).
func (*Cyclic[T]) Rotate ¶
Rotate list by P places. This method is interesting for optimization. For first optimization we must decrease P value so that it ranges from 0 to N-1. For this we need to use the operation of division modulo. But be careful if P is less than 0. if it is - make it positive. This can be done without violating the meaning of the number by adding to it a multiple of N. Now you can decrease P modulo N to rotate the list by the minimum number of places. We use the fact that moving forward in a circle by P places is the same as moving N - P places back. Therefore, if P > N / 2, you can turn the list by N-P places back. Complexity O(n).
type Doubly ¶
Doubly structure with just the Head Node We call it `Doubly` to make it easier to understand when calling this in peoples own local code to understand and experiment with this easily. For example, we can use gotools `go get` command to get this repository cloned inside the $GOPATH/src/github.com/FridaFino/goalgorithms (you can do this manually as well) and use the import statement as follows:
`import "github.com/FridaFino/goalgorithms/structure/linkedlist"`
and call linkedlist.Doubly to create a new doubly linked list.
func (*Doubly[T]) AddAtBeg ¶
func (ll *Doubly[T]) AddAtBeg(val T)
AddAtBeg Add a node to the beginning of the linkedlist
func (*Doubly[T]) AddAtEnd ¶
func (ll *Doubly[T]) AddAtEnd(val T)
AddAtEnd Add a node at the end of the linkedlist
func (*Doubly[T]) DelByPos ¶
DelByPos deletes node at middle based on position in list and returns value. If empty or position of node is less than linked list length, returns false
func (*Doubly[T]) DisplayReverse ¶
func (ll *Doubly[T]) DisplayReverse()
DisplayReverse Display the linkedlist in reverse order
func (*Doubly[T]) MoveToBack ¶
type Node ¶
Node Structure representing the linkedlist node. This node is shared across different implementations.
type Singly ¶
type Singly[T any] struct { // Note that Node here holds both Next and Prev Node // however only the Next node is used in Singly methods. Head *Node[T] // contains filtered or unexported fields }
Singly structure with length of the list and its head
func (*Singly[T]) AddAtBeg ¶
func (ll *Singly[T]) AddAtBeg(val T)
AddAtBeg adds a new snode with given value at the beginning of the list.
func (*Singly[T]) AddAtEnd ¶
func (ll *Singly[T]) AddAtEnd(val T)
AddAtEnd adds a new snode with given value at the end of the list.
func (*Singly[T]) CheckRangeFromIndex ¶
func (*Singly[T]) DelAtBeg ¶
DelAtBeg deletes the snode at the head(beginning) of the list and returns its value. Returns false if the list is empty.
func (*Singly[T]) DelAtEnd ¶
DelAtEnd deletes the snode at the tail(end) of the list and returns its value. Returns false if the list is empty.
func (*Singly[T]) DelByPos ¶
DelByPos deletes the node at the middle based on position in the list and returns its value. Returns false if the list is empty or length is not more than given position
func (*Singly[T]) Display ¶
func (ll *Singly[T]) Display()
Display prints out the elements of the list.
func (*Singly[T]) ReversePartition ¶
ReversePartition Reverse the linked list from the ath to the bth node