Skip to content

Chapter 5, Queue 구현 영역에서 코드 수정 필요 #5

@baobabnamu

Description

@baobabnamu

안녕하세요, 박은종 저자님
다름이 아니라 올려주신 샘플 코드에 문제가 있어보여 issue 남겨드립니다.
Chapter 5 - 05 Queue 구현 부분에 대한 내용입니다.

Queue 구현 시 상속된 isEmpty() 메서드를 통해 Queue의 공간이 비었는지 확인합니다.
그러나 Front, Rear 변수(MyListNode 타입) 중에 어떠한 것도 해당 메서드에 영향을 받지 않는 것으로 보입니다.
이에 별도로 isEmpty를 오버로딩 한 후 사용해야 할 것 같습니다.

public boolean isEmpty(MyListNode node)
	{
		if(node == null) return true;
		else return false;
	}
public String deQueue()
	{
		if(isEmpty(front)&&isEmpty(rear)){
			System.out.println("Queue is Empty");
			return null;
		}
		String data = front.getData();
		front = front.next;
		if( front == null ){  // 마지막 항목
			rear = null;
		}
		return data;
	}

또는 isEmpty() 메서드 대신 Front, Rear 변수가 null 인지 확인하는 조건문을 사용하면 될 것 같습니다.

public String deQueue()
	{
		if(front == null && rear == null){
			System.out.println("Queue is Empty");
			return null;
		}
		String data = front.getData();
		front = front.next;
		if( front == null ){  // 마지막 항목
			rear = null;
		}
		return data;
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions