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