컴퓨터공학 💻 도서관📚
Part2. 5-6 큐(Queue) 구현하기 본문
Queue의 특징
- 맨 앞( front ) 에서 자료를 꺼내거나 삭제하고, 맨 뒤( rear )에서 자료를 추가 함
- Fist In First Out (선입선출) 구조
- 일상 생활에서 일렬로 줄 서 있는 모양
- 순차적으로 입력된 자료를 순서대로 처리하는데 많이 사용 되는 자료구조
- 콜센터에 들어온 문의 전화, 메세지 큐 등에 활용됨
- jdk 클래스 : ArrayList
연결리스트로 구현했기 떄문에 꽉 차 있는 경우는 생각하지 않아도 된다.
그래서 비어 있는 경우만 고려하면 된다
import linkedlist.MyListNode; // 이건 밑에 있다
import linkedlist.MyLinkedList;
interface IQueue{ // 인터페이스 선언해서 구현
public void enQueue(String data);
public String deQueue();
public void printQueue();
}
// 연결리스트를 상속받고 IQueue 를 구현
public class MyListQueue extends MyLinkedList implements IQueue{
MyListNode front;
MyListNode rear;
public MyListQueue()
{
front = null;
rear = null;
}
@override
public void enQueue(String data)
{
MyListNode newNode;
if(isEmpty()) // isEmpty 메서드는 MyLinkedList에 있다
{
newNode = addElement(data); // addElement 반환값을 newNode로 받아주기
front = newNode; // front, rear를 newNode로 초기화
rear = newNode;
}
else
{
newNode = addElement(data);
rear = newNode; // 맨 뒤에 추가
}
System.out.println(newNode.getData() + " added");
}
@override
public String deQueue()
{
if(isEmpty()){
System.out.println("Queue is Empty");
return null;
}
String data = front.getData();
front = front.next; // front에서 요소가 나감
if( front == null ){ // 큐가 빈 경우
rear = null;
}
return data;
}
@override
public void printQueue()
{
printAll();
}
}
public class MyListQueueTest {
public static void main(String[] args) {
MyListQueue listQueue = new MyListQueue();
listQueue.enQueue("A");
listQueue.enQueue("B");
listQueue.enQueue("C");
listQueue.enQueue("D");
listQueue.enQueue("E");
System.out.println(listQueue.deQueue());
listQueue.printAll();
}
}
참고) MyListNode 클래스, MyLinkedList 클래스
Part2. 5-4 연결 리스트 (LinkedList) 구현하기
Part2. 5-4 연결 리스트 (LinkedList) 구현하기
연결 리스트(LinkedList) 특징 동일한 데이터 타입을 순서에 따라 관리하는 자료 구조자료를 저장하는 노드에는 자료와 다음 요소를 가리키는 링크(포인터)가 있음자료가 추가 될때 노드 만큼의 메
computer-science-library.tistory.com
'✅🌲강의 복습 노트 > 패캠 JavaSpring 강의,코드 복습' 카테고리의 다른 글
Part2. 5-8 <T extends 클래스> 사용하기 (0) | 2025.06.24 |
---|---|
Part2. 5-7 무엇이든 담을 수 있는 제네릭(Generic) 프로그래밍 (자료형 매개변수 T) (0) | 2025.06.24 |
Part2. 5-5 스택(Stack) 구현하기 (0) | 2025.06.23 |
Part2. 5-4 연결 리스트 (LinkedList) 구현하기 (0) | 2025.06.21 |
Part2. 5-3 배열(Array) 구현하기 (0) | 2025.06.20 |
Comments