<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(1)</b></font></span></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">first edition of my practice of data structure, some of them are just a kind of copy of algorithms in </span></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>text. Actually not exact copying, I am trying to understand it by re-write by myself. Of course, for the unfamiliar</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>ones, I need to refer to text. </b></span><b><span lang="en-ca"> </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"><font size="3"><b>To write those data structure and  algorithms for practice. </b></font></span></pre>
</div>
<div align="left">
  <font FACE="CenturySchoolbook-BoldItalic" SIZE="3"><i><b></b></i></font>
</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 practice by myself. </b></span></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>void buildHeap(Link&lt;int&gt;**ptr, int size)</font></b></pre>
</div>
<div align="left">
  <pre><font size="3"><b><span lang="en-ca">2. </span>void heapSort(Link&lt;int&gt;**ptr, int size)</b></font></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>3. void siftDown(Link&lt;int&gt;**ptr, int index, int size)</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>These functions are used to build Max-heap and heap-sort.The boundary conditions are really painful.</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>4. void mergeSort(int array[], int size)</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>5. void doMergeSort(int array[], int temp[], int left, int right)</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>These are standard merge-sort algorithms. The boundary conditions are really painful.</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>6. AList&lt;int&gt;* eliminate(AList&lt;int&gt;* l1, AList&lt;int&gt;* l2)</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>7. AList&lt;int&gt;* eliminate2(AList&lt;int&gt;* l1, AList&lt;int&gt;* l2)</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>These are two ways to solve a problem proposed by professor during review class: Given two sorted list,</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>to generate a third list such that it contains all elements of two list that is not common existing </b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>in both list. The first way is quite an efficient way as it solves the problem in one shot and the </b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>new list is also in sorted manner. However, it is more complicated than I expected and cost me a couple </b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>of hours of debugging. It is impossible to figure out during exam. It is quite like merge operation</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>of merge sort as you always merge two list. But there is a little trick to see that if there are</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>repeating elements in two lists and when you encounter the common element in both list, you are supposed </b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>move current cursor of both list, then you have to make sure that the next repeated element won't be</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>selected!</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;
#include &lt;time.h&gt;
#include &quot;Link.h&quot;
#include &quot;AList.h&quot;

using namespace std;

const int Range=100;

const int MaxArraySize=35;

int array[MaxArraySize]={0};

Link&lt;int&gt;* list[MaxArraySize];

void displayList(Link&lt;int&gt;* ptr);

Link&lt;int&gt;* createList(int* array, int size);

Link&lt;int&gt;* reverseList(Link&lt;int&gt;* ptr);

void quksort(Link&lt;int&gt;** ptr, int size);

int partition(Link&lt;int&gt;**ptr, int l, int r);

void displayArray(Link&lt;int&gt;**ptr, int size);

void buildHeap(Link&lt;int&gt;**ptr, int size);

void swap(Link&lt;int&gt;** ptr, int x, int y);

void siftDown(Link&lt;int&gt;**ptr, int index, int size);

void heapSort(Link&lt;int&gt;**ptr, int size);

AList&lt;int&gt;* eliminate(AList&lt;int&gt;* l1, AList&lt;int&gt;* l2);

void getTwoList(AList&lt;int&gt;*&amp; l1, AList&lt;int&gt;*&amp; l2);

void createList(AList&lt;int&gt;*&amp; l, int array[], int size);

void displayList(AList&lt;int&gt;* lst);

void mergeSort(int array[], int size);

bool findVal(AList&lt;int&gt;* l, int key);

AList&lt;int&gt;* eliminate2(AList&lt;int&gt;* l1, AList&lt;int&gt;* l2);

