Design Pattern | Data structure

[Liked List] 단방향 구현

빠빠담 2020. 7. 7. 00:52
반응형

youtu.be/C1SDkdPvQPA

youtu.be/IrXYr7T8u_s

package dataStructure.singlyLinkedList;

public class Node {

    private int data;
    private Node next = null;

    public Node(int data) {
        this.data = data;
    }

    public int getData() {
        return data;
    }

    public boolean hasNext(){
        if(this.next == null){
            return false;
        }else{
            return true;
        }
    }

    public Node getNext(){
        return next;
    }

    public boolean setNext(Node next){
        try{
            this.next = next;
            return true;

        }catch (Exception e){
            return false;
        }
    }

}

 

package dataStructure.singlyLinkedList;

public class SinglyLinkedList {

    private Node head;

    public SinglyLinkedList() {
        head = null;
    }

    public boolean append(int d){
        try {
            Node end = new Node(d);

            Node nextNode;

            if (head == null) {
                head = end;

            } else {
                nextNode = head;

                while (nextNode.hasNext()) {
                    nextNode = nextNode.getNext();
                }

                nextNode.setNext(end);

            }

            return true;

        }catch (Exception e){
            return false;
        }

    }

    public boolean delete (int d){
        try{

            if(head.getData() == d){
                head = head.getNext();

            }else{
                Node nextNode = head;
                while(nextNode.hasNext()){
                    Node beforeNode = nextNode;
                    nextNode = nextNode.getNext();
                    if(nextNode.getData() == d){
                        beforeNode.setNext(nextNode.getNext());
                    }
                }

            }

            return true;

        }catch (Exception e){
            return false;
        }
    }

    public void retrieve(){
        Node nextNode = head;
        if(nextNode != null){
            System.out.print(nextNode.getData());
            while(nextNode.hasNext()){
                nextNode = nextNode.getNext();
                System.out.print(" -> " + nextNode.getData());
            }
            System.out.println();
        }
    }

}

 

package dataStructure.singlyLinkedList;

public class TestRun {

    public static void main(String[] args) {
        SinglyLinkedList singlyLinkedList = new SinglyLinkedList();

        singlyLinkedList.append(1);
        singlyLinkedList.append(2);
        singlyLinkedList.append(3);
        singlyLinkedList.append(4);

        singlyLinkedList.retrieve();

        singlyLinkedList.delete(2);

        singlyLinkedList.retrieve();

        singlyLinkedList.append(5);

        singlyLinkedList.retrieve();


    }
}

 

package dataStructure.singlyLinkedList;

public class SinglyLinkedList {

    private Node head;

    public SinglyLinkedList() {
        head = new Node();
    }

    private static class Node{
        private int data;
        private Node next = null;
    }

    public boolean append(int d){
        try {
            Node end = new Node();
            end.data = d;

            Node nextNode = head;
            while (nextNode.next != null) {
                nextNode = nextNode.next;
            }

            nextNode.next = end;


            return true;

        }catch (Exception e){
            return false;
        }

    }

    public boolean delete (int d){
        try{

            Node nextNode = head;
            while(nextNode.next != null){
                if(nextNode.next.data == d){
                    nextNode.next = nextNode.next.next;
                }

                nextNode = nextNode.next;
            }

            return true;

        }catch (Exception e){
            return false;
        }
    }

    public void retrieve(){
        Node nextNode = head;
        if(nextNode.next != null){
            nextNode = nextNode.next;

            System.out.print(nextNode.data);
            while(nextNode.next != null){
                nextNode = nextNode.next;
                System.out.print(" -> " + nextNode.data);
            }
            System.out.println();
        }
    }

}
반응형

'Design Pattern | Data structure' 카테고리의 다른 글

Transactional Outbox Pattern (아웃박스 패턴)  (0) 2023.06.18
[Linked List] 중복값 삭제  (0) 2020.07.08
[Linked List] 개념  (0) 2020.07.07
Observer Pattern  (0) 2020.05.10