|
| 1 | +/* |
| 2 | +This is a linked list implementation. |
| 3 | +This implementation of linked list is not circular unlike |
| 4 | +the container/list package provided by default |
| 5 | +*/ |
| 6 | + |
| 7 | +package linkedList |
| 8 | + |
| 9 | +import ("fmt" |
| 10 | + //"reflect" |
| 11 | +) |
| 12 | + |
| 13 | +type Element struct { |
| 14 | + // Element is each node |
| 15 | + next *Element |
| 16 | + ll *linkedList // The linked list to which the element |
| 17 | + Value interface{} |
| 18 | +} |
| 19 | + |
| 20 | +// Next returns the next list element or nil |
| 21 | +func (e *Element) Next() *Element { |
| 22 | + if p := e.next; e.ll != nil && p != &e.ll.root { |
| 23 | + return p |
| 24 | + } |
| 25 | + return nil |
| 26 | +} |
| 27 | + |
| 28 | +type linkedList struct { |
| 29 | + root Element |
| 30 | + len int |
| 31 | + last *Element |
| 32 | +} |
| 33 | + |
| 34 | +//Clears the linkedlist and initializes the root element |
| 35 | +func (ll *linkedList) Init() *linkedList { |
| 36 | + ll.root.next = nil |
| 37 | + ll.len = 0 |
| 38 | + ll.last = nil |
| 39 | + return ll |
| 40 | +} |
| 41 | + |
| 42 | +//Creates a new linkedList and Initializes it and returns |
| 43 | +func New() *linkedList { |
| 44 | + return new(linkedList).Init() |
| 45 | +} |
| 46 | + |
| 47 | +// Returns the length of the linked list |
| 48 | +func (ll *linkedList) Len() int { |
| 49 | + return ll.len |
| 50 | +} |
| 51 | + |
| 52 | +func (ll *linkedList) Front() *Element { |
| 53 | + if ll.len == 0 { |
| 54 | + return nil |
| 55 | + } |
| 56 | + return ll.root.next |
| 57 | +} |
| 58 | + |
| 59 | +func (ll *linkedList) Back() *Element { |
| 60 | + if ll.len == 0 { |
| 61 | + return nil |
| 62 | + } |
| 63 | + return ll.last |
| 64 | +} |
| 65 | + |
| 66 | +func (ll *linkedList) PushBack(val interface{}) *Element { |
| 67 | + var e = new(Element) //e is pointer to a new Element |
| 68 | + if ll.root.next == nil { |
| 69 | + ll.root.next = e |
| 70 | + } else { |
| 71 | + ll.last.next = e |
| 72 | + } |
| 73 | + ll.len++ |
| 74 | + ll.last = e |
| 75 | + e.Value = val |
| 76 | + e.ll = ll |
| 77 | + return e |
| 78 | +} |
| 79 | + |
| 80 | +func (ll *linkedList) PushFront(val interface{}) *Element { |
| 81 | + var e = new(Element) |
| 82 | + if ll.root.next == nil { |
| 83 | + ll.root.next = e |
| 84 | + } else { |
| 85 | + temp := ll.root.next |
| 86 | + ll.root.next = e |
| 87 | + e.next = temp |
| 88 | + } |
| 89 | + ll.len++ |
| 90 | + e.Value = val |
| 91 | + e.ll = ll |
| 92 | + return e |
| 93 | +} |
| 94 | + |
| 95 | +func (ll *linkedList) Print() { |
| 96 | + temp := ll.Front() |
| 97 | + for temp.next != nil { |
| 98 | + fmt.Println(temp.Value) |
| 99 | + temp = temp.next |
| 100 | + } |
| 101 | + fmt.Println(temp.Value) |
| 102 | +} |
| 103 | + |
| 104 | +/* |
| 105 | +func (ll *linkedList) Insert() (ll_head *linkedLis { |
| 106 | + temp := ll_head |
| 107 | + for temp.next != nil { |
| 108 | + fmt.Println(temp.data) |
| 109 | + temp = temp.next |
| 110 | + } |
| 111 | + // Last element in the linked list |
| 112 | + fmt.Println(temp.data) |
| 113 | +} |
| 114 | +*/ |
| 115 | + |
| 116 | + |
| 117 | +// Unit Test function |
| 118 | +func main() { |
| 119 | + |
| 120 | + ll := New() |
| 121 | + ll.PushBack(5) |
| 122 | + ll.Print() |
| 123 | + l := ll.Len() |
| 124 | + fmt.Println(l) |
| 125 | + |
| 126 | + ll.PushFront(10) |
| 127 | + //fmt.Println(e) |
| 128 | + //fmt.Println(ll) |
| 129 | + ll.Print() |
| 130 | + l = ll.Len() |
| 131 | + fmt.Println(l) |
| 132 | +} |
| 133 | + |
0 commit comments