LinkedList (Convert to Java)

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;

}

답글 남기기

아래 항목을 채우거나 오른쪽 아이콘 중 하나를 클릭하여 로그 인 하세요:

WordPress.com 로고

WordPress.com의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

Twitter 사진

Twitter의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

Facebook 사진

Facebook의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

%s에 연결하는 중