-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsll_ins_del_2nd_test.go
78 lines (68 loc) · 2.14 KB
/
sll_ins_del_2nd_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
#created_on : 27 Dec 2016,Tuesday(afternoon)
#coded_by : hygull
*/
package ds
import (
"fmt"
"testing"
)
func TestInsertAndDeleteNodeAtBeginningAndEnd(t *testing.T) {
/* CASE 1*/
var start2 *Node = nil
err := ShowNodes(start2)
if err != nil {
fmt.Println(err) //singly linked list is empty
}
/* CASE 2 */
var start *Node
fmt.Println("\n********* Testing (Inserting nodes at beginning & end of the linked list) **********\n")
node := CreateNode("Hygull", 24)
start = InsertSllNodeAtBeginning(start, node) //We can also use the global START variable
/*
+---------------+----+
start-->| {"Hygull",24} | nil|
+--------------------+
*/
node2 := CreateNode("Hemkesh", 22)
start = InsertSllNodeAtBeginning(start, node2)
/*
+---------------+----+ +----------------+----+
start===>| {"Hemkesh",22}| ======>| {"Hygull", 24} | nil|
+---------------+----+ +----------------+----+
*/
node3 := CreateNode("Darshan", 23)
start = InsertSllNodeAtEnd(start, node3)
/*
+---------------+----+ +----------------+----+ +----------------+----+
start===>| {"Hemkesh",22}| ======>| {"Hygull", 24} | =======>| {"Darshan",23} | nil|
+---------------+----+ +----------------+----+ +----------------+----+
*/
ShowNodes(start)
fmt.Println()
ShowNodesHierarchy(start)
fmt.Println()
recordsList := GetNodesRecords(start)
if len(recordsList) == 0 {
fmt.Println("Singly linked list is empty")
}
fmt.Println()
marshalledRecords, _ := Marshall(recordsList)
fmt.Println(marshalledRecords)
/* Deleting 1 node from the beginning of linked list */
start, _ = DeleteSllNodeFromBeginning(start)
fmt.Println("After deleting 1 node from beginning")
fmt.Println(GetNodesRecords(start))
/* Inserting 1 node at end */
node4 := CreateNode("Surendra", 24)
start = InsertSllNodeAtEnd(start, node4)
fmt.Println("After adding 1 node at end")
fmt.Println(GetNodesRecords(start))
/* Deleting 1 node from the end of linked list */
err = DeleteSllNodeFromEnd(start)
if err != nil { //List is empty
fmt.Println(err)
}
fmt.Println("After deleting 1 node from end")
fmt.Println(GetNodesRecords(start))
}