[JAVA] Queue

NamdarineΒ·2022λ…„ 1μ›” 24일
0

Data Structure

λͺ©λ‘ 보기
2/4
post-thumbnail

Queue

This post was written based on what I've learn in college class and the materials I looked for. Also, it was written to study while reviewing, so there may be insufficient or wrong information. If there's anything to fix, please leave a comment!

A queue is an example of a linear data structure, or more abstractly a sequential collection. Queues are common in computer programs, where they are implemented as data structures coupled with access routines, as an abstract data structure or in object-oriented languages as classes.

Definition of Queue

A line or sequence of people or vehicles awaiting their turn to be attended to or to proceed.

A collection of entities that are maintained in a sequence and can be modified by the addition of entities at one end of the sequence and the removal of entities from the other end of the sequence.
A structure in which elements are added to the rear and removed from the front; a "First in, First out" (FIFO) structure.

Real-life example of queue

One of the real-life example of queue is that getting in the line to order in McDonald's. The person who comes earlier through the door orders first and leave the line. The other people who come next order next and leave the line. In this situation, the door of this store is the entry(rear) of the queue, the line is the queue, and the other end that people leave after order is the exit(front) of the queue.
Simply we usually call it "First come, First serve".

Difference between Stack and Queue

In a stack we remove the element the most recently added. In a queue, we remove the element the least recently added.

Operation on Queue


Constructor

  • new: creates an empty queue

Transformers

  • enqueue: Adds an element to the rear of a queue. If the queue is full, throws the overflow exception. Similar with 'push()' in stack, but the difference is that 'push()' is added to the front of the stack.

  • dequeue: Removes an element from the front of the queue. The elements are popped in the same order in which they are pushed. If the queue is empty, throws the underflow exception. Similar with the 'pop()' in stack.

Observers
They have no effect on the queue.

  • front: Get the front element from queue.
  • rear: get the last item from queue.

Full code (Floating Front Queue)

Full code with Fixed Front Queue

Usage of Queue

  • Operating systems often maintain a queue of processes that are ready to execute or that are waiting for a particular event to occur.
  • Computer systems must often provide a "holding area" for messages between two processes, two programs, or even two systems. This holding area is usually called a "buffer" and is often implemented as a queue.
  • When a resource is shared among multiple consumers. Such as CPU scheduling, disk Scheduling.
  • When data is transferred asynchronously (data not necessarily received at same rate as sent) between two processes.Such as IO Buffers, pipes, file IO.

Complexity of Queue (Big-O)

enqueue, dequeue, front, and rear are O(1).

Implementation of Queue

Array-based, Linked-based.

Comparing Queue implementations

Storage size

  • Array-based: takes the same amount of memory, no matter how many array slots are actually used, proportional to current capacity.
  • Link-based: takes space proportional to actual size of the queue. But each element requires more space than array-based.

Operation efficiency

All operations are O(1), except for the constructors

  • Array-based: O(N)
  • Linked-based: O(1)

https://github.com/namdarine/DataStructure.git
QueueInterface, QueueOverflowException, QueueUnderflowException, Fixed Front Queue, Floating Front Queue, Wrap around with floating front Queue, and Linked list Queue codes are uploaded in this github repository.


큐

λ³Έ ν¬μŠ€νŒ…μ€ ν•™κ΅μ—μ„œ 배운 μˆ˜μ—…λ‚΄μš©κ³Ό 더 μ°Ύμ•„λ³Έ 자료λ₯Ό 기반으둜 μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
λ‹€μ‹œ λ³΅μŠ΅ν•˜λ©΄μ„œ κ³΅λΆ€ν•˜κΈ°μœ„ν•΄μ„œ μž‘μ„±ν•˜μ˜€μœΌλ―€λ‘œ λΆ€μ‘±ν•˜κ±°λ‚˜ 잘λͺ»λœ 것이 μžˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.
μˆ˜μ • 사항이 μžˆλ‹€λ©΄ λŒ“κΈ€ λ‹¬μ•„μ£Όμ„Έμš”!

νλŠ” μ„ ν˜• 데이터 ꡬ쑰의 예이고, 더 μΆ”μƒμ μœΌλ‘œ 순차적 μ»¬λ ‰μ…˜μ΄λ‹€. νλŠ” μ ‘κ·Ό 루틴과 κ²°ν•©λœ 데이터 ꡬ쑰, 좔상 데이터 ꡬ쑰 λ˜λŠ” 클래슀둜 객체 지ν–₯ μ–Έμ–΄λ‘œ κ΅¬ν˜„λ˜λŠ” 컴퓨터 ν”„λ‘œκ·Έλž¨μ—μ„œ ν”νžˆ λ³Ό 수 μžˆλ‹€.

