-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssessmentSLL.java
More file actions
152 lines (136 loc) · 3.45 KB
/
AssessmentSLL.java
File metadata and controls
152 lines (136 loc) · 3.45 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//Kyriacos Sophocleous 2471955s
//do not include package statement
//do not import any classes
//just complete the methods indicated,
//namely:
//insertTail
//deleteAlternate and
//merge
public class AssessmentSLL<E extends Comparable<E>> {
// Each SLL object is the header of
// a singly-linked-list
private Node<E> first;
/**
* constructor
*/
public AssessmentSLL() {
// Construct an empty SLL.
first = null;
}
// //////// Inner class //////////
private static class Node<E> {
// Each Node object is a node of a
// singly-linked-list.
protected E element;
protected Node<E> next;
public Node(E elem, Node<E> n) {
element = elem;
next = n;
}
}
// ////////////////////////////////
/**
* print all elements starting from first node
*/
public void printFirstToLast() {
// Print all elements in this SLL, in first-to-last order.
Node<E> curr = first;
while (curr != null) {
System.out.println(curr.element);
curr = curr.next;
}
}
/**
* insert new node containing elem at front of list
*
*/
public void insert(E elem) {
Node<E> newNode = new Node<E>(elem, first);
first = newNode;
}
/**
* add new node to end of list
*/
public void insertTail(E elem) {
Node <E> newNode=new Node<E>(elem,null);
if (first==null) {
first=newNode;
}else {
Node <E> last=first;
while (last.next != null){
last=last.next;
}
last.next=newNode;
}
}
/** delete every alternate element
* imagine the elements are indexed 0, 1, 2, ... , n-1
* delete all the odd indexed elements
* so if list had values ant, badger, cat, dog
* the method would delete nodes containing badger and dog
*/
public void deleteAlternate() {
if (first==null) {
return;
}
Node <E> pred=first;
Node <E> toDelete=first.next;
while((pred!=null)&&(toDelete!=null)) {
pred.next=toDelete.next;
toDelete=null;
pred=pred.next;
if (pred!=null) {
toDelete=pred.next;
}
}
}
/**
* method to merge two ordered lists with this one whilst removing duplicates
* order should be preserved
* you need to replace the type list1, list2 and list3 and the return type
* with the new name of this class
*/
public AssessmentSLL<E> merge(AssessmentSLL<E> list1, AssessmentSLL<E> list2){
AssessmentSLL<E> mergedList = new AssessmentSLL<E>();
Node<E> current1=list1.first;
Node<E> current2=list2.first;
Node<E> result=helpingMethod(current1,current2);
mergedList.first=result;
current1=mergedList.first;
current2=this.first;
result=helpingMethod(current1,current2);
result=removeDuplicates(result);
mergedList.first=result;
return mergedList;
}
public Node<E> helpingMethod(Node<E> current1,Node<E> current2){
if(current1==null) {
return current2;
}
if (current2==null) {
return current1;
}
Node<E> mergedList=null;
if (current1.element.compareTo(current2.element)<=0) {
current1.next=helpingMethod(current1.next,current2);
mergedList=current1;
}else {
current2.next=helpingMethod(current1,current2.next);
mergedList=current2;
}
return mergedList;
}
public Node<E> removeDuplicates(Node<E> current){
if (current == null)
return null;
if (current.next != null) {
if (current.element == current.next.element) {
current.next = current.next.next;
removeDuplicates(current);
}else {
removeDuplicates(current.next);
}
}
return current;
}
}