그런데.. 결과값이 원하는대로 안나와서 처음부터 다시 한게 벌써 5번째입니다... 그런데 계속 같은 결과값이 나오네요..
public class SimpleLinkedList {
/**
* Link list containing all digits of student ID
* 117747148(Originally 017-747-148)
*/
public Node head; // Head is first node in linked list
public SimpleLinkedList() {
head = null;
}
public SimpleLinkedList(Node head) {
this.head = head;
}
public boolean isEmpty() {
return length() == 0;
}
public void insertAtEnd(String data) {
if (head == null) {
insertAtBeginning(data);
} else {
Node tmpNode = head;
Node newNode = new Node(data);
while (tmpNode != null) {
tmpNode = tmpNode.next;
}
head = newNode;
head.next = tmpNode;
}
}
public void insertAtBeginning(String data) {
if(head == null){
head = new Node(data);
}
else{
Node tmpNode = head;
while(tmpNode.next != null){
tmpNode = tmpNode.next;
}
tmpNode.next = new Node(data);
}
}
public Node tail() { // returns the last node
Node tmpNode = head;
while (tmpNode.next != null) {
tmpNode = tmpNode.next;
}
return tmpNode;
}
public int length() {
Node tmpNode = head;
int len = 0;
while (tmpNode.next != null) {
System.out.println(tmpNode);
len++;
}
return len;
}
void insertAtIndex(int idx, String data) {
Node targetNode = new Node(data);
if(idx == 0){
targetNode.next = head;
head = targetNode;
}
else{
Node tmpNode = head;
while(--idx > 0){
tmpNode = tmpNode.next;
}
targetNode.next = tmpNode.next;
tmpNode.next = targetNode;
}
}
Node findAtIndex(int idx) { // Done
Node tmpNode = head;
while(--idx >=0){
tmpNode = tmpNode.next;
}
return tmpNode;
}
void deleteAtIndex(int idx) {
if(idx == 0){
head = head.next;
}
else{
Node tmpNode = head;
while(--idx > 0){
tmpNode = tmpNode.next;
}
Node targetNode = tmpNode.next.next;
if(targetNode != null){
tmpNode.next = targetNode;
}
else{
tmpNode.next = null;
}
}
}
@Override
public String toString() { // returns the string 117747148
String result = "";
Node tmpNode = head;
while(tmpNode != null){
result += tmpNode.data;
tmpNode = tmpNode.next;
}
return result;
}
/*
* this methods reverse the list, for example 30891147 becomes 74119803
*/
Node reverse() {
Node tmpNode1 = head;
Node tmpNode2 = null;
while(tmpNode1 != null){
tmpNode2.next = tmpNode1;
tmpNode2.next.next = tmpNode2.next;
tmpNode1 = tmpNode1.next;
}
return tmpNode2;
}
/*
* the same as previous method but use the recursive style
*/
void reverseRecursive(Node p) {
insertAtEnd(p.data);
deleteAtIndex(0);
reverseRecursive(p.next);
}
void deleteData(String s) {
Node tmpNode = head;
while(tmpNode != null){
if(tmpNode.data == s){
tmpNode = null;
continue;
}
tmpNode = tmpNode.next;
}
}
@Override
public int hashCode() {
return findAtIndex(0).hashCode() * tail().hashCode();
}
@Override
public boolean equals(Object obj) {
Node tmpNode = head;
while(tmpNode != null){
if(tmpNode.data == obj){
return true;
}
}
return false;
}
// Node is nested class because it only exists along with linked list
public static class Node {
private String data;
private Node next;
public Node(String data) {
this.data = data;
}
}
public void buildListFromID(long num, SimpleLinkedList digits) {
if (num / 10 > 0) {
buildListFromID(num / 10, digits);
}
String s = String.valueOf(num % 10);
insertAtEnd(s);
}
public static void main(String[] args) {
SimpleLinkedList sll = new SimpleLinkedList();
long sid = 117747148L;// 30891147L;
sll.buildListFromID(sid, sll);
System.out.println("\n Okay");
System.out.println(sll); // *
for (int i = 1; i <= sll.length(); i++){
// **
System.out.println(i);
System.out.print(sll.findAtIndex(i));
}
// your program must print twice from * and **
// your student ID
// 30891147
// 30891147
}
}
1. buildListFromID 이 펑션이 set 역할을 하는건 알겠는데 왜 리커시브를 돌리는지 모르겠습니다
2. 그리고 구글링 해보니까 Node 클래스에