Jam's story

[컬렉션 프레임워크] - 스택 , 큐 본문

Java

[컬렉션 프레임워크] - 스택 , 큐

애플쩀 2022. 3. 25. 01:01

 

큐는 항상 첫번째 데이터를 삭제하므로, 
데이터 추가 삭제가 쉬운 
LinkedList 를 사용하는 것이 좋다. 

 

LIFO, FIFO 예제 

public class Prac {
public static void main(String[] args) {
	Stack st=new Stack();
	Queue q=new LinkedList();
	st.push("0");
	st.push(1);
	st.push(2);
	
	q.offer(0);
	q.offer(1);
	q.offer(2);
	
	System.out.println();
	while(!st.empty()) {
		System.out.println(st.pop());
	}
	while(!q.isEmpty()){
		System.out.println(q.poll()  );
	}
}
}
스택- 웹브라우저 앞뒤 
public class Prac {
	public static Stack back=new Stack();
	public static Stack forward=new Stack();
public static void main(String[] args) {

	goURL("1.네이트");
	goURL("2.야후");
	goURL("네이버");
	goURL("다음 ");
	
	printStatus();
} //main
/**
 * 
 */
private static void printStatus() {
	// TODO Auto-generated method stub
	System.out.println("back"+back);
	System.out.println("forward:"+forward);
	System.out.println(back.peek());

}

private static void goURL(String url) {
	// TODO Auto-generated method stub
	back.push(url);
	if(!forward.empty()) forward.clear();
}
public static void goForward() {
	if(!forward.empty()) back.push(forward.pop());
}
} //class

 

스택 - 수식계산, 수식괄호검사, 워드프로세서의 undo/redo , 웹브라우저의 뒤로 /앞으로 
큐- 최근사용문서, 인쇄작업 대기목록 , 버퍼 

 

'Java' 카테고리의 다른 글

[컬렉션 프레임워크] - TreeSet , HashMap  (0) 2022.03.26
26일차  (0) 2022.03.25
[컬렉션 프레임워크] -ArrayList  (0) 2022.03.24
25일차  (0) 2022.03.24
24일차- 제네릭 프레임워크  (0) 2022.03.23
Comments