public App()
{
Stack stack = new Stack();
bool empty = stack.IsEmpty();
Console.WriteLine("empty: {0}", empty);
stack.Push(3);
}
class Stack
{
// nested class 중첩 클래스
private class Node
{
public int Data
{
get;
private set;
}
public Node Next { get; set; }
public Node(int data)
{
this.Data = data;
}
}
private Node top;
public Stack()
{
Console.WriteLine("스택이 생성되었습니다.");
}
public void Push(int value)
{
if (top == null)
{
top = new Node(value);
}
}
public bool IsEmpty()
{
if (this.top == null)
{
return true;
}
else
{
return false;
}
}
}
stack.Push(3);
stack.Push(5);
public void Push(int value)
{
if (top == null)
{
top = new Node(value);
Console.WriteLine("stack에 {0}이 추가 되었습니다.", value);
}
else
{
top.Next = new Node(value);
Console.WriteLine("stack에 {0}이 새로 추가 되었습니다.", value);
}
}
// Push 메서드 변경
public void Push(int value)
{
if (top == null)
{
top = new Node(value);
Console.WriteLine("stack에 {0}이 추가 되었습니다.", value);
}
else
{
Node temp = top;
top = new Node(value);
top.Next = temp;
Console.WriteLine("stack에 {0}이 새로 추가 되었습니다.", value);
}
}