-
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
1 parent
739c45e
commit 517be84
Showing
2 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
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,178 @@ | ||
public class MyDoublyLinkedList { | ||
private MyDoublyLinkedData head; | ||
private MyDoublyLinkedData tail; | ||
private MyDoublyLinkedData prevCurrent; | ||
private MyDoublyLinkedData nextCurrent; | ||
private MyDoublyLinkedData current; | ||
private int compareCount; | ||
|
||
MyDoublyLinkedList() { | ||
head = new MyDoublyLinkedData("*header*", ""); | ||
tail = head; | ||
current = head; | ||
tail.next = null; | ||
} | ||
|
||
public MyDoublyLinkedData getCurrent() { | ||
return current; | ||
} | ||
|
||
public boolean movePrev(){ | ||
if (current.prev != null) { | ||
nextCurrent = current; | ||
current = current.prev; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public void setCurrent(String str) { | ||
if ("head".equals(str)) { | ||
current = head; | ||
} else if ("tail".equals(str)) { | ||
current = tail; | ||
} | ||
} | ||
|
||
public boolean moveNext() { | ||
if (current.next != null) { | ||
prevCurrent = current; | ||
current = current.next; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public void resetCompareCount() { | ||
compareCount = 0; | ||
} | ||
|
||
public void setCompareCount(int count) { | ||
compareCount = count; | ||
} | ||
|
||
public int getCompareCount() { | ||
return compareCount; | ||
} | ||
|
||
// current の後ろに新規データを挿入 | ||
public void insertAfter(String key, String data) { | ||
MyDoublyLinkedData newData = new MyDoublyLinkedData(key, data); | ||
|
||
newData.prev = current; | ||
if(current.next == null){ | ||
newData.next = null; | ||
tail.next = newData; | ||
} | ||
else{ | ||
newData.next = current.next; | ||
current.next.prev = newData; | ||
current.next = newData; | ||
} | ||
} | ||
|
||
public void insertBefore(String key, String data) { | ||
MyDoublyLinkedData newData = new MyDoublyLinkedData(key, data); | ||
|
||
newData.next = current; | ||
if(current.prev == null){ | ||
newData.prev = null; | ||
head.prev = newData; | ||
} | ||
else{ | ||
newData.prev = current.prev; | ||
current.prev.next = newData; | ||
current.prev = newData; | ||
} | ||
|
||
} | ||
|
||
public void printInfo() { | ||
MyDoublyLinkedData data = head; | ||
while (data != null) { | ||
System.out.println(data.toStringNodeData()); | ||
data = data.next; | ||
} | ||
} | ||
|
||
// 連結リストの末尾に新規データを挿入 | ||
public void insertLast(String key, String data) { | ||
MyDoublyLinkedData newData = new MyDoublyLinkedData(key, data); | ||
newData.next = null; | ||
tail.next = newData; | ||
prevCurrent = tail; | ||
|
||
tail = newData; | ||
current = newData; | ||
} | ||
|
||
public MyDoublyLinkedData searchByKey(String key) { | ||
MyDoublyLinkedData p = head; | ||
while (p != null) { | ||
if (strcmp(key, p.getKey()) == 0) { | ||
current = p; | ||
return p; | ||
} | ||
prevCurrent = p; | ||
p = p.next; | ||
} | ||
prevCurrent = null; | ||
current = head; | ||
return null; | ||
} | ||
|
||
public MyDoublyLinkedData searchByData(String data) { | ||
MyDoublyLinkedData p = head; | ||
while (p != null) { | ||
if (strcmp(data, p.getData()) == 0) { | ||
current = p; | ||
return p; | ||
} | ||
prevCurrent = p; | ||
p = p.next; | ||
} | ||
prevCurrent = null; | ||
current = head; | ||
return null; | ||
} | ||
|
||
public MyDoublyLinkedData delete() { | ||
if (current != null) { | ||
if (current.getKey().equals("*header*")) { | ||
return null; | ||
} | ||
MyDoublyLinkedData prevData = prevCurrent; | ||
prevData.next = current.next; | ||
MyDoublyLinkedData oldCurrent = current; | ||
current = current.next; | ||
if (current == null) { | ||
tail = prevCurrent; | ||
prevCurrent = null; | ||
current = head; | ||
} | ||
return oldCurrent; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
public void printData() { | ||
MyDoublyLinkedData p = head; | ||
while (p != null) { | ||
if (p == current){ | ||
System.out.print("*cur* "); | ||
}else{ | ||
System.out.print(" "); | ||
} | ||
System.out.println(p.toStringSimple()); | ||
p = p.next; | ||
} | ||
} | ||
|
||
public int strcmp(String key1, String key2) { | ||
compareCount++; | ||
return key1.compareTo(key2); | ||
} | ||
} |