-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicLLClass.java
199 lines (175 loc) · 5.02 KB
/
BasicLLClass.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package p7_package;
public class BasicLLClass
{
private class BasicNode
{
BasicNode nextRef;
int value;
public BasicNode(int newVal)
{
value = newVal;
nextRef = null;
}
public BasicNode(BasicNode copied)
{
value = copied.value;
nextRef = null;
if(copied.nextRef !=null)
{
nextRef = new BasicNode(copied.nextRef);
}
}
}
static int FAILED_ACCESS =-9999999;
BasicNode headRef;
static int INSERT_AFTER =1003;
static int INSERT_BEFORE = 1002;
static int REMOVE = 1004;
static int REPLACE = 1001;
static int RETRIEVE = 1005;
public BasicLLClass()
{
headRef = null;
}
public BasicLLClass( BasicLLClass copied)
{
//copy constructor
headRef = new BasicNode(copied.headRef);
if(headRef!= null)
{
BasicNode currNode = headRef;
BasicNode copiedNode = headRef;
while(currNode != null)
{
currNode.nextRef = new BasicNode(copiedNode.nextRef);
currNode = currNode.nextRef;
copiedNode = copiedNode.nextRef;
}
}
}
private int accessAtIndex(int controlCode, int index)
{
if(index >= 0 && index < getCurrentSize())
{
if(controlCode == RETRIEVE)
{
return getRefAtIndex(this.headRef, index).value;
}
else if(index == 0)
{
this.headRef = headRef.nextRef;
}
BasicNode tempNode = getRefAtIndex(this.headRef, index);
getRefAtIndex(this.headRef, index - 1).nextRef = getRefAtIndex(
this.headRef, index + 1);
return tempNode.value;
}
return FAILED_ACCESS;
}
public void clear()
{
//Clears array of all valid values by setting array size to zero, values remain in array but are not accessible
headRef = null;
}
public int getAtIndex(int accessIndex)
{
//Accesses item in array at specified index if index within array size bounds
//Note: Calls accessAtIndex with RETRIEVE to conduct action
return accessAtIndex(RETRIEVE, accessIndex);
}
public int getCurrentSize()
{
return getCurrentSizeHelper(headRef);
}
private int getCurrentSizeHelper(BasicNode wkgRef)
{
//Note: size of array indicates number of valid or viable values in the array
//checks if reference is null and so ends the recursion
if(wkgRef == null)
{
return 0;
}
//adds
return 1 + getCurrentSizeHelper(wkgRef.nextRef);
}
private BasicNode getRefAtIndex(BasicLLClass.BasicNode wkgRef,
int index)
{
//Gets reference for pseudo index using recursive process
if(index !=0)
{
if(wkgRef.nextRef != null)
{
wkgRef = wkgRef.nextRef;
return getRefAtIndex(wkgRef, index-1);
}
}
return wkgRef;
}
public boolean isEmpty()
{
//Tests for size of array equal to zero, no valid values stored in array
if(headRef == null)
{
return true;
}
return false;
}
public int removeAtIndex(int removeIndex)
{
//Description: Removes item from array at specified index if index within array size bounds
//Note: Each data item from the element immediately above the remove index to the end of the array is moved down by one element
//Note: Each data item from the element immediately above the remove index to the end of the array is moved down by one element
if(removeIndex >=0 && removeIndex < getCurrentSize())
{
return accessAtIndex(REMOVE,removeIndex );
}
return FAILED_ACCESS;
}
public boolean setAtIndex(int setIndex, int newValue, int replaceFlag)
{
BasicNode tempNode;
if(setIndex == 0 && this.isEmpty()
&& (replaceFlag == INSERT_BEFORE || replaceFlag == INSERT_AFTER))
{
this.headRef = new BasicNode(newValue);
return true;
}
else if(setIndex >= 0 && setIndex < this.getCurrentSize())
{
if(replaceFlag == REPLACE)
{
tempNode = getRefAtIndex(this.headRef, setIndex);
getRefAtIndex(this.headRef, setIndex - 1).nextRef = new BasicNode(
newValue);
getRefAtIndex(this.headRef, setIndex).nextRef = tempNode.nextRef;
return true;
}
else if(replaceFlag == INSERT_BEFORE)
{
if(setIndex == 0)
{
tempNode = getRefAtIndex(this.headRef, setIndex);
this.headRef = new BasicNode(newValue);
this.headRef.nextRef = tempNode;
}
else
{
tempNode = getRefAtIndex(this.headRef, setIndex);
getRefAtIndex(this.headRef, setIndex - 1).nextRef = new BasicNode(
newValue);
getRefAtIndex(this.headRef, setIndex).nextRef = tempNode;
}
return true;
}
else if(replaceFlag == INSERT_AFTER)
{
tempNode = getRefAtIndex(this.headRef, setIndex + 1);
getRefAtIndex(this.headRef, setIndex).nextRef = new BasicNode(newValue);
getRefAtIndex(this.headRef, setIndex + 1).nextRef = tempNode;
return true;
}
}
return false;
}
}