큐의 μ •μ˜

μ°¨λ‘€λ₯Ό κΈ°λ‹€λ¦¬κ±°λ‚˜ μ§„ν–‰ν•˜κΈ° μœ„ν•΄ λŒ€κΈ° 쀑인 μ‚¬λžŒμ΄λ‚˜ μ°¨λŸ‰μ˜ 쀄 λ˜λŠ” μˆœμ„œ.

μ‹œν€€μŠ€μ—μ„œ μœ μ§€λ˜κ³  μ‹œν€€μŠ€μ˜ ν•œ 끝에 μš”μ†Œλ₯Ό μΆ”κ°€ν•˜κ³  λ‹€λ₯Έ λμ—λŠ” μš”μ†Œλ₯Ό μ œκ±°ν•˜μ—¬ μˆ˜μ •ν•  수 μžˆλŠ” μš”μ†Œλ“€μ˜ 집합이닀.
μš”μ†Œκ°€ 뒀에 μΆ”κ°€λ˜κ³  μ•žμ—μ„œ μ œκ±°λ˜λŠ” ꡬ쑰둜 "μ„ μž…μ„ μΆœ" ꡬ쑰이닀.

큐의 μ‹€μ œ μ˜ˆμ‹œ

우리 μΌμƒμƒν™œμ—μ„œ ν”νžˆ λ³Ό 수 μžˆλŠ” 큐의 μ˜ˆμ‹œλ‘œλŠ” 물건을 κ΅¬μž…ν•˜κΈ° μœ„ν•΄ μ„œμžˆλŠ” 쀄이닀. 문을 ν†΅ν•΄μ„œ μ‚¬λžŒλ“€μ΄ 맀μž₯으둜 λ“€μ–΄μ˜€κ³  μ£Όλ¬Έν•˜κΈ° μœ„ν•΄μ„œ 쀄을 μ„ λ‹€. λ¨Όμ € λ“€μ–΄μ˜¨ μ†λ‹˜μ€ λ‹€λ₯Έ 뒀에 μžˆλŠ” μ‚¬λžŒλ³΄λ‹€ λ¨Όμ € μ£Όλ¬Έν•˜κ³  μ€„μ—μ„œ λ‚˜κ°„λ‹€. κ·Έ λ‹€μŒ μ†λ‹˜λ„ λ§ˆμ°¬κ°€μ§€λ‘œ 주문을 ν•˜κ³  μ€„μ—μ„œ λ‚˜κ°„λ‹€. 순차적으둜 주문을 ν•˜κ³  μ€„μ—μ„œ λ‚˜κ°„λ‹€. μ—¬κΈ°μ„œ 'λ¬Έ'은 큐의 μž…κ΅¬, 즉 λ’€μͺ½μ΄λ‹€. '쀄'은 큐 본체이고, μ£Όλ¬Έν•˜λŠ” μͺ½μ€ 큐의 좜ꡬ, 즉 큐의 μ•žμͺ½μ΄λ‹€.

μŠ€νƒκ³Ό 큐의 차이점

μŠ€νƒμ€ κ°€μž₯ μ΅œκ·Όμ— μΆ”κ°€λœ μš”μ†Œλ₯Ό μ œκ±°ν•œλ‹€. λ°˜λŒ€λ‘œ νλŠ” κ°€μž₯ λ¨Όμ € μΆ”κ°€λœ μš”μ†Œλ₯Ό μ œκ±°ν•œλ‹€.

큐의 μž‘λ™


μƒμ„±μž

  • new: μƒˆλ‘œμš΄ 빈 큐λ₯Ό μƒμ„±ν•œλ‹€.

트랜슀포머

  • enqueue: 큐의 λ’€μͺ½μ— μš”μ†Œλ₯Ό μΆ”κ°€ν•œλ‹€. λ§Œμ•½ 큐가 λ‹€ μ°Όλ‹€λ©΄, overflow exception을 λ˜μ§„λ‹€. μŠ€νƒμ˜ 'push()'와 λΉ„μŠ·ν•˜μ§€λ§Œ 차이점은 'push()'λŠ” μŠ€νƒμ˜ μ•žμͺ½μœΌλ‘œ μΆ”κ°€ν•œλ‹€.

  • dequeue: 큐의 μ•žμͺ½μ— μžˆλŠ” μš”μ†Œλ₯Ό μ œκ±°ν•œλ‹€. μš”μ†ŒλŠ” μΆ”κ°€λœ μˆœμ„œλŒ€λ‘œ μ œκ±°λœλ‹€. λ§Œμ•½ 큐가 λΉ„μ–΄μžˆλ‹€λ©΄, underflow exception을 λ˜μ§„λ‹€. μŠ€νƒμ˜ 'pop()'κ³Ό λΉ„μŠ·ν•˜λ‹€.

