728x90
반응형
public class Node{
//노드 클래스
public String data;//데이터필드
public Node link;
public Node(String data){
super();
this.data = data;
}
public Node(String data, Node link){
this(data);
this.link = link;}
@override
public String toString(){
return "Node [ data="+data+"link="+link+"]";
}
public class Stack{
private Node top; //리스트의 시작점
private void push(String data){
Node newNode = new Node(data, top);
top= newNode;
}
//isEmpty
public boolean isEmpty(){return top ==null;}
//pop
public String pop(){
if(!isEmpty){Node popNode = top; top = popNode.link; popNode.link = null; return popNode.data;}
else{System.out.println("invalid command. stack is empty...");return null;} }
//peek
public String pek(){
if(!isEmpty){return top.data;}
else{System.out.println("invalid command. stack is empty...");return null;}
@override
public String toString(){
StringBuilder sb - new StringBuilder();
sb.append("S (");
for(Node currNode = top; currNode != null; currNode == curNode.link){
sb.append(currNode.data).append(",");}
if(!isEmpty()) sb.setlength(sb.length()-1); //마지막comma 지우기
sb.append(")");
return sb.toString();
}
}
public class StackTest{
public static void main(String[] args){
Stack stack = new Stack();
System.out.println(stack.isEmpty());
statck.push("뀨뀨");
System.out.println(stack.peek());
System.out.println(statck.pop());
System.out.println(stack);
System.out.println(stack);
}
}
728x90
반응형
'취준 > JAVA' 카테고리의 다른 글
재귀 (0) | 2022.05.18 |
---|---|
1차원 배열 (0) | 2022.05.18 |
Tree (0) | 2022.05.18 |
List (0) | 2022.05.18 |
QUEUE (0) | 2022.05.18 |