1. Queue ¶
- Queue๋ ๋์ค์ ๋ฃ์ ๊ฒ์ ๋์ค์ ๋์ค๊ณ ๋จผ์ ๋ฃ์ ๊ฒ์ ๋จผ์ ๋์ค๋ ์๋ฃ ๊ตฌ์กฐ์
๋๋ค.
- ์ ์๋ ๋ปฅ ๋ซ๋ฆฐ.. ์๋ก ๋ฌผ๋ถ์ผ๋ฉด ๋ฐ์ผ๋ก ๋์ค๊ฒ ์ฃ ? (๋จผ์ ๋ค์ด๊ฐ๊ฑด ๋จผ์ ๋์จ๋ค!)
- ์ํ๊ธฐ๋ฅผ ์๊ฐํ๋ฉด ๋๊ฒ์ฃ ? ๋จผ์ ์ ์ฌ๋์ด ๋จผ์ ๋๊ฐ๋..(์-_-์น๊ธฐ ์ ์ธ)
- Front๋ ํ์ ๋งจ ์์ ๊ฐ๋ฅดํค๋ ๋ณ์, Rear๋ ํ์ ๋งจ ๋ ์์๋ฅผ ๊ฐ๋ฅดํค๋ ๋ณ์
1.1. ๋ฐฐ์ด๋ก ๊ตฌํํ Queue ¶
~cpp
class Queue
{
enum {Size=100};
private:
int m_nFront;
int m_nRear;
int m_nData[Size];
bool IsEmpty();
bool IsFull();
public:
Queue() {m_nFront=m_nRear=-1;}
bool Add(int ndata);
bool Erase();
void Show();
~Queue() {}
};
bool Queue::IsEmpty()
{
true;
return false;
}
bool Queue::IsFull()
{
if(m_nRear==Size-1 && m_nFront!=m_nRear)
return true;
return false;
}
bool Queue::Add(int ndata)
{
if(!IsFull())
{
m_nData[++m_nRear]=ndata;
return true;
}
else
{
cout<<"๊ฝ์ฐผ๋ค";
return false;
}
}
bool Queue::Erase()
{
if(!IsEmpty())
{
m_nFront++;
return true;
}
else
{
cout<<"๋น์๋ค";
return false;
}
}
void Queue::Show()
{
int count=m_nFront;
while(1)
{
cout<<m_nData[++count];
if(count==m_nRear)
break;
}
}
- ๊ทธ๋ฐ๋ฐ.. ์ด ํ๋ ๋ฌธ์ ์ ์ด ์์ต๋๋ค.
- ์์๋ฅผ ํ 90๊ฐ ๋ฃ๊ณ ๊ทธ ์์๋ฅผ 90๊ฐ ๋ค์ง์ฐ๋ฉด? Front์ Rear๊ฐ ๊ฐ๊ฐ 89๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋๊ฒ ์ง์? ๊ทธ๋ผ ๋จ๋ ๊ณต๊ฐ์ 10-_-๊ฐ๋ฐ์ ์๋ฉ๋๋ค.
- ์ด๋ ๊ฒ ๋๋ฉด ์ฒจ์ 100๊ฐ์ ๋ฐฐ์ด์ ํ ๋นํด์ค๊ฒ์ค์ 90๊ฐ๋ฅผ ๋ชป ์ฐ๊ฒ ๋๊ฒ ๋ค์.
- ์ด๋ฅผ ๋ณด์ํ๊ธฐ ์ํด ์ํ ํ๋ผ๋๊ฒ ์๋๋๋๋ค. ๋๋ ๋งํฌ๋ ๋ฆฌ์คํธ๋ก ํ๋ฅผ ๋ง๋ค์ด์ ์ ํ์์ด ์ฐ๋ ๋ฐฉ๋ฒ๋ ์๊ฒ ์ฃ .
- ์๊ฒ๋ ๋ง๋ค์ด๋ด์ผ๊ฒ ์ฃ ?^^
1.2. Linked List๋ก ๋ง๋ ํ ¶
~cpp
class Queue
{
private:
struct Node
{
int nData;
Node* pPrev;
};
Node* m_pFront;
Node* m_pRear;
bool IsEmpty();
public:
Queue();
~Queue();
void Add(int x);
void Remove();
};
Queue::Queue()
{
m_pFront=new Node;
m_pFront->pPrev=NULL;
m_pRear=m_pFront;
}
void Queue::Add(int x)
{
Node* temp=new Node;
temp->nData=x;
temp->pPrev=m_pRear;
m_pRear=temp;
}
void Queue::Remove()
{
if(!IsEmpty())
{
Node* temp=m_pRear->pPrev;
delete m_pRear;
m_pRear=temp;
}
else
cout<<"...";
}
bool Queue::IsEmpty()
{
if(m_pRear==m_pFront)
return true;
else
return false;
}
Queue::~Queue()
{
while(!IsEmpty())
{
Remove();
}
delete m_pFront;
}










