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
๋ฐ์ํ
'๐ STUDY > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ์ฌ๊ท (2) | 2022.05.18 |
|---|---|
| 1์ฐจ์ ๋ฐฐ์ด (1) | 2022.05.18 |
| Tree (0) | 2022.05.18 |
| List (0) | 2022.05.18 |
| QUEUE (0) | 2022.05.18 |