Example #1
0
func getLevelbasedList(root *binarytree.Tree, level int) []*list.List {
	if root == nil {
		return nil
	}
	var nodeList []*list.List
	parents := list.New()
	current := list.New()

	current.PushFront(root)

	for current.Len() > 0 {
		nodeList = append(nodeList, current)
		parents = current
		current = list.New()

		for x := parents.Front(); x != nil; x = x.Next() {
			node := x.Value.(*binarytree.Tree)
			if node.Left != nil {
				current.PushFront(node.Left)
			}
			if node.Right != nil {
				current.PushFront(node.Right)
			}
		}
	}
	return nodeList

}
Example #2
0
//Here *[]*list.List used in the function argument to input pass by referrence slice
func getLevelbasedList(nodeList *[]*list.List, t *binarytree.Tree, level int) {
	if t == nil {
		return
	}
	l := list.New()
	if len(*nodeList) == level {
		l = list.New()
		l.PushFront(t.Value)
		*nodeList = append(*nodeList, l)

	} else {
		l = (*nodeList)[level] //index operator doesn't automatically dereference pointers. we need to use parentheses to specify what is dereferenced.
		l.PushFront(t.Value)
	}
	getLevelbasedList(nodeList, t.Left, level+1)
	getLevelbasedList(nodeList, t.Right, level+1)
}
Example #3
0
func main() {
	l := list.New()
	l.PushFront(4)
	l.PushFront(5)
	l.PushFront(7)
	l.PushFront(9)
	m := list.New()
	m.PushFront(3)
	m.PushFront(2)
	m.PushFront(8)
	//m.PushFront(6)
	res := addLists(l, m)
	for e := res.Front(); e != nil; e = e.Next() {
		fmt.Print(e.Value)
	}
	fmt.Println(" ")
}
Example #4
0
//Reverse list function
func reverseList(l *list.List) *list.List {
	m := list.New()

	for e := l.Front(); e != nil; e = e.Next() {
		m.PushFront(e.Value.(int))
	}
	return m
}
Example #5
0
func main() {
	l := list.New()
	for i := 1; i < 100; i++ {
		l.PushBack(i)
	}
	kFromLastElem := findKFromLast(l, 3)
	fmt.Println("Iterative Method : ", kFromLastElem.Value)
	kFromLastElemRec := findKFromLastRecr(l.Front(), 3, &WrapObj{0})
	fmt.Println("Recursive Method : ", kFromLastElemRec.Value.(int))
}
Example #6
0
func main() {

	l := list.New()
	l.PushFront(4)
	l.PushFront(5)
	l.PushFront(4)
	l.PushFront(5)
	l.PushFront(4)

	m := list.New()
	m.PushFront(3)
	m.PushFront(2)
	m.PushFront(8)
	m.PushFront(6)
	res := isPalindrome(l)
	fmt.Println(res)
	res = isPalindromeUsingStack(l)
	fmt.Println(res)
}
Example #7
0
//Function to split the list around the value x
func splitAroundX(l *list.List, x int) *list.List {
	if l == nil {
		return nil
	}
	//Two lists for less than x and greater than X
	lThanX := list.New()
	gThanX := list.New()

	for e := l.Front(); e != nil; e = e.Next() {
		m := e.Value.(int)
		//Compare the value of x with the current node value and append to the respective list
		if m < x {
			lThanX.PushBack(m)
		} else {
			gThanX.PushBack(m)
		}
	}
	lThanX.PushBackList(gThanX)
	return lThanX
}
Example #8
0
func main() {
	l := list.New()
	l.PushFront(4)
	l.PushFront(5)
	l.PushFront(7)
	l.PushFront(6)
	l.PushBack(9)
	res := splitAroundX(l, 6)
	for e := res.Front(); e != nil; e = e.Next() {
		fmt.Println(e.Value)
	}
}
Example #9
0
func main() {
	m := list.New()
	m.PushFront(4)
	m.PushFront(5)
	e4 := m.PushFront(7)
	m.PushFront(6)
	m.PushBack(9)

	//removeInPlace function is the tweak in the original list. newly appened function in the list package "go/chapter02-linkedlists/list"

	m.RemoveInPlace(e4)
	fmt.Println("In Place ")
	for f := m.Front(); f != nil; f = f.Next() {
		fmt.Println(f.Value.(int))
	}
}
Example #10
0
func main() {
	l := list.New()
	l.PushFront(4)
	l.PushFront(5)
	l.PushFront(7)
	l.PushFront(6)
	l.PushFront(5)
	l.PushFront(4)
	l.PushFront(5)
	l.PushFront(7)
	l.PushBack(9)
	l = removeDuplicate(l)
	//To print the result list without duplicates
	for e := l.Front(); e != nil; e = e.Next() {
		fmt.Println(e.Value)
	}
}
Example #11
0
func main() {
	l := list.New()
	l.PushBack(0)
	l.PushBack(1)
	l.PushBack(2)
	l.PushBack(3)
	l.PushBack(4)
	l.PushBack(5)

	e6 := l.PushBack(6)
	l.PushBack(7)
	e8 := l.PushBack(8)
	e9 := l.InsertAfter(9, e8)
	l.InsertBefore(e9, e6)

	for e := l.Front(); e != nil; e = e.Next() {
		fmt.Println(e.Value)
	}
}
Example #12
0
//Function to add the list
func addLists(l *list.List, m *list.List) *list.List {
	if l == nil && m == nil {
		return nil
	}
	lLength := l.Len()
	mLength := m.Len()

	carry := 0
	value := 0
	resList := list.New()

	var e *list.Element
	var f *list.Element
	for e, f = l.Front(), m.Front(); e != nil && f != nil; e, f = e.Next(), f.Next() {
		value = carry + e.Value.(int) + f.Value.(int)
		//get the carry and value
		carry = 0
		carry = value / 10
		value = value % 10
		resList.PushFront(value)
	}
	//To identify the long list if the size is different
	var p *list.Element
	if lLength > mLength {
		p = e
	} else {
		p = f
	}
	for ; p != nil; p = p.Next() {
		value = carry + p.Value.(int)
		carry = 0
		carry = value / 10
		value = value % 10
		resList.PushFront(value)
	}
	if carry != 0 {
		resList.PushFront(carry)
	}
	return resList
}