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