LinkedList를 사용한 스택

1019 단어 linkedlist 스택
import java.util.LinkedList;

/*
 *  LinkedList     
 *LinkedList       ,           
 *
 */
public class Stack<T>{
	
	//          
	private LinkedList<T> stack;
	
	public Stack(){
		
		stack = new LinkedList<T>();
	}
	
	//    
	public void push(T obj){
		
		stack.addFirst(obj);
	}
	
	//      
	public T peek(){
		
		//       
		if(!isEmpty()){
			
			return stack.getFirst();
			
		}else{
			
			return null;
		}
		
	}
	
	//    
	public T pop(){
		
		//       
		if(!isEmpty()){
			
			return stack.removeFirst();
			
		}else{
			
			return null;
		}
		
	}
	
	//       
	public boolean isEmpty(){
		
		return stack.isEmpty();
	}
	
	//    
	public static void main(String[] args){
		
		String[] list = "my name is beyondlife".split(" ");
		
		Stack<String> stack = new Stack<String>();
		
		for(String obj : list){
			
			stack.push(obj);
		}
		
		while(stack.peek() != null){
			
			System.out.print(stack.pop() + " ");
		}
	}
}

좋은 웹페이지 즐겨찾기