-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Seogeurim/DataStructure/Seogeurim
Data structure/seogeurim
- Loading branch information
Showing
5 changed files
with
339 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
contents/data-structure/code/LinkedList/DoublyLinkedList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package LinkedList; | ||
|
||
public class DoublyLinkedList<E> implements ILinkedList<E> { | ||
private Node<E> header; | ||
private Node<E> trailer; | ||
private int size; | ||
|
||
public DoublyLinkedList() { | ||
header = new Node<>(null, null, null); | ||
trailer = new Node<>(null, header, null); | ||
header.setNext(trailer); | ||
size = 0; | ||
} | ||
|
||
@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()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
if (isEmpty()) return "[]"; | ||
|
||
StringBuilder sb = new StringBuilder("["); | ||
DoublyLinkedList.Node<E> current = header.getNext(); | ||
while (current.getNext() != trailer) { | ||
sb.append(current.getElement()).append(", "); | ||
current = current.getNext(); | ||
} | ||
sb.append(current.getElement()).append("]"); | ||
return sb.toString(); | ||
} | ||
|
||
private void addBetween(E e, Node<E> predecessor, Node<E> successor) { | ||
Node<E> newest = new Node<>(e, predecessor, successor); | ||
predecessor.setNext(newest); | ||
successor.setPrev(newest); | ||
size ++; | ||
} | ||
|
||
private E remove(Node<E> node) { | ||
Node<E> predecessor = node.getPrev(); | ||
Node<E> successor = node.getNext(); | ||
predecessor.setNext(successor); | ||
successor.setPrev(predecessor); | ||
size --; | ||
return node.getElement(); | ||
} | ||
|
||
private static class Node<E> { | ||
private E element; | ||
private Node<E> prev; | ||
private Node<E> next; | ||
|
||
public Node() { | ||
this(null, null, null); | ||
} | ||
|
||
public Node(E element, Node<E> prev, Node<E> next) { | ||
this.element = element; | ||
this.prev = prev; | ||
this.next = next; | ||
} | ||
|
||
public E getElement() { | ||
return element; | ||
} | ||
|
||
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
46 changes: 46 additions & 0 deletions
46
contents/data-structure/code/LinkedList/LinkedListExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package LinkedList; | ||
|
||
public class LinkedListExample { | ||
public static void main(String[] args) { | ||
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("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("==================Doubly Linked List=================="); | ||
ILinkedList<Integer> dll = new DoublyLinkedList<>(); | ||
dll.addFirst(3); | ||
dll.addLast(10); | ||
dll.addFirst(2); | ||
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("removeFirst : " + dll.removeFirst()); | ||
System.out.println(dll); | ||
System.out.println("removeLast : " + dll.removeLast()); | ||
System.out.println(dll); | ||
System.out.println("size : " + dll.size()); | ||
} | ||
} | ||
|
125 changes: 125 additions & 0 deletions
125
contents/data-structure/code/LinkedList/SinglyLinkedList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package LinkedList; | ||
|
||
public class SinglyLinkedList<E> implements ILinkedList<E> { | ||
private Node<E> head; | ||
private Node<E> tail; | ||
private int size; | ||
|
||
public SinglyLinkedList() { | ||
head = tail = null; | ||
size = 0; | ||
} | ||
|
||
@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> 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 (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; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
if (isEmpty()) return "[]"; | ||
|
||
StringBuilder sb = new StringBuilder("["); | ||
Node<E> current = head; | ||
while (current.getNext() != null) { | ||
sb.append(current.getElement()).append(", "); | ||
current = current.getNext(); | ||
} | ||
sb.append(current.getElement()).append("]"); | ||
return sb.toString(); | ||
} | ||
|
||
private static class Node<E> { | ||
private E element; | ||
private Node<E> next; | ||
|
||
public Node() { | ||
this(null, null); | ||
} | ||
|
||
public Node(E element, Node<E> next) { | ||
this.element = element; | ||
this.next = next; | ||
} | ||
|
||
public E getElement() { | ||
return element; | ||
} | ||
|
||
public void setElement(E element) { | ||
this.element = element; | ||
} | ||
|
||
public Node<E> getNext() { | ||
return next; | ||
} | ||
|
||
public void setNext(Node<E> next) { | ||
this.next = next; | ||
} | ||
} | ||
} | ||
|