κ΄€μ°°μž
큐에 λΌμΉ˜λŠ” 영ν–₯은 μ—†λ‹€.

  • front: 큐의 λ§¨μ•žμ˜ μš”μ†Œλ₯Ό λ°˜ν™˜ν•œλ‹€.
  • rear: 큐의 맨 λ’€μ˜ μš”μ†Œλ₯Ό λ°˜ν™˜ν•œλ‹€.

전체 μ½”λ“œ (Floating Front Queue)

Fixed Front Queue 전체 μ½”λ“œ

큐의 μ‚¬μš©

  • 운영 μ²΄μ œλŠ” μ‹€ν–‰ μ€€λΉ„κ°€ λ˜μ—ˆκ±°λ‚˜ νŠΉμ • μ΄λ²€νŠΈκ°€ λ°œμƒν•˜κΈ°λ₯Ό κΈ°λ‹€λ¦¬λŠ” ν”„λ‘œμ„ΈμŠ€μ˜ λŒ€κΈ°μ—΄μ„ μœ μ§€ν•˜λŠ” 경우 자주 μ‚¬μš©λœλ‹€.
  • 컴퓨터 μ‹œμŠ€ν…œμ€ μ’…μ’… 두 ν”„λ‘œμ„ΈμŠ€, 두 ν”„λ‘œκ·Έλž¨ λ˜λŠ” 심지어 두 μ‹œμŠ€ν…œ μ‚¬μ΄μ˜ λ©”μ‹œμ§€λ₯Ό μœ„ν•œ "보λ₯˜ μ˜μ—­(holding area)"을 μ œκ³΅ν•΄μ•Ό ν•œλ‹€. 이 보λ₯˜ μ˜μ—­μ€ 보톡 "버퍼(buffer)"라고 뢈리며 μ’…μ’… 큐둜 κ΅¬ν˜„λœλ‹€.
  • λ¦¬μ†ŒμŠ€κ°€ μ—¬λŸ¬ μ†ŒλΉ„μž 간에 κ³΅μœ λ˜λŠ” 경우. 예λ₯Ό λ“€μ–΄ CPU μŠ€μΌ€μ€„λ§, λ””μŠ€ν¬ μŠ€μΌ€μ€„λ§ 등이 μžˆλ‹€.
  • 데이터가 두 ν”„λ‘œμ„ΈμŠ€ 간에 λΉ„λ™κΈ°μ μœΌλ‘œ 전솑될 λ•Œ (데이터λ₯Ό λ°˜λ“œμ‹œ λ™μΌν•œ μ†λ„λ‘œ μˆ˜μ‹ ν•  ν•„μš”λŠ” μ—†μŒ). 예λ₯Ό λ“€μ–΄ IO 버퍼, νŒŒμ΄ν”„, 파일 IO 등이 μžˆλ‹€.

큐의 λ³΅μž‘μ„± (Big-O)

enqueue, dequeue, front 그리고 rear 연산은 O(1)이닀.

큐의 κ΅¬ν˜„

Array-based, Linked-based

큐 κ΅¬ν˜„λ°©λ²•λ“€μ˜ 비ꡐ

μ €μž₯곡간

  • Array-based: μ‹€μ œλ‘œ μ‚¬μš©λ˜λŠ” array 슬둯 μˆ˜μ— 관계없이 ν˜„μž¬ μš©λŸ‰μ— λΉ„λ‘€ν•˜μ—¬ λ™μΌν•œ μ–‘μ˜ λ©”λͺ¨λ¦¬λ₯Ό μ‚¬μš©ν•œλ‹€.
  • Linked-based: 큐의 μ‹€μ œ 크기에 λΉ„λ‘€ν•˜λŠ” 곡간을 μ‚¬μš©ν•œλ‹€. κ·ΈλŸ¬λ‚˜ 각 μš”μ†Œμ—λŠ” array κΈ°λ°˜λ³΄λ‹€ 더 λ§Žμ€ 곡간이 ν•„μš”ν•˜λ‹€.

μž‘μ—… 효율

μƒμ„±μžλ₯Ό μ œμ™Έν•œ λͺ¨λ“  연산은 O(1)이닀

  • Array-based: O(N)
  • Linked-based: O(1)

Reference

https://github.com/namdarine/DataStructure.git
이 κΉƒν—ˆλΈŒ λ ˆνΌμ§“ν† λ¦¬μ— QueueInterface, QueueOverflowException, QueueUnderflowException, Fixed Front Queue, Floating Front Queue, Wrap arround with floating front Queue, and Linked list Queue μ½”λ“œλ“€μ΄ μžˆμŠ΅λ‹ˆλ‹€.

0개의 λŒ“κΈ€