package stack;
public class MyStack<T> implements IStack<T> {
private int size = 0;
private Node head = new Node();
@Override
public void push(T data) {
Node newNode = new Node();
newNode.data = data;
newNode.next = head.next;
head.next = newNode;
size++;
}
@Override
public T pop() {
if (isEmpty()) {
return null;
}
Node node = head.next;
head.next = node.next;
node.next = null; // 이 부분이 없어서 넣었는데
// 필요한건지 잘 모르겠음
this.size--;
return node.data;
}
@Override
public T peek() {
if (isEmpty()) {
return null;
}
return head.next.data;
}
@Override
public int size() {
return this.size;
}
private boolean isEmpty() {
return this.size == 0;
}
private class Node {
Node next;
T data;
}
}
댓글남기기