int main()
{
	srand(time(0));
	//this is a strange combination, a link list with pointer also resided in
	//an sorted array
	int size=MaxArraySize;
	Link&lt;int&gt;* pHead;
	pHead=createList(array, size);
	displayList(pHead);
	cout&lt;&lt;&quot;try to reverse\n&quot;;
	pHead = reverseList(pHead);
	displayList(pHead);
	cout&lt;&lt;&quot;now try to build heap\n&quot;;
	buildHeap(list, size);
	heapSort(list, size);
	displayArray(list, size);
	//try quick sort of link list
	cout&lt;&lt;&quot;now try to sort the array, before sort\n&quot;;
	displayArray(list, size);
	quksort(list, size);

	displayArray(list, size);

	//this is a problem proposed by professor in review class:
	//given two lists to generate a third list without those common element of two
	//and keep two lists unchanged.
	AList&lt;int&gt;*l1, *l2, *l3;
	createList(l1, array, 10);
	cout&lt;&lt;&quot;l1 is:\n&quot;;
	displayList(l1);
	cout&lt;&lt;&quot;l2 is:\n&quot;;
	createList(l2, array, 15);
	displayList(l2);
	cout&lt;&lt;&quot;now l3 is:\n&quot;;
	l3=eliminate2(l1, l2);//this is second approach
	displayList(l3);
	return 0;
}

AList&lt;int&gt;* eliminate2(AList&lt;int&gt;* l1, AList&lt;int&gt;* l2)
{
	int i;
	AList&lt;int&gt;* l;

	l1-&gt;setStart();
	l2-&gt;setStart();
	l =new AList&lt;int&gt;(l1-&gt;rightLength()+l2-&gt;rightLength());
	while (l1-&gt;getValue(i))
	{
		if (!findVal(l2, i))
		{
			l-&gt;append(i);
		}
		l1-&gt;next();
	}

	l2-&gt;setStart();
	while (l2-&gt;getValue(i))
	{
		if (!findVal(l1, i))
		{
			l-&gt;append(i);
		}
		l2-&gt;next();
	}
	return l;
}



void createList(AList&lt;int&gt;*&amp; l, int array[], int size)
{
	l=new AList&lt;int&gt;;
	for (int i=0; i&lt;size; i++)
	{
		array[i]=rand()%Range;
	}
	mergeSort(array, size);
	for (i=0; i&lt;size; i++)
	{
		l-&gt;append(array[i]);
	}
}



void doMergeSort(int array[], int temp[], int left, int right)
{
	int mid= (left+right)/2;
	int n1, n2;
	if (left==right)
	{
		return;
	}
	doMergeSort(array, temp, left, mid);
	doMergeSort(array, temp, mid+1, right);
	for (int i=left; i&lt;=right; i++)
	{
		temp[i]=array[i];
	}
	n1=left; 
	n2=mid+1;
	for (i=left; i&lt;=right; i++)
	{
		if (n1==mid+1)
		{
			array[i]=temp[n2];
			n2++;
		}
		else
		{
			if (n2==right+1)
			{
				array[i]=temp[n1];
				n1++;
			}
			else
			{
				if (temp[n1]&lt;temp[n2])
				{
					array[i]=temp[n1];
					n1++;
				}
				else
				{
					array[i]=temp[n2];
					n2++;
				}
			}
		}
	}
}

void mergeSort(int array[], int size)
{
	void doMergeSort(int array[], int temp[], int left, int right);
	int temp[MaxArraySize];
	doMergeSort(array, temp, 0, size-1);
}

void displayList(AList&lt;int&gt;* lst)
{
	int i;
	lst-&gt;setStart();
	while (lst-&gt;getValue(i))
	{
		cout&lt;&lt;i&lt;&lt;&quot;  &quot;;
		lst-&gt;next();
	}
	cout&lt;&lt;endl;
}


void getTwoList(AList&lt;int&gt;*&amp; l1, AList&lt;int&gt;*&amp; l2)
{
	l1=new AList&lt;int&gt;;
	l2=new AList&lt;int&gt;;
	for (int i=0; i&lt;10; i++)
	{
		l1-&gt;append(rand()%100);
		l2-&gt;append(rand()%100);
	}
	//make l1 longer
	for (i=0; i&lt;5; i++)
	{
		l1-&gt;append(rand()%100);
	}
}


bool findVal(AList&lt;int&gt;* l, int key)
{
	int i;
	l-&gt;setStart();
	while (l-&gt;getValue(i))
	{
		if (key==i)
		{
			return true;
		}
		else
		{
			if (key&lt;i)
			{
				return false;
			}
		}
		l-&gt;next();
	}
	return false;
}


