<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"><span lang="en-ca"><font size="6" color="#FF0000"><b>Data 
Structure Practice(</span>3<span lang="en-ca">)</span></b></font></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. Third Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is third<span lang="en-ca"> edition of my practice of data structure, </span>or whatever you like to call it. It is trivial and I just</b></pre>
</div>
<div align="left">
  <pre><b>don't want to keep all functions messed up. </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"><font size="3"><b>To write those data structure and  algorithms for practice. </b></font></span></pre>
</div>
<div align="left">
  <pre><font size="3"><b>1. To write a function to print all data of a binary tree by a specific level.</b></font></pre>
</div>
<div align="left">
  <pre><font size="3"><b>2. To sort a stack and all you can use is other stacks.กก<span lang="en-ca">The result should be like this: from the </span></b></font></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>top of stack to bottom of it, the element is in ascending order.</b></font></span></pre>
</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>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">
  กก</div>
<p><span lang="en-ca"><b>I am just </span>practicing<span lang="en-ca"> by myself. </span> </b></p>
<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><font size="3"><span lang="en-ca">1. </span></font></b><span lang="en-ca"><font size="3"><b>void print(int array[], int l, int level, int index)</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>This is a somewhat variant edition of previous implementation as I use an array to implement tree.</b></font></span></pre>
</div>
<div align="left">
  <pre><font size="3"><b>2. void sortStack(Stack&amp; stack)</b></font></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>I sort this stack by insert-sort with help of two extra stack. And stack has such a property that you</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>have to sort them in ascending and descending orders alternatively. That is once I sort part of stack</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>in ascending order, by increment length of stack by one, I have to insert the new element into stack</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>by descending order because I am moving data from one stack to another. That is why before return the</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>sorted stack I have to do an extra reversing-order job by help of a temporary stack.</b></font></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>กก</pre>
</div>
<pre>#include &lt;iostream&gt;

using namespace std;

const int Length=20;

int array[Length];

//print specific level of a binary tree
void print(int array[], int l, int level, int index);

struct Node
{
	int data;
	Node* next;
};

class Stack
{
private:
	Node* top;
public:
	Stack();
	~Stack();
	bool push(int num);
	bool pop(int&amp; num);
	bool empty() const;
	void print() const;
};
void doSort(Stack&amp; input, Stack&amp; output, int num, bool ascending);
void sortStack(Stack&amp; stack);
void copyStack(Stack&amp; source, Stack&amp; target);

int main()
{
	const int Length=10;
	/*
	for (int i=0; i&lt;Length; i++)
	{
		array[i]=rand()%100;
		cout&lt;&lt;&quot;  &quot;&lt;&lt;array[i];
	}
	cout&lt;&lt;&quot;\nthe array is like this\n&quot;;
	print(array, 0, 3, 0);
	*/
	Stack S;
//	int num;
	for (int i=0; i&lt;Length; i++)
	{
		S.push(rand()%100);
	}
	S.print();
	cout&lt;&lt;&quot;sorting...\n&quot;;
	sortStack(S);
	S.print();
	return 0;
}

void copyStack(Stack&amp; source, Stack&amp; target)
{
	int num;
	Stack temp;
	while (source.pop(num))
	{
		temp.push(num);
	}
	while (temp.pop(num))
	{
		target.push(num);
	}
}

void doSort(Stack&amp; input, Stack&amp; output, int num, bool ascending)
{
	int hold;
	bool inserted=false;
	while (input.pop(hold))
	{
		if (!inserted)
		{
			if (ascending&amp;&amp;num&gt;hold||!ascending&amp;&amp;num&lt;hold)
			{
				output.push(num);
				inserted=true;
			}
		}
		output.push(hold);
	}
	//not inserted, so place it at the end
	if (!inserted)
	{
		output.push(num);
	}

}


//require stack has at least one element
void sortStack(Stack&amp; stack)
{
	int num;
	Stack ascend, descend;
	stack.pop(num);
	descend.push(num);
	while (true)
	{
		if (stack.pop(num))
		{
			doSort(descend, ascend, num, true);
		}
		else
		{
			copyStack(descend, stack);
			break;
		}
		if (stack.pop(num))
		{
			doSort(ascend, descend, num, false);
		}
		else
		{
			copyStack(ascend, stack);
			break;
		}
	}
}

void Stack::print() const
{
	Node* temp=top;
	while (temp!=NULL)
	{
		cout&lt;&lt;temp-&gt;data&lt;&lt;&quot;  &quot;;
		temp=temp-&gt;next;
	}
	cout&lt;&lt;endl;
}



bool Stack::empty() const
{
	return top==NULL;
}

Stack::Stack()
{
	top=NULL;
}

Stack::~Stack()
{
	Node* temp=top;
	while (top!=NULL)
	{
		temp=top-&gt;next;
		delete top;
		top=temp;
	}
}


bool Stack::push(int num)
{
	Node* temp=top;
	top=new Node;
	top-&gt;data=num;
	top-&gt;next=temp;
	return true;
}

bool Stack::pop(int&amp; num)
{
	Node* temp=top;
	if (top==NULL)
	{
		return false;
	}
	else
	{
		num=top-&gt;data;
		top=top-&gt;next;
		delete temp;//even it is NULL
		return true;
	}
}


void print(int array[], int l, int level, int index)
{
	//index is like the pointer to test NULL
	if (index==Length)
	{
		return;
	}
	if (l==level)
	{
		cout&lt;&lt;&quot; &quot;&lt;&lt;array[index];
	}
	else
	{
		print(array, l+1, level, index*2+1);
		print(array, l+1, level, index*2+2);
	}
}


<font color="#0000FF"><b>Here is the result:</b></font></pre>
<pre> 64 62 58 78 24 69 0 34 67 41
sorting...
0 24 34 41 58 62 64 67 69 78
Press any key to continue</pre>
<pre></pre>
<pre>กก</pre>

<pre></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>