Skip to content

Commit 60cfc43

Browse files
committed
Created a linkedList package
1 parent bd2cf9b commit 60cfc43

File tree

4 files changed

+172
-53
lines changed

4 files changed

+172
-53
lines changed

linkedList/doublyLinkedList.go

Lines changed: 0 additions & 3 deletions
This file was deleted.

linkedList/linkedList.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

linkedList/linkedList/linkedList.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+

linkedList/reverseLinkedList.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Iterative and Recursive implementation of
3+
reversing a linked list
4+
*/
5+
6+
package main
7+
8+
import ("fmt"
9+
"AlgorithmsGolang/linkedList/linkedList"
10+
)
11+
12+
//Pass the root element to this function and by the end of it the linkedlist would have been reversed
13+
func (ll *linkedList.linkedList) reverseLinkedListRecursive(e *linkedlist.Element) {
14+
15+
}
16+
17+
//Pass the root element to this function
18+
func (ll *linkedList.linkedList ) reverseLinkedListIterartive(e *linkedList.Element) {
19+
20+
}
21+
22+
// Test
23+
func main() {
24+
ll := linkedList.New()
25+
ll.PushBack(10)
26+
ll.PushBack(20)
27+
ll.PushBack(5)
28+
ll.PushBack(25)
29+
ll.PushBack(100)
30+
31+
// The solution printed should be 100,25,5,20,10
32+
33+
root := ll.Front()
34+
ll.reverselinkedListRecursive(e) //Should have reversed the linked list
35+
ll.reverselinkedListIterative(e) //Should have re reversed and show the original linkedList
36+
37+
rllR.Print()
38+
rllI.Print()
39+
}

0 commit comments

Comments
 (0)