int min(int i, int j)
{
	if (i&lt;j)
	{
		return i;
	}
	else
	{
		return j;
	}
}

//the idea is quite similar to merge operation of merge sort.
AList&lt;int&gt;* eliminate(AList&lt;int&gt;* l1, AList&lt;int&gt;* l2)
{

	int min(int i, int j);

	bool leftDone=false;
	int i, j, same;

	//assume both list has at least one element
	l1-&gt;setStart();
	l2-&gt;setStart();

	AList&lt;int&gt;* l =new AList&lt;int&gt;(l1-&gt;rightLength()+l2-&gt;rightLength());
	l-&gt;setStart();

	l1-&gt;getValue(i);
	l2-&gt;getValue(j);
	while (true)
	{	
		//if they are not equal, so it should be added, 
		//but the only smaller one we are sure, as the smaller 
		//has no more chance to get equal from other list.
		//And we push the list with the smaller one to 
		//get more value. 
		if (i!=j)
		{
			if (i&lt;j)
			{
				if (i!=same)
				{
					l-&gt;append(i);
				}
				l1-&gt;next();//keep left moving
				//also push the ball going always from the smaller side
				if (!l1-&gt;getValue(i))
				{
					leftDone=true;//book keeping which side makes the while loop end
					break;
				}
			
			}
			else
			{
				if (j!=same)
				{
					l-&gt;append(j);
				}
				l2-&gt;next();//keep right moving
				if (!l2-&gt;getValue(j))
				{
					leftDone=false; //make sure it is false
					break;				
				}
				
			}
		}
		else
		{
			same=i;//same just to record the last same number
			//in case they get same value, we know, we have to push both side going
			l1-&gt;next();
			l2-&gt;next();
			if (!l1-&gt;getValue(i))
			{
				leftDone=true;//book keeping which side makes the while loop end
				break;
			}
			if (!l2-&gt;getValue(j))
			{
				leftDone=false;
				break;
			}
		
		}
	}
	if (leftDone)
	{
		if (i!=j)//this means last time, we have not insert j.
		{
			l-&gt;append(j);			
		}
		l2-&gt;next();
		while (l2-&gt;getValue(j))
		{
			if (i!=j)//this means last time, we have not insert j.
			{
				l-&gt;append(j);			
			}
			l2-&gt;next();
		}
	}
	else
	{
		if (i!=j)//this means last time, we have not insert i.
		{
			l-&gt;append(i);			
		}
		l1-&gt;next();//we need move even i==j
		while (l1-&gt;getValue(i))
		{
			if (i!=j)//this means last time, we have not insert j.
			{
				l-&gt;append(i);			
			}
			l1-&gt;next();
		}
	}
	return l;
}


void heapSort(Link&lt;int&gt;**ptr, int size)
{
	for (int i=size-1; i&gt;0; i--)
	{
		swap(ptr, i, 0);
		siftDown(ptr, 0, i);
	}
}


void buildHeap(Link&lt;int&gt;**ptr, int size)
{
	for (int i=size/2-1; i&gt;=0; i--)
	{
		siftDown(ptr, i, size);
	}
}

void siftDown(Link&lt;int&gt;**ptr, int index, int size)
{
	int l, r;
	l=index*2+1;
	r=index*2+2;
	if (size==1)
	{
		return;
	}
	if (r&lt;size)
	{
		if (ptr[l]-&gt;element&lt;ptr[r]-&gt;element)
		{
			l=r;	
		}
	}
	if (ptr[index]-&gt;element&lt;ptr[l]-&gt;element)
	{
		swap(ptr, index, l);
		if (l&lt;=size/2-1)
		{
			siftDown(ptr, l, size);
		}
	}

}

void displayArray(Link&lt;int&gt;**ptr, int size)
{
	for (int i=0; i&lt;size; i++)
	{
		cout&lt;&lt;ptr[i]-&gt;element&lt;&lt;&quot;  &quot;;
	}
	cout&lt;&lt;endl;
}

void doQukSort(Link&lt;int&gt;** ptr, int l, int r)
{
	if (r&gt;l)
	{
		int mid;
		mid=partition(ptr, l, r);
		doQukSort(ptr, 0, mid-1);
		doQukSort(ptr, mid+1, r);
	}
}

void quksort(Link&lt;int&gt;** ptr, int size)
{
	void doQukSort(Link&lt;int&gt;** ptr, int l, int r);

	doQukSort(ptr, 0, size-1);
}

void swap(Link&lt;int&gt;** ptr, int x, int y)
{
	Link&lt;int&gt;* temp=ptr[x];
	ptr[x]=ptr[y]; 
	ptr[y]=temp;
}

int partition(Link&lt;int&gt;** ptr, int start, int end)
{
	
	Link&lt;int&gt;* pivot=ptr[end];
	int l=-1, r=end-1;
	while (r&gt;l)
	{
		do 
		{
			l++;
		} while (l&lt;end&amp;&amp;ptr[l]-&gt;element&lt;=pivot-&gt;element);
		do 
		{
			r--;
		} while (r&gt;=0&amp;&amp;ptr[r]-&gt;element&gt;=pivot-&gt;element);
		swap(ptr, l, r);
	}
	swap(ptr, l, r);
	swap(ptr, l, end);
	return l;
}



void displayList(Link&lt;int&gt;* ptr)
{
	Link&lt;int&gt;* temp=ptr;
	while (temp!=NULL)
	{
		cout&lt;&lt;temp-&gt;element&lt;&lt;&quot;  &quot;;
		temp=temp-&gt;next;
	}
	cout&lt;&lt;endl;
}

Link&lt;int&gt;* reverseList(Link&lt;int&gt;* ptr)
{
	Link&lt;int&gt;* head,* pre=NULL, *nxt;
	if (ptr==NULL)
	{
		cout&lt;&lt;&quot;empty list!\n&quot;;
		return NULL;
	}
	head=ptr-&gt;next;
	if (head==NULL)
	{
		return ptr;
	}
	nxt=head-&gt;next;
	pre=ptr;
	head-&gt;next=pre;
	pre-&gt;next=NULL;

	while (nxt!=NULL)
	{
		pre=head;
		head=nxt;
		nxt=nxt-&gt;next;
		head-&gt;next=pre;
	}
	return head;
}

Link&lt;int&gt;* createList(int* array, int size)
{
	Link&lt;int&gt;* head, *ptr, *temp;
	if (size&lt;=0)
	{
		return NULL;
	}

	for (int i=0; i&lt;size; i++)
	{
		array[i]=rand()%100;
		ptr =new Link&lt;int&gt;(array[i]);
		if (i==0)
		{
			head=ptr;
		}
		else
		{
			temp-&gt;next=ptr;		
		}
		temp = ptr;
		list[i]=ptr;
	}
	return head;
}

</pre>
<pre><font color="#0000FF"><b>Here is the result:</b></font></pre>
<pre>84 58 63 43 68 19 13 12 21 3 26 78 27 60 85 29 77 57 14 59 28 27 56 70 38 2
4 47 46 84 74 6 47 73 13 4
try to reverse
4 13 73 47 6 74 84 46 47 24 38 70 56 27 28 59 14 57 77 29 85 60 27 78 26 3
21 12 13 19 68 43 63 58 84
now try to build heap
3 4 6 12 13 13 14 19 21 24 26 27 27 28 29 38 43 46 47 47 56 57 58 59 60 63
68 70 73 74 77 78 84 84 85
now try to sort the array, before sort
3 4 6 12 13 13 14 19 21 24 26 27 27 28 29 38 43 46 47 47 56 57 58 59 60 63
68 70 73 74 77 78 84 84 85
3 4 6 12 13 13 14 19 21 24 26 27 27 28 29 38 43 46 47 47 56 57 58 59 60 63
68 70 73 74 77 78 84 84 85
l1 is:
3 7 16 18 37 51 53 64 77 79
l2 is:
2 9 19 31 37 40 52 55 59 64 85 91 91 95 97
now l3 is:
3 7 16 18 51 53 77 79 2 9 19 31 40 52 55 59 85 91 91 95 97
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>