1. Stack ¶
- ์คํ(Stack) : ๋์ค์ ๋ค์ด์จ๊ฒ ๋จผ์ ๋๊ฐ. ๋ฐ์ ๋งํ ์๊ณ ์๋ง ๋ซ๋ ค ์๋ ํต์ด๋ผ๊ณ ์๊ฐํ๋ฉด ๋จ. ๋ฐ์ ๋๋ ค๋ด๊ณ ๊บผ๋ผ์๋ ์๋ ๋
ธ๋ฆ์ด๋ ์ง์ด๋ฃ์๋๋ ์๋ก, ๋บ๋๋ ์๋ก ๋นผ์ผ๊ฒ ์ ธ?^^;;
- top ํฌ์ธํฐ๋ ๋งจ ๋์ค์ ์ง์ด ๋ฃ์ ๋
ธ๋๋ฅผ ๊ฐ๋ฅดํด.
- ๋ฐ๋ผ์ ๋ฐ์ดํฐ๊ฐ ์ถ๊ฐ๋๊ฑฐ๋ ์ญ์ ๋ ๋๋ง๋ค topํฌ์ธํฐ๊ฐ ๋ณํ๊ฒ ์ฃ ?^^;;
1.1. ๋ฐฐ์ด๋ก ์ง ์คํ ¶
~cpp
class Stack
{
enum {Size=100};
private:
int data[Size];
int top;
bool IsEmpty();
bool IsFull();
public:
Stack() { top=-1; }
bool Push(int ndata);
bool Pop();
void Show();
~Stack() {}
};
bool Stack::IsEmpty()
{
if(top==-1)
return true;
return false;
}
bool Stack::IsFull()
{
if(top==Size-1)
return true;
return false;
}
bool Stack::Push(int ndata)
{
if(!IsFull())
{
data[++top]=ndata;
return true;
}
else
{
cout<<"๊ฝ ์ฐผ๋ค";
return false;
}
}
bool Stack::Pop()
{
if(!IsEmpty())
{
top--;
return true;
}
else
{
cout<<"๋น์๋ค";
return false;
}
}
void Show()
{
int temp=top;
while(temp!=-1)
cout<<data[temp--];
}
1.2. Linked List๋ก ๋ง๋ Stack ¶
~cpp
class Stack
{
private:
struct Node
{
int m_nData;
Node* m_pPrev;
};
Node* Head; // ์๋ฌด๊ฒ๋ ์๋ ํค๋ ๋
ธ๋ ํ๋ ์์ฑ(์๊ฒ ์์ผ๋ฉด ์์ฒญ ํธํจ!)
Node* top;
bool IsEmpty();
public:
Stack();
void Push(int x);
void Pop();
void Show();
~Stack();
};
Stack::Stack()
{
Head=new Node;
Head->m_pPrev=NULL;
top=Head;
}
void Stack::Push(int x)
{
Node* temp=new Node;
temp->m_nData=x;
temp->m_pPrev=top;
top=temp;
}
bool Stack::Pop()
{
if(!IsEmpty())
{
Node* temp=top->m_pPrev;
delete top;
top=temp;
return true;
}
else
{
cout<<"๋น์๋ค";
return false;
}
}
void Stack::Show()
{
Node* temp=top;
while(!IsEmpty())
{
cout<<top->m_nData<<endl;
top=top->m_pPrev;
}
top=temp;
}
bool Stack::IsEmpty()
{
if(top==Head)
return true;
return false;
}
Stack::~Stack()
{
while(!IsEmpty())
{
Pop();
}
delete Head;
}










