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