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.0041 sec