2014년 2월 5일 수요일

자바 연결리스트로 구현한 스택(Java Linkked List Stack) 소스 입니다.

자바 연결리스트로 구현한 스택(Java Linkked List Stack) 소스 입니다.
 
interface Stack {  
            public boolean isEmpty();
            public Object peek();
            public void push(Object theObject);
            public Object pop();
}
 
 
 
 
class ChainNode  {           
          Object element;
          ChainNode next;          
          ChainNode() {}    
          ChainNode(Object element) { this.element = element;}
          ChainNode(Object element, ChainNode next)  {
    this.element = element;   this.next = next;
     }
}
public class LinkedStack implements Stack {
     protected ChainNode topNode;
        
     public LinkedStack() {      
  topNode = new ChainNode();
  }


 public boolean isEmpty() {
  return topNode.next == null;
 }

 public Object peek() {
      if (isEmpty())      throw new EmptyStackException();
      return topNode.next.element;
}

  public void push(Object theElement) {
  ChainNode p = new ChainNode(theElement, null);
  p.next = topNode.next;
  topNode.next = p;
  }
 
  public Object pop() {
    if (isEmpty())    throw new EmptyStackException();
    Object topElement = topNode.next.element;
    topNode.next = topNode.next.next;
    return topElement;
  }
  public String  toString() {
   StringBuffer sb = new StringBuffer("[");
   ChainNode p = topNode.next;
   do  {
    if (p.element == null) sb.append("null");
    else sb.append(p.element.toString());
    p = p.next;
    if (p != null) sb.append(",");
   } while (p != null);
   sb.append("]");
   return sb.toString();
  }
}
 
public class EmptyStackException extends RuntimeException {
   public EmptyStackException()   {
      super ("The stack is empty.");
   }
   public EmptyStackException (String message)   {
      super (message);
   }
}
 
 
 
public class Main {
  public static void main(String args[]) {   
      LinkedStack myStack = new LinkedStack();
      myStack.push("a");
      myStack.push("b");
      myStack.push("c");
   System.out.println(myStack);
   myStack.pop();  
     System.out.println(myStack);  
   System.out.print("현재 top pointer의 data : ");
     System.out.println(myStack.peek());
  }
}

자바 연결리스트로 구현한 스택(Java Linkked List Stack) 소스 입니다.
 
interface Stack {  
            public boolean isEmpty();
            public Object peek();
            public void push(Object theObject);
            public Object pop();
}
 
 
 
 
class ChainNode  {           
          Object element;
          ChainNode next;          
          ChainNode() {}    
          ChainNode(Object element) { this.element = element;}
          ChainNode(Object element, ChainNode next)  {
    this.element = element;   this.next = next;
     }
}
public class LinkedStack implements Stack {
     protected ChainNode topNode;
        
     public LinkedStack() {      
  topNode = new ChainNode();
  }


 public boolean isEmpty() {
  return topNode.next == null;
 }

 public Object peek() {
      if (isEmpty())      throw new EmptyStackException();
      return topNode.next.element;
}

  public void push(Object theElement) {
  ChainNode p = new ChainNode(theElement, null);
  p.next = topNode.next;
  topNode.next = p;
  }
 
  public Object pop() {
    if (isEmpty())    throw new EmptyStackException();
    Object topElement = topNode.next.element;
    topNode.next = topNode.next.next;
    return topElement;
  }
  public String  toString() {
   StringBuffer sb = new StringBuffer("[");
   ChainNode p = topNode.next;
   do  {
    if (p.element == null) sb.append("null");
    else sb.append(p.element.toString());
    p = p.next;
    if (p != null) sb.append(",");
   } while (p != null);
   sb.append("]");
   return sb.toString();
  }
}
 
public class EmptyStackException extends RuntimeException {
   public EmptyStackException()   {
      super ("The stack is empty.");
   }
   public EmptyStackException (String message)   {
      super (message);
   }
}
 
 
 
public class Main {
  public static void main(String args[]) {   
      LinkedStack myStack = new LinkedStack();
      myStack.push("a");
      myStack.push("b");
      myStack.push("c");
   System.out.println(myStack);
   myStack.pop();  
     System.out.println(myStack);  
   System.out.print("현재 top pointer의 data : ");
     System.out.println(myStack.peek());
  }
}


댓글 없음:

댓글 쓰기