template<typename TYPE>
template<typename TYPE>
struct Node
{
TYPE data;
Node<TYPE>* next;
};
template<typename TYPE>
// 스택에서는 template를 하나만 해도 된다. 유니크한 것이 없기 때문에. KEY는 필요 없음.
class Stack
{
private:
int count;
Node<TYPE>* top;
public:
//필요 함수 8개 생성자,소멸자,푸쉬.팝
Stack();
~Stack();
bool PushStack(TYPE dataln); //데이터 받아서 끝
bool PopStack(TYPE& dataOut); //데이터 값을 외부에 전달 = &
bool StackTop(TYPE& dataOut);
bool emptyStack();
bool fullStack();
int StackCount();
};
template<typename TYPE>
Stack<TYPE>::Stack()
{
top = NULL;
count = 0;
} //Stack<TYPE>::Stack() 생성자
template<typename TYPE>
Stack<TYPE>::~Stack()
{
Node<TYPE>* temp;
while(top != NULL) // 노드 하나하나 삭제해야함!
{
temp = top;
top = top->next;
delete temp;
} //while(top != NULL)
count = 0;
} //Stack<TYPE>::~Stack()
template<typename TYPE>
bool Stack<TYPE>::PushStack(TYPE dataln)
{
bool success;
Node<TYPE>* newPtr;
newPtr = new NODE<TYPE>; // 메모리확보
if(count == NULL)
{
success = false;
}
else
{
newPtr->data = dataln;
newPtr->next = top;
top = newPtr;
count++;
success = true;
}
return success;
}//bool Stack<TYPE>::PushStack(TYPE dataln)
template<typename TYPE>
bool Stack<TYPE>::PopStack(TYPE& dataOut)
{
bool success;
Node<TYPE>* dltPtr;
if( count == 0)
{
success = false;
}
else
{
dltPtr = top;
dataOut = top->data;
top = top->next;
count--;
success = true;
}
} //bool Stack<TYPE>::PopStack(TYPE& dataOut)
template<typename TYPE>
bool Stack<TYPE>::StackTop(TYPE& dataOut)
{
bool success;
if( count == 0)
{
success = false;
}
else
{
dataOut = top->data;
success = true;
}
} //bool Stack<TYPE>::StackTop(TYPE& dataOut)
template<typename TYPE>
int Stack<TYPE>::StackCount()
{
return count;
}//int Stack<TYPE>::StackCount()
template<typename TYPE>
bool Stack<TYPE>::fullStack()
{
bool success;
Node<TYPE>* tt;
//메모리확보하기
tt = new NODE<TYPE>;
if( tt = NULL )
{
return false;
}
else
{
delete tt; //필요없는 메모리 삭제하기
return true;
}
return success;
}//bool Stack<TYPE>::fullStack()
template<typename TYPE>
bool Stack<TYPE>::emptyStack()
{
// 스택이 비어있으면 return true를 한다.
return (count== 0);
}