-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
86 changed files
with
2,976 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
public class CDLinkedList { | ||
DListNode header; | ||
int size; | ||
static final int HEADERVALUE = -9999999; | ||
|
||
public CDLinkedList() { | ||
header = new DListNode(HEADERVALUE); | ||
makeEmpty();// necessary, otherwise next/previous node will be null | ||
} | ||
|
||
public boolean isEmpty() { | ||
return header.nextNode == header; | ||
} | ||
|
||
public boolean isFull() { | ||
return false; | ||
} | ||
|
||
/** make the list empty. */ | ||
public void makeEmpty() { | ||
header.nextNode = header; | ||
header.previousNode = header; | ||
size =0; | ||
} | ||
|
||
// put in new data after the position of p. | ||
public void insert(int value, Iterator p) throws Exception { | ||
if (p == null || !(p instanceof DListIterator)) | ||
throw new Exception(); | ||
DListIterator p2 = (DListIterator) p; | ||
if (p2.currentNode == null) | ||
throw new Exception(); | ||
|
||
DListIterator p3 = new DListIterator(p2.currentNode.nextNode); | ||
DListNode n = new DListNode(value, p3.currentNode, p2.currentNode); | ||
p2.currentNode.nextNode = n; | ||
p3.currentNode.previousNode = n; | ||
size++; | ||
} | ||
|
||
// return position number of value found in the list. | ||
// otherwise, return -1. | ||
public int find(int value) throws Exception { | ||
DListIterator itr = new DListIterator(header); | ||
int index = -1; | ||
while (itr.hasNext()) { | ||
int v = itr.next(); | ||
index++; | ||
if (itr.currentNode == header) | ||
return -1; | ||
if (v == value) | ||
return index; // return the position of value. | ||
} | ||
return -1; | ||
} | ||
|
||
// return data stored at kth position. | ||
public int findKth(int kthPosition) throws Exception { | ||
if (kthPosition < 0 || kthPosition > size-1) | ||
throw new Exception();// exit the method if the position is | ||
// beyond the first/last possible | ||
// position, throwing exception in the process. | ||
DListIterator itr = new DListIterator(header); | ||
int index = -1; | ||
while (itr.hasNext()) { | ||
int v = itr.next(); | ||
index++; | ||
if (itr.currentNode == header) | ||
throw new Exception(); | ||
if (index == kthPosition) | ||
return v; | ||
} | ||
throw new Exception(); | ||
} | ||
|
||
// Return iterator at position before the first position that stores value. | ||
// If the value is not found, return null. | ||
public Iterator findPrevious(int value) throws Exception { | ||
if (isEmpty()) | ||
return null; | ||
Iterator itr1 = new DListIterator(header); | ||
Iterator itr2 = new DListIterator(header); | ||
int currentData = itr2.next(); | ||
while (currentData != value) { | ||
currentData = itr2.next(); | ||
itr1.next(); | ||
if (((DListIterator) itr2).currentNode == header) | ||
return null; | ||
} | ||
return itr1; | ||
} | ||
|
||
// remove content at position just after the given iterator. Skip header if | ||
// found. | ||
public void remove(Iterator p) { | ||
if (isEmpty()) | ||
return; | ||
if (p == null || !(p instanceof DListIterator)) | ||
return; | ||
DListIterator p2 = (DListIterator) p; | ||
if (p2.currentNode == null) | ||
return; | ||
if (p2.currentNode.nextNode == header) | ||
p2.currentNode = header; | ||
if (p2.currentNode.nextNode == null) | ||
return; | ||
DListIterator p3 = new DListIterator(p2.currentNode.nextNode.nextNode); | ||
p2.currentNode.nextNode = p3.currentNode; | ||
p3.currentNode.previousNode = p2.currentNode; | ||
size--; | ||
} | ||
|
||
// remove the first instance of the given data. | ||
public void remove(int value) throws Exception { | ||
Iterator p = findPrevious(value); | ||
if (p == null) | ||
return; | ||
remove(p); | ||
} | ||
|
||
// remove data at position p. | ||
// if p points to header or the list is empty, do nothing. | ||
public void removeAt(Iterator p) throws Exception { | ||
if (isEmpty() || p == null || !(p instanceof DListIterator) || ((DListIterator) p).currentNode == null | ||
|| ((DListIterator) p).currentNode == header) | ||
return; | ||
|
||
DListIterator p2 = (DListIterator) (findPrevious(p)); | ||
remove(p2); | ||
|
||
} | ||
|
||
// Print each contact out, one by one. | ||
// To be completed by students. | ||
public void printList() throws Exception { | ||
Iterator itr = new DListIterator(header); | ||
while (itr.hasNext()) { | ||
Object data = itr.next(); | ||
|
||
System.out.println(data); | ||
|
||
} | ||
} | ||
|
||
public int size() throws Exception { | ||
return size; | ||
} | ||
|
||
// return iterator pointing to location before position. | ||
public Iterator findPrevious(Iterator position) throws Exception { | ||
if (position == null) | ||
return null; | ||
if (!(position instanceof DListIterator)) | ||
return null; | ||
if (((DListIterator) position).currentNode == null) | ||
return null; | ||
|
||
DListIterator p = ((DListIterator) position); | ||
DListIterator p2 = new DListIterator(p.currentNode.previousNode); | ||
return p2; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class CDLinkedListTest { | ||
CDLinkedList l1 = new CDLinkedList(); | ||
DListIterator[] itrs = new DListIterator[4]; | ||
DListIterator itr = new DListIterator(l1.header); | ||
int[] a = new int[12]; | ||
|
||
void setIterators(int p1, int p2, int p3, int p4) throws Exception { | ||
for (int i = 1; i <= 12; i++) { | ||
itr.next(); | ||
if (i == p1) { | ||
itrs[0] = new DListIterator(itr.currentNode); | ||
} | ||
if (i == p2) { | ||
itrs[1] = new DListIterator(itr.currentNode); | ||
} | ||
if (i == p3) { | ||
itrs[2] = new DListIterator(itr.currentNode); | ||
} | ||
if (i == p4) { | ||
itrs[3] = new DListIterator(itr.currentNode); | ||
} | ||
|
||
} | ||
itr = new DListIterator(l1.header); | ||
} | ||
|
||
void storeInArray() throws Exception { | ||
for (int i = 1; i <= 12; i++) { | ||
int v = itr.next(); | ||
a[i-1] = v; | ||
} | ||
itr = new DListIterator(l1.header); | ||
} | ||
|
||
@BeforeEach | ||
void setUp() throws Exception { | ||
l1 = new CDLinkedList(); | ||
itrs = new DListIterator[4]; | ||
itr = new DListIterator(l1.header); | ||
for (int i = 1; i <= 12; i++) { | ||
l1.insert(i, itr); | ||
itr.next(); | ||
} | ||
itr = new DListIterator(l1.header); | ||
} | ||
|
||
@Test | ||
void testSwapRangeNull() throws Exception { | ||
l1.swapRange(null, null, null, null); | ||
DListIterator itr = new DListIterator(l1.header); | ||
for (int i = 1; i <= 12; i++) { | ||
int v = itr.next(); | ||
assertEquals(i, v); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
void testSwapRangeNotDListIterator() throws Exception{ | ||
setIterators(2,4,8,11); | ||
Iterator it = new Iterator() { | ||
|
||
@Override | ||
public void set(int value) { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public int previous() throws Exception { | ||
// TODO Auto-generated method stub | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int next() throws Exception { | ||
// TODO Auto-generated method stub | ||
return 0; | ||
} | ||
|
||
@Override | ||
public boolean hasPrevious() { | ||
// TODO Auto-generated method stub | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
// TODO Auto-generated method stub | ||
return false; | ||
} | ||
}; | ||
|
||
l1.swapRange(itrs[0], itrs[1], it, itrs[3]); | ||
DListIterator itr = new DListIterator(l1.header); | ||
for (int i = 1; i <= 12; i++) { | ||
int v = itr.next(); | ||
assertEquals(i, v); | ||
} | ||
} | ||
|
||
@Test | ||
void testSwapRangeGeneric() throws Exception { | ||
setIterators(2,4,8,11); | ||
l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]); | ||
itr = new DListIterator(l1.header); | ||
|
||
storeInArray(); | ||
assertEquals(1,a[0]); | ||
assertEquals(8,a[1]); | ||
assertEquals(9,a[2]); | ||
assertEquals(10,a[3]); | ||
assertEquals(11,a[4]); | ||
assertEquals(5,a[5]); | ||
assertEquals(6,a[6]); | ||
assertEquals(7,a[7]); | ||
assertEquals(2,a[8]); | ||
assertEquals(3,a[9]); | ||
assertEquals(4,a[10]); | ||
assertEquals(12,a[11]); | ||
|
||
} | ||
|
||
@Test | ||
void testSwapRangeOneDataBoth() throws Exception { | ||
setIterators(2,2,8,8); | ||
l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]); | ||
|
||
storeInArray(); | ||
assertEquals(1,a[0]); | ||
assertEquals(8,a[1]); | ||
assertEquals(3,a[2]); | ||
assertEquals(4,a[3]); | ||
assertEquals(5,a[4]); | ||
assertEquals(6,a[5]); | ||
assertEquals(7,a[6]); | ||
assertEquals(2,a[7]); | ||
assertEquals(9,a[8]); | ||
assertEquals(10,a[9]); | ||
assertEquals(11,a[10]); | ||
assertEquals(12,a[11]); | ||
|
||
} | ||
|
||
@Test | ||
void testSwapRangeOneData1stRange() throws Exception { | ||
setIterators(3,3,6,9); | ||
l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]); | ||
|
||
storeInArray(); | ||
assertEquals(1,a[0]); | ||
assertEquals(2,a[1]); | ||
assertEquals(6,a[2]); | ||
assertEquals(7,a[3]); | ||
assertEquals(8,a[4]); | ||
assertEquals(9,a[5]); | ||
assertEquals(4,a[6]); | ||
assertEquals(5,a[7]); | ||
assertEquals(3,a[8]); | ||
assertEquals(10,a[9]); | ||
assertEquals(11,a[10]); | ||
assertEquals(12,a[11]); | ||
} | ||
|
||
@Test | ||
void testSwapRangeOneData2NdRange() throws Exception { | ||
setIterators(2,6,10,10); | ||
l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]); | ||
|
||
storeInArray(); | ||
assertEquals(1,a[0]); | ||
assertEquals(10,a[1]); | ||
assertEquals(7,a[2]); | ||
assertEquals(8,a[3]); | ||
assertEquals(9,a[4]); | ||
assertEquals(2,a[5]); | ||
assertEquals(3,a[6]); | ||
assertEquals(4,a[7]); | ||
assertEquals(5,a[8]); | ||
assertEquals(6,a[9]); | ||
assertEquals(11,a[10]); | ||
assertEquals(12,a[11]); | ||
} | ||
|
||
@Test | ||
void testSwapRangeAdjacent() throws Exception { | ||
setIterators(1,4,5,7); | ||
l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]); | ||
|
||
storeInArray(); | ||
assertEquals(5,a[0]); | ||
assertEquals(6,a[1]); | ||
assertEquals(7,a[2]); | ||
assertEquals(1,a[3]); | ||
assertEquals(2,a[4]); | ||
assertEquals(3,a[5]); | ||
assertEquals(4,a[6]); | ||
assertEquals(8,a[7]); | ||
assertEquals(9,a[8]); | ||
assertEquals(10,a[9]); | ||
assertEquals(11,a[10]); | ||
assertEquals(12,a[11]); | ||
} | ||
|
||
@Test | ||
void testSwapRangeEndList() throws Exception { | ||
setIterators(4,7,9,12); | ||
l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]); | ||
|
||
storeInArray(); | ||
assertEquals(1,a[0]); | ||
assertEquals(2,a[1]); | ||
assertEquals(3,a[2]); | ||
assertEquals(9,a[3]); | ||
assertEquals(10,a[4]); | ||
assertEquals(11,a[5]); | ||
assertEquals(12,a[6]); | ||
assertEquals(8,a[7]); | ||
assertEquals(4,a[8]); | ||
assertEquals(5,a[9]); | ||
assertEquals(6,a[10]); | ||
assertEquals(7,a[11]); | ||
|
||
|
||
} | ||
|
||
|
||
} |
Oops, something went wrong.