Last active
January 25, 2021 16:43
-
-
Save gowoonsori/e27cfc11b677f7936b6f55aac1500420 to your computer and use it in GitHub Desktop.
LinkedList ( Java로 ListNode 클래스로 구현하기)
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
package javaStudy; | |
public interface LinkedList { | |
void add(ListNode listNode,int position); | |
void remove(int position); | |
boolean contains(Integer key); | |
} |
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
package javaStudy.ListNode; | |
public class ListNode implements LinkedList { | |
private Integer key; | |
private ListNode next; | |
public void setKey(Integer key) { | |
this.key = key; | |
} | |
public void setNext(ListNode next) { | |
this.next = next; | |
} | |
public Integer getKey() { | |
return key; | |
} | |
public ListNode getNext() { | |
return next; | |
} | |
public ListNode() {} | |
public ListNode(Integer key) {this.key = key;} | |
@Override | |
public void add(ListNode listNode, int position) { | |
ListNode node = this; | |
if (node == null) { | |
node.setNext(listNode); | |
} else if (position == 1) { | |
listNode.setNext(node.getNext()); | |
node.setNext(listNode); | |
} else { | |
for (int i = 0; i < position - 1; i++) { | |
if (node.next != null) { | |
node = node.next; | |
} else return; | |
} | |
listNode.setNext(node.next); | |
node.setNext(listNode); | |
} | |
} | |
@Override | |
public ListNode remove(int position) { | |
ListNode node = this; | |
if (node == null) { | |
return null; | |
} | |
if (position == 1) { | |
ListNode d = node.getNext(); | |
node.setNext(d.getNext()); | |
d.setNext(null); | |
node = d; | |
} else { | |
ListNode p = node; | |
for (int i = 0; i < position; i++) { | |
if (node != null) { | |
p = node; | |
node = node.getNext(); | |
} | |
else return null; | |
} | |
p.setNext(node.getNext()); | |
node.setNext(null); | |
} | |
return node; | |
} | |
@Override | |
public boolean contains(Integer key) { | |
ListNode node = this; | |
while(node.getNext()!= null){ | |
if(node.getKey() == key) return true; | |
node = node.getNext(); | |
} | |
return false; | |
} | |
} |
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
package test.ListNode; | |
import javaStudy.ListNode.ListNode; | |
import org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.api.Test; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
import static org.junit.jupiter.api.Assertions.assertNull; | |
class ListNodeTest { | |
private ListNode listNode; | |
@BeforeEach | |
void init(){ | |
this.listNode = new ListNode(); | |
ListNode q = new ListNode(1); | |
ListNode i = new ListNode(2); | |
ListNode j = new ListNode(3); | |
ListNode k = new ListNode(4); | |
this.listNode.setNext(q); | |
q.setNext(i); | |
i.setNext(j); | |
j.setNext(k); | |
} | |
@Test | |
void addFirstTest(){ | |
listNode.add(new ListNode(5),1); | |
assertListNodeKey(5,1,2,3,4); | |
} | |
@Test | |
void addZeroIndexTest(){ | |
listNode.add(new ListNode(5),0); | |
assertListNodeKey(5,1,2,3,4); | |
} | |
@Test | |
void addMidTest(){ | |
listNode.add(new ListNode(5),3); | |
assertListNodeKey(1,2,5,3,4); | |
} | |
@Test | |
void addLastTest(){ | |
listNode.add(new ListNode(5),5); | |
assertListNodeKey(1,2,3,4,5); | |
} | |
@Test | |
void addLastExceptionTest(){ | |
listNode.add(new ListNode(5),7); | |
assertListNodeKey(1,2,3,4); | |
} | |
@Test | |
void removeFirstTest(){ | |
listNode.remove(1); | |
assertListNodeKey(2,3,4); | |
} | |
@Test | |
void removeTest(){ | |
listNode.remove(3); | |
assertListNodeKey(1,2,4); | |
} | |
@Test | |
void removeExceptionTest(){ | |
listNode.remove(6); | |
assertListNodeKey(1,2,3,4); | |
} | |
@Test | |
void removeLastTest(){ | |
listNode.remove(4); | |
assertListNodeKey(1,2,3); | |
} | |
@Test | |
void containsTest(){ | |
boolean result = listNode.contains(2); | |
assertEquals(true,result); | |
} | |
@Test | |
void containsFailTest(){ | |
boolean result = listNode.contains(5); | |
assertEquals(false,result); | |
} | |
void assertListNodeKey(int ...data){ | |
ListNode node = listNode; | |
for(int i : data){ | |
node = node.getNext(); | |
assertEquals(i,node.getKey()); | |
} | |
assertNull(node.getNext()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment