|
| 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 | +//package main // Uncomment this line and comment the above line to run as a main program |
| 9 | + |
| 10 | +import ("fmt" |
| 11 | + //"reflect" |
| 12 | +) |
| 13 | + |
| 14 | +type Element struct { |
| 15 | + // Element is each node |
| 16 | + next *Element |
| 17 | + ll *LinkedList // The linked list to which the element belongs |
| 18 | + Value interface{} |
| 19 | +} |
| 20 | + |
| 21 | +// Next returns the next list element or nil |
| 22 | +func (e *Element) Next() *Element { |
| 23 | + if p := e.next; e.ll != nil && p != &e.ll.root { |
| 24 | + return p |
| 25 | + } |
| 26 | + return nil |
| 27 | +} |
| 28 | + |
| 29 | +type LinkedList struct { |
| 30 | + root Element |
| 31 | + len int |
| 32 | + last *Element |
| 33 | +} |
| 34 | + |
| 35 | +//Clears the linkedlist and initializes the root element |
| 36 | +func (ll *LinkedList) Init() *LinkedList { |
| 37 | + ll.root.next = nil |
| 38 | + ll.len = 0 |
| 39 | + ll.last = nil |
| 40 | + return ll |
| 41 | +} |
| 42 | + |
| 43 | +//Creates a new LinkedList and Initializes it and returns |
| 44 | +func New() *LinkedList { |
| 45 | + return new(LinkedList).Init() |
| 46 | +} |
| 47 | + |
| 48 | +// Returns the length of the linked list |
| 49 | +func (ll *LinkedList) Len() int { |
| 50 | + return ll.len |
| 51 | +} |
| 52 | + |
| 53 | +func (ll *LinkedList) Front() *Element { |
| 54 | + if ll.len == 0 { |
| 55 | + return nil |
| 56 | + } |
| 57 | + return ll.root.next |
| 58 | +} |
| 59 | + |
| 60 | +func (ll *LinkedList) Back() *Element { |
| 61 | + if ll.len == 0 { |
| 62 | + return nil |
| 63 | + } |
| 64 | + return ll.last |
| 65 | +} |
| 66 | + |
| 67 | +func (ll *LinkedList) ModifyRoot(e *Element) *LinkedList { |
| 68 | + ll.root.next = e |
| 69 | + return ll |
| 70 | +} |
| 71 | + |
| 72 | +func (ll *LinkedList) PushBack(val interface{}) *Element { |
| 73 | + var e = new(Element) //e is pointer to a new Element |
| 74 | + if ll.root.next == nil { |
| 75 | + ll.root.next = e |
| 76 | + } else { |
| 77 | + ll.last.next = e |
| 78 | + } |
| 79 | + ll.len++ |
| 80 | + ll.last = e |
| 81 | + e.Value = val |
| 82 | + e.ll = ll |
| 83 | + return e |
| 84 | +} |
| 85 | + |
| 86 | +func (ll *LinkedList) PushFront(val interface{}) *Element { |
| 87 | + var e = new(Element) |
| 88 | + if ll.root.next == nil { |
| 89 | + ll.root.next = e |
| 90 | + } else { |
| 91 | + temp := ll.root.next |
| 92 | + ll.root.next = e |
| 93 | + e.next = temp |
| 94 | + } |
| 95 | + ll.len++ |
| 96 | + e.Value = val |
| 97 | + e.ll = ll |
| 98 | + return e |
| 99 | +} |
| 100 | + |
| 101 | +func (ll *LinkedList) Print() { |
| 102 | + temp := ll.Front() |
| 103 | + for temp.next != nil { |
| 104 | + fmt.Println(temp.Value) |
| 105 | + temp = temp.next |
| 106 | + } |
| 107 | + fmt.Println(temp.Value) |
| 108 | +} |
| 109 | + |
| 110 | +func (ll *LinkedList) ReversedRecursive(e *Element) *LinkedList { |
| 111 | + if ll.len == 0 || ll.len == 1 { |
| 112 | + return ll |
| 113 | + } |
| 114 | + temp := e |
| 115 | + if temp.next.next != nil { |
| 116 | + ll.ReversedRecursive(temp.next) |
| 117 | + } else { |
| 118 | + q := temp.next |
| 119 | + q.next = temp |
| 120 | + ll.ModifyRoot(q) |
| 121 | + temp.next = nil |
| 122 | + return ll |
| 123 | + } |
| 124 | + q := temp.next |
| 125 | + q.next = temp |
| 126 | + temp.next = nil |
| 127 | + return ll |
| 128 | +} |
| 129 | + |
| 130 | +func (ll *LinkedList) ReversedIterative() *LinkedList { |
| 131 | + if ll.len == 1 || ll.len == 0 { |
| 132 | + return ll |
| 133 | + } |
| 134 | + var prev *Element = nil |
| 135 | + var curr *Element = ll.root.next |
| 136 | + var next *Element = ll.root.next.next |
| 137 | + for next.next != nil { |
| 138 | + curr.next = prev |
| 139 | + prev = curr |
| 140 | + curr = next |
| 141 | + next = next.next |
| 142 | + } |
| 143 | + ll.ModifyRoot(next) |
| 144 | + next.next = curr |
| 145 | + curr.next = prev |
| 146 | + return ll |
| 147 | +} |
| 148 | + |
| 149 | +// Unit Test function |
| 150 | +func main() { |
| 151 | + |
| 152 | + ll := New() |
| 153 | + ll.PushBack(5) |
| 154 | + ll.PushBack(20) |
| 155 | + ll.PushBack(12) |
| 156 | + ll.PushBack(100) |
| 157 | + //ll.Print() |
| 158 | + //l := ll.Len() |
| 159 | + //fmt.Println(l) |
| 160 | + |
| 161 | + ll.PushFront(10) |
| 162 | + //fmt.Println(e) |
| 163 | + //fmt.Println(ll) |
| 164 | + //ll.Print() |
| 165 | + e := ll.Front() |
| 166 | + ll.ReversedRecursive(e) |
| 167 | + ll.Print() |
| 168 | + fmt.Println("--------") |
| 169 | + ll.ReversedIterative() |
| 170 | + ll.Print() |
| 171 | + //l = ll.Len() |
| 172 | + //fmt.Println(l) |
| 173 | +} |
| 174 | + |
0 commit comments