자바고수님 혹시 있으신가요

jwl0302 작성일 19.05.19 03:33:16
댓글 6조회 1,210추천 2
155820439539146.png
public class LinkedListWithIteratorDemo2 {

public static void main(String[] args) {
String BadString = "Thrie";
String FiveString = "Five";
String SixString = "Six";

StringLinkedListWithIterator list = new StringLinkedListWithIterator();
list.addANodeToStart("One");
list.addANodeToStart("Two");
list.addANodeToStart("Thrie");
list.addANodeToStart("Four");
list.resetIteration();
try {
while (list.length() >= 0) {
if (BadString.equals(list.getDataAtCurrent()))
list.deleteCurrentNode();
else
list.goToNext();
}
} catch (LinkedListException e) {
if (e.getMessage().equals("Iterating with an empty list.")) {
System.out.println("Fatal error.");
System.exit(0);
}
}
// 추가된 메소드 public void addANodeToEnd(Sting addString)
list.addANodeToEnd(FiveString);
list.addANodeToEnd(SixString);
System.out.println("Start of list: ");
list.showList();
System.out.println("End of list.");
}
}
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

위에 보이는 addANodeToEnd를 구현해야합니다

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ


public class StringLinkedListWithIterator {

private ListNode head;
private ListNode current;
private ListNode previous;

public StringLinkedListWithIterator() {
head = null;
current = null;
previous = null;
}

public int length() {
int count = 0;
ListNode = head;
while (!= null) {
count++;
= }

return count;
}

public void addANodeToStart(String addData) {
head = new ListNode(addData, head);
if (current == head.link && current != null)
previous = head;
}

public void addANodeToEnd(String addData) {
resetIteration();
try {
while (moreToIterate())
goToNext();
if (head == null)
addANodeToStart(addData);
else {

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

구현할 부분입니다.

공부시작한지 두달이 넘었는데 수업문제도 아직은 어렵네요..

여기저기 찾아보고 구현해봐도 에러가 떠서 질문을 올려봅니다.




ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ


}
} catch (LinkedListException e) {
System.out.println(e.getMessage());
System.exit(0);
}
}

public boolean onList(String target) {
return (find(target) != null);
}

private ListNode find(String target) {
ListNode = head;
String dataAt while (!= null) {
dataAt= if (dataAt return = }

return null;
}

public void showList() {
ListNode = head;
while (!= null) {
System.out.println( = }
}

public String[] arrayCopy() {
String[] a = new String[length()];

ListNode = head;
int i = 0;
while (!= null) {
a[i] = i++;
= }

return a;
}

public void resetIteration() {
current = head;
previous = null;
}

public void goToNext()
throws LinkedListException {
if (current != null) {
previous = current;
current = current.link;
} else if (head != null) {
throw new LinkedListException(
"Iterated too many times or uninitialized iteration.");
} else {
throw new LinkedListException("Iterating with an empty list.");
}
}

public boolean moreToIterate() {
return (current != null);
}

public String getDataAtCurrent()
throws LinkedListException {
if (current != null)
return (current.data);
else
throw new LinkedListException(
"Getting data when current is not at any node.");
}

public void resetDataAtCurrent(String newData)
throws LinkedListException {
if (current != null)
current.data = newData;
else
throw new LinkedListException(
"Setting data when current is not an any node.");
}

public void insertNodeAfterCurrent(String newData)
throws LinkedListException {
ListNode newNode = new ListNode();
newNode.data = newData;
if (current != null) {
newNode.link = current.link;
current.link = newNode;
} else if (head != null) {
throw new LinkedListException(
"Inserting when iterator is past all "
+ "nodes or uninitialized iterator.");
} else {
throw new LinkedListException(
"Using insertNodeAfterCurrent with empty list.");
}
}

public void deleteCurrentNode()
throws LinkedListException {
if ((current != null) && (previous != null)) {
previous.link = current.link;
current = current.link;
} else if ((current != null) && (previous == null)) {
head = current.link;
current = head;
} else {
throw new LinkedListException(
"Deleting with uninitialized current or an empty list.");
}
}

private class ListNode {

private String data;
private ListNode link;

public ListNode() {
link = null;
data = null;
}

public ListNode(String newData, ListNode linkValue) {
data = newData;
link = linkValue;
}
}
}
jwl0302의 최근 게시물

자유·수다 인기 게시글