<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Population</title>
</head>

<body>



<p align="center"><font size="6" color="#FF0000"><b><span lang="en-ca">Queue</span></b></font></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A.<span lang="en-ca">First</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is <span lang="en-ca">my first edition of a simple queue based on array with very simple and limited methods.</span></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>A queue is an array with start and end points such that all elements between start and end is the elements of </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>the queue. (redundant?)</b></span></pre>
</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca">C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  <pre><span lang="en-ca"><b>Very simple, that I only use a counter to verify if queue is full or empty.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b>1. bool Queue::enqueue(int data)</b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>Add new data into queue except that I check all elements in queue to make sure there is no repetition of data.</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>2. </b></span><b>bool Queue::dequeue(int&amp; data)</b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>Retrieve data from start position and in order to avoid remove data from empty queue, I make it return type of </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>bool. Then data is passed by reference.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">C</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca">1. Its purpose is for my BFS with array. So far it is enough.</span></b></pre>
</div>
<pre>กก</pre>
<pre>#include &lt;iostream&gt;

using namespace std;

const int MaxQueueNumber = 4000;

class Queue
{
private:
	int lst[MaxQueueNumber];
	int start;
	int end;
	int counter;
	bool isRepeat(int data);
	int next(int index);
public:
	void onVisitData(int data);
	bool enqueue(int data);
	bool dequeue(int&amp; data);
	int queueCount(){return counter;}
	Queue();
};

int main()
{
	Queue Q;
	int data;
	for (int i=0; i&lt;400; i++)
	{
		Q.enqueue(i);
		cout&lt;&lt;Q.queueCount()&lt;&lt;endl;
	}
	for (i=0; i&lt;401; i++)
	{
		Q.dequeue(data);
		cout&lt;&lt;&quot;data is&quot;&lt;&lt;data&lt;&lt;endl;
		cout&lt;&lt;Q.queueCount()&lt;&lt;endl;
	}

	return 0;
}


Queue::Queue()
{
	start = 0;
	end = start;
	counter =0;
}

bool Queue::dequeue(int&amp; data)
{
	if (counter==0)
	{
		cout&lt;&lt;&quot;Queue is empty!&quot;&lt;&lt;endl;
		return false;
	}
	data = lst[start];
	start++;
	if (start==MaxQueueNumber)
	{
		start=0;
	}
	counter--;
	return true;
}


bool Queue::enqueue(int data)
{
	if (counter==MaxQueueNumber)
	{
		cout&lt;&lt;&quot;Queue is full!&quot;&lt;&lt;endl;
		return false;
	}
	if (!isRepeat(data))
	{
		lst[end]=data;
		end++;
		if (end==MaxQueueNumber)
		{
			end=0;
		}
		counter++;
		return true;
	}
	return false;	
}


int Queue::next(int index)
{
	if (end&gt;start)//normal
	{
		if (index&lt;end-1&amp;&amp;index&gt;=start)
		{
			return index+1;
		}
	}
	else
	{
		if (end&lt;start)//
		{
			if (index&gt;=start&amp;&amp;index&lt;MaxQueueNumber)//
			{
				if (index+1==MaxQueueNumber)
				{
					return 0;
				}
				else
				{
					return index+1;
				}
			}
			else
			{
				if (index&lt;end-1&amp;&amp;index&gt;=0)
				{
					return index+1;
				}
			}
		}
	}
	return -1;
}


bool Queue::isRepeat(int data)
{
	int index=start;

	while(index!=-1)
	{
		if (lst[index]==data)
		{
			return true;
		}
		index = next(index);
	}
	return false;
}







	</pre>
<pre></pre>
<pre>			
</pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="WhoAmI.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>