From e112848ae9e65892ce65f96970c00143aae434df Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Tue, 10 Mar 2015 03:21:55 +0100 Subject: [PATCH] documentation on doubly linked list --- README.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/README.md b/README.md index 263a0ed..f87ed9c 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Implementation of various data structures in Go. - [TreeSet](#treeset) - [Lists](#lists) - [ArrayList](#arraylist) + - [DoublyLinkedList](#doublylinkedlist) - [Stacks](#stacks) - [LinkedListStack](#linkedliststack) - [ArrayStack](#arraystack) @@ -182,6 +183,44 @@ func main() { } ``` +#####DoublyLinkedList + +This structure implements the _List_ interface and is a linked data structure where each element points to the next and previous element in the list. + +Direct access method _Get(index)_ and _Remove()_ are of linear performance. _Append_ and _Prepend_ are of constant time performance. Checking with _Contains()_ is of quadratic complexity. + +```go +package main + +import ( + dll "github.com/emirpasic/gods/lists/doublylinkedlist" + "github.com/emirpasic/gods/utils" +) + +func main() { + list := dll.New() + list.Add("a") // ["a"] + list.Append("b") // ["a","b"] (same as Add()) + list.Prepend("c") // ["c","a","b"] + list.Sort(utils.StringComparator) // ["a","b","c"] + _, _ = list.Get(0) // "a",true + _, _ = list.Get(100) // nil,false + _ = list.Contains("a", "b", "c") // true + _ = list.Contains("a", "b", "c", "d") // false + list.Remove(2) // ["a","b"] + list.Remove(1) // ["a"] + list.Remove(0) // [] + list.Remove(0) // [] (ignored) + _ = list.Empty() // true + _ = list.Size() // 0 + list.Add("a") // ["a"] + list.Clear() // [] +} + + +``` + + ####Stacks The stack interface represents a last-in-first-out (LIFO) collection of objects. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to check whether the stack is empty and the size (number of elements).