-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLinkedListTest.java
85 lines (79 loc) · 2.08 KB
/
LinkedListTest.java
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
79
80
81
82
83
84
85
package com.ctc.list;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class LinkedListTest {
@Test
public void removeDuplicateTest() {
LinkedList a = new LinkedList();
a.appendToList(3);
a.appendToList(3);
a.appendToList(56);
a.appendToList(1);
a.appendToList(3);
a.appendToList(37);
a.appendToList(34);
// "3 3 56 1 3 "-----sequence added and duplicates removed
RemoveDuplicates r = new RemoveDuplicates();
LinkedList c = r.remove(a);
String result1 = c.printList();
assertEquals(result1, "3 56 1 37 34 ");
}
@Test
public void kthElementToLastTest() {
LinkedList a = new LinkedList();
a.appendToList(3);
a.appendToList(3);
a.appendToList(56);
a.appendToList(1);
a.appendToList(3);
a.appendToList(37);
a.appendToList(34);
KthElementFromLast obj = new KthElementFromLast();
int result = obj.getKthElementFromElement(a, 2);
// System.out.println(result);
assertEquals(result, 37);
}
@Test
public void deleteNodeTest() {
LinkedList a = new LinkedList();
a.appendToList(3);
a.appendToList(56);
a.appendToList(1);
a.appendToList(37);
a.appendToList(34);
DeleteNode obj = new DeleteNode();
Node temp = a.getHead();
Node nodeToBeDeleted = null;
while (temp != null) {
if (temp.data == 1) {
nodeToBeDeleted = temp;
break;
}
temp = temp.next;
}
boolean answer = obj.deleteNode(nodeToBeDeleted);
// String result = a.printList();
// System.out.println(result);
assertEquals(answer, true);
}
@Test
public void partitionListTest() {
LinkedList a = new LinkedList();
a.appendToList(3);
a.appendToList(10);
a.appendToList(56);
a.appendToList(15);
a.appendToList(8);
a.appendToList(89);
a.appendToList(1);
a.appendToList(37);
a.appendToList(34);
PartitionList obj = new PartitionList();
Node head = obj.partition(15,a);
//System.out.println(head);
a.setHead(head);
String result = a.printList();
//System.out.println(result);
assertEquals(result,"3 10 8 1 56 15 89 37 34 ");
}
}