[test] added new test

master
Furkan Türkal 5 years ago
parent d16fb68aae
commit 3610263f99
No known key found for this signature in database
GPG Key ID: 3015FE56155820F2

@ -29,7 +29,7 @@ func New() *Node {
func (n *Node) Next() *Node { func (n *Node) Next() *Node {
return n.next return n.next
} }
//Returns the last node in list if exist, otherwise returns current //Returns the last node in list if exist, otherwise returns current
func (n *Node) Back() *Node { func (n *Node) Back() *Node {
current := n.next current := n.next
@ -56,15 +56,15 @@ func Push(head_ref **Node, new_data int) {
*head_ref = new_node *head_ref = new_node
} }
//Pull off the front node of the source and put it in dest //Pull off the front node of the source and put it in dest
/* MoveNode() function takes the node from the front of the /* MoveNode() function takes the node from the front of the
   source, and move it to the front of the dest.    source, and move it to the front of the dest.
   It is an error to call this with the source list empty.    It is an error to call this with the source list empty.
 
   Before calling MoveNode():    Before calling MoveNode():
   source == {1, 2, 3}    source == {1, 2, 3}
   dest == {1, 2, 3}    dest == {1, 2, 3}
 
   Affter calling MoveNode():    Affter calling MoveNode():
   source == {2, 3}    source == {2, 3}
   dest == {1, 1, 2, 3} */    dest == {1, 1, 2, 3} */
@ -83,48 +83,58 @@ func MoveNode(dest_ref **Node, source_ref **Node) {
} }
/* Takes two lists sorted in increasing order, and splices /* Takes two lists sorted in increasing order, and splices
   their nodes together to make one big sorted list which    their nodes together to make one big sorted list which
   is returned.  */    is returned.  */
func SortedMerge(a *Node, b *Node) *Node { func SortedMerge(a *Node, b *Node) *Node {
//A dummy first node to hang the result on //A dummy first node to hang the result on
dummy := New() dummy := New()
//Tail points to the last result node //Tail points to the last result node
tail := dummy tail := dummy
//So tail.next is the place to add new nodes to result //So tail.next is the place to add new nodes to result
dummy.next = nil dummy.next = nil
for { for {
if (a == nil){ if a == nil {
//If either list runs out, use the other list //If either list runs out, use the other list
tail.next = b tail.next = b
break break
} else if (b == nil){ } else if b == nil {
tail.next = a tail.next = a
break break
} }
if (a.data <= b.data){ if a.data <= b.data {
MoveNode(&(tail.next), &a) MoveNode(&(tail.next), &a)
} else{ } else {
MoveNode(&(tail.next), &b) MoveNode(&(tail.next), &b)
} }
tail = tail.next tail = tail.next
} }
return dummy.next return dummy.next
} }
//This function prints contents of linked list starting from the given node //This function prints contents of linked list starting from the given node
func printList(n *Node){ func printList(n *Node) {
for n != nil { for n != nil {
fmt.Println(n.data) fmt.Print(n.data)
fmt.Print(" ,")
n = n.next n = n.next
} }
} }
func GetDataList(n *Node) []int {
data := []int{}
for n != nil {
data = append(data, n.data)
n = n.next
}
return data
}
func main() { func main() {
//Start with the empty list //Start with the empty list
res := New() res := New()
@ -144,4 +154,4 @@ func main() {
fmt.Println("Merged LinkedList is:") fmt.Println("Merged LinkedList is:")
printList(res) printList(res)
} }

Loading…
Cancel
Save