Skip to content

Commit

Permalink
[DataStructure] LinkedList 구현수정 (using Interface)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seogeurim committed Jan 27, 2021
1 parent b47ca15 commit 2ef55a8
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 45 deletions.
34 changes: 22 additions & 12 deletions contents/data-structure/code/LinkedList/DoublyLinkedList.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package LinkedList;

public class DoublyLinkedList<E> {
public class DoublyLinkedList<E> implements ILinkedList<E> {
private Node<E> header;
private Node<E> trailer;
private long size;
private int size;

public DoublyLinkedList() {
header = new Node<>(null, null, null);
Expand All @@ -12,37 +12,45 @@ public DoublyLinkedList() {
size = 0;
}

public long size() {
@Override
public int size() {
return size;
}

@Override
public boolean isEmpty() {
return size == 0;
}

@Override
public E first() {
if (isEmpty()) return null;
return header.getNext().getElement();
}

@Override
public E last() {
if (isEmpty()) return null;
return trailer.getPrev().getElement();
}

@Override
public void addFirst(E e) {
addBetween(e, header, header.getNext());
}

@Override
public void addLast(E e) {
addBetween(e, trailer.getPrev(), trailer);
}

@Override
public E removeFirst() {
if (isEmpty()) return null;
return remove(header.getNext());
}

@Override
public E removeLast() {
if (isEmpty()) return null;
return remove(trailer.getPrev());
Expand Down Expand Up @@ -83,7 +91,9 @@ private static class Node<E> {
private Node<E> prev;
private Node<E> next;

public Node() { }
public Node() {
this(null, null, null);
}

public Node(E element, Node<E> prev, Node<E> next) {
this.element = element;
Expand All @@ -95,22 +105,22 @@ public E getElement() {
return element;
}

public Node<E> getPrev() {
return prev;
}

public Node<E> getNext() {
return next;
}

public void setElement(E element) {
this.element = element;
}

public Node<E> getPrev() {
return prev;
}

public void setPrev(Node<E> prev) {
this.prev = prev;
}

public Node<E> getNext() {
return next;
}

public void setNext(Node<E> next) {
this.next = next;
}
Expand Down
16 changes: 16 additions & 0 deletions contents/data-structure/code/LinkedList/ILinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package LinkedList;

public interface ILinkedList<E> {

public int size();
public boolean isEmpty();

public E first();
public E last();

public void addFirst(E e);
public void addLast(E e);

public E removeFirst();
public E removeLast();
}
48 changes: 30 additions & 18 deletions contents/data-structure/code/LinkedList/LinkedListExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,45 @@

public class LinkedListExample {
public static void main(String[] args) {
System.out.println("==================SLL==================");
SinglyLinkedList<Integer> sll = new SinglyLinkedList<>();
sll.addLast(10);
sll.addFirst(5);
System.out.println("==================Singly Linked List==================");
ILinkedList<Integer> sll = new SinglyLinkedList<>();
sll.addFirst(3);
sll.addLast(10);
sll.addFirst(2);
sll.addLast(12);
System.out.println(sll);
System.out.println(sll.size());
System.out.println(sll.first());
System.out.println(sll.last());
System.out.println(sll.removeFirst());
System.out.println("size : " + sll.size());
System.out.println("first : " + sll.first());
System.out.println("last : " + sll.last());
System.out.println("removeFirst : " + sll.removeFirst());
System.out.println(sll);
System.out.println("removeLast : " + sll.removeLast());
System.out.println(sll);
System.out.println("removeFirst : " + sll.removeFirst());
System.out.println(sll);
System.out.println("removeLast : " + sll.removeLast());
System.out.println(sll);
System.out.println("size : " + sll.size());

System.out.println("==================DLL==================");
DoublyLinkedList<Integer> dll = new DoublyLinkedList<>();
System.out.println("==================Doubly Linked List==================");
ILinkedList<Integer> dll = new DoublyLinkedList<>();
dll.addFirst(3);
dll.addLast(10);
dll.addFirst(2);
dll.addFirst(1);
dll.addFirst(10);
dll.addLast(50);
dll.addLast(12);
System.out.println(dll);
System.out.println("size : " + dll.size());
System.out.println("first : " + dll.first());
System.out.println("last : " + dll.last());
System.out.println("removeFirst : " + dll.removeFirst());
System.out.println(dll);
System.out.println("removeLast : " + dll.removeLast());
System.out.println(dll);
System.out.println(dll.size());
System.out.println(dll.first());
System.out.println(dll.last());
System.out.println(dll.removeFirst());
System.out.println("removeFirst : " + dll.removeFirst());
System.out.println(dll);
System.out.println(dll.removeLast());
System.out.println("removeLast : " + dll.removeLast());
System.out.println(dll);
System.out.println("size : " + dll.size());
}
}

59 changes: 44 additions & 15 deletions contents/data-structure/code/LinkedList/SinglyLinkedList.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,80 @@
package LinkedList;

public class SinglyLinkedList<E> {
public class SinglyLinkedList<E> implements ILinkedList<E> {
private Node<E> head;
private Node<E> tail;
private long size;
private int size;

public SinglyLinkedList() {
head = tail = null;
size = 0;
}

public long size() {
@Override
public int size() {
return size;
}

@Override
public boolean isEmpty() {
return size == 0;
}

@Override
public E first() {
if (isEmpty()) return null;
return head.getElement();
}

@Override
public E last() {
if (isEmpty()) return null;
return tail.getElement();
}

@Override
public void addFirst(E e) {
head = new Node<>(e, head);
if (isEmpty()) tail = head;
size ++;
}

@Override
public void addLast(E e) {
Node<E> newNode = new Node<>(e, null);
if (isEmpty()) head = newNode;
else tail.setNext(newNode);
tail = newNode;
Node<E> newest = new Node<>(e, null);
if (isEmpty()) head = newest;
else tail.setNext(newest);
tail = newest;
size ++;
}

@Override
public E removeFirst() {
if (isEmpty()) return null;
E target = head.getElement();
head = head.getNext();
size --;
if (size == 0) tail = null;
if (isEmpty()) tail = null;
return target;
}

@Override
public E removeLast() {
if (isEmpty()) return null;
E target;
if (head == tail) {
target = head.getElement();
head = tail = null;
} else {
Node<E> current = head;
while (current.getNext() != tail) {
current = current.getNext();
}
target = tail.getElement();
tail = current;
tail.setNext(null);
}
size --;
return target;
}

Expand All @@ -69,7 +96,9 @@ private static class Node<E> {
private E element;
private Node<E> next;

public Node() { }
public Node() {
this(null, null);
}

public Node(E element, Node<E> next) {
this.element = element;
Expand All @@ -80,16 +109,16 @@ public E getElement() {
return element;
}

public Node<E> getNext() {
return next;
public void setElement(E element) {
this.element = element;
}

public void setElement(E newElement) {
this.element = newElement;
public Node<E> getNext() {
return next;
}

public void setNext(Node<E> newNext) {
this.next = newNext;
public void setNext(Node<E> next) {
this.next = next;
}
}
}
Expand Down

0 comments on commit 2ef55a8

Please sign in to comment.