package com.blogspot.coozplz;
/**
* C로 배우는 알고리즘에 있는
* 연결 리스트를 자바로 구현.
* @author Coozplz
*/
public class LinkedList1 {
Node head;
Node tail;
public void initList() {
head = new Node();
tail = new Node();
head.next = tail;
tail.next = tail;
}
public void deleteNext(Node node) {
Node temp;
if(node.next.hashCode() == tail.hashCode()) {
return ;
}
temp = node.next;
node.next = node.next.next;
temp = null;
}
public Node insertNode(int t, int k) {
Node s;
Node p;
Node r;
p = head;
s = p.next;
while(s.key != k && s.hashCode() != tail.hashCode()) {
p = p.next;
s = p.next;
}
if(s.hashCode() != tail.hashCode()) {
r = new Node();
r.key = t;
p.next = r;
r.next = s;
}
return p.next;
}
public Node orderedInsert(int k) {
Node s;
Node p;
Node r;
p = head;
s = p.next;
while(s.key <= k && s.hashCode() != tail.hashCode()) {
p = p.next;
s = p.next;
}
r = new Node();
r.key = k;
p.next = r;
r.next = s;
return r;
}
public void printList(Node t) {
System.out.println("Print Nodes");
while(t.hashCode() != tail.hashCode()) {
System.out.print("\t"+t.key);
t = t.next;
}
}
public static void main(String[] args) {
LinkedList1 test = new LinkedList1();
test.initList();
test.orderedInsert(10);
test.orderedInsert(5);
test.orderedInsert(8);
test.orderedInsert(3);
test.orderedInsert(1);
test.printList(test.head.next);
}
}
class Node {
int key;
Node next;
}