자바 연결리스트로 구현한 스택(Java Linkked List 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;
}
}
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;
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();
}
}
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() {
super ("The stack is empty.");
}
public EmptyStackException (String message) {
super (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());
}
}
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());
}
}
평일주간(9:30~18:30) 개강
(3/31)[기업100%환급]PL/SQL,ORACLE HINT,TUNING
(4/07)[기업100%환급]SQL기초에서 Schema Object까지
(4/07)[기업100%환급]자바기초에서 JDBC, Servlet/JSP까지
(4/14)C#4.0,ADO.NET,Network 프로그래밍
(4/14)[기업100%환급]Spring ,MyBatis,Hibernate실무과정
평일야간(19:00~22:00) 개강
(4/01)안드로이드개발자과정
(4/04)웹퍼블리싱 마스터
(4/04)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
(4/04)C#,ASP.NET마스터
(4/07)SQL초보에서실전전문가까지
(4/08)Spring3.X, MyBatis, Hibernate실무과정
주말(10:00~18:00) 개강
(3/29)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
(4/05)웹퍼블리싱 마스터
(4/05)닷넷실무자를위한WPF개발자과정
(4/05)Spring3.X, MyBatis, Hibernate실무과정
(4/05)SQL초보에서실전전문가까지
(4/12)C#,ASP.NET마스터
(4/12)안드로이드개발자과정
댓글 없음:
댓글 쓰기