U E D R , A S I H C RSS

Data Structure/Queue




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;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:05
Processing time 0.0106 sec