Zeta Oph's Study

[Data Structure (Java)] Queue 본문

프로그래밍/Java

[Data Structure (Java)] Queue

Zeta Oph 2024. 5. 9. 18:37

https://crane206265.tistory.com/77

 

[Data Structure (Java)] Stack

https://crane206265.tistory.com/76 [Data Structure (Java)] SLL (Singly Linked List)https://crane206265.tistory.com/75 [Data Structure (Java)] Generichttps://crane206265.tistory.com/74?category=1076637  [Data Structure (Java)] Arrayhttps://crane206265.ti

crane206265.tistory.com

저번에 stack을 다루었으니, 이번에는 queue입니다. stack과 queue의 비교는 저번글에 있으니 참고하시면 될 것 같습니다.


Queue API

API of Queue<E> class

methods explanation
void enqueue(E e) add an item to the collection (add to end)
E dequeue() remove and return the item least recently added (remove from beginning)
E first() return the first element
boolean isEmpty() return if stack is empty
int size() return the size of stack

Queue implementation -  Linked List

using SLL class,

public class LinkedQueue<E> implements Queue<E> {
    private SinglyLinkedList<E> list = new SinglyLinkedList<>();
    
    public LinkedQueue() {};
    
    public int size() { return list.size(); }
    public boolean isEmpty() { return list.isEmpty(); }
    public void enqueue(E e) { list.addLast(e); }
    public E first() { return list.first(); }
    public E dequeue() { return list.removeFirst(); }
}

 

* enqueue : addLast() / dequeue : removeFirst()

<-- to implement operation as running time O(1)

(removeLast() : O(n))

 

Properties about Linked-List queue
- running time analysis : O(1) for every operation
- memory use : scalable