public class dailyCode{ public static void main(String[] args){ LinkedList list = new LinkedList(); list.insertLastNode("one"); list.insertLastNode("three"); list.printList();
ListNode preNode = list.searchNode("one"); if(preNode == null){ System.out.println("Can't find data."); } else { list.insertMiddleNode(preNode, "two"); list.printList(); } } } class LinkedList{ private ListNode first;
public LinkedList(){ first = null; }
public void insertMiddleNode(ListNode preNode, String data){ ListNode newNode = new ListNode(data); newNode.link = preNode.link; preNode.link = newNode; }
public void insertLastNode(String data){ ListNode newNode = new ListNode(data); if(first == null){ this.first = newNode; }else{ ListNode temp = first; while(temp.link != null){ temp = temp.link; } temp.link = newNode; } }
public void deleteLastNode(){ ListNode preNode, temp; if(first == null){ return; } if(first.link == null){ first = null; }else{ preNode = first; temp = first.link; while(temp.link != null){ preNode = temp; temp = temp.link; } preNode.link = null; } }
public ListNode searchNode(String data){ ListNode temp = this.first; while(temp != null){ if(temp.getData().equals(data)){ break; } temp = temp.link; } return temp; }
public void printList(){ ListNode temp = this.first; System.out.printf("List : "); while(temp != null){ System.out.printf(temp.getData()); temp = temp.link; if(temp != null){ System.out.printf(", "); } } System.out.println(""); } } class ListNode{ private String data; public ListNode link;
public ListNode(){ this.data = null; this.link = null; }
public ListNode(String data){ this.data = data; this.link = null; }
public ListNode(String data, ListNode link){ this.data = data; this.link = link; }
public String getData(){ return this.data; } } |
'프로그래밍 > JAVA' 카테고리의 다른 글
원형 큐(Circular Queue) (0) | 2016.12.08 |
---|---|
DatagramSocket Example (0) | 2016.12.03 |
Implement Stack (0) | 2016.12.01 |
How to get google server time (URLConnection Example) (0) | 2016.11.27 |
URL class Example (0) | 2016.11.27 |
Find Google IP (InetAddress class Example) (0) | 2016.11.25 |
람다 표현식 (Lambda Expression) (0) | 2016.11.24 |