<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>Symple <span lang="en-ca">Shuffle</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 </span>second<span lang="en-ca"> edition of assignment of shuffle which is quite different from <a href="Poke.htm">what I write by myself before</a></span> </b></pre>
</div>
<div align="left">
  <pre><b>which is a dealing program<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><b>T</b><span lang="en-ca"><b>he problem is to use the methods specified in the base abstract class List which implemented in two derived class</b></span></pre>
  <pre><span lang="en-ca"><b>---AList, LList. They are array-based and linklist-based implementation class. </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>This is a simplified version of my original assignment.</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><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>
<div align="left">
  <pre><b><font color="#FF0000"><span lang="en-ca">//List.h The base abstract class</span></font></b></pre>
</div>
<pre>#ifndef LIST_H
#define LIST_H
// List abstract class
const int DefaultListSize=52;
template &lt;class Elem&gt; class List {
public:
  // Reinitialize the list.  The client is responsible for
  // reclaiming the storage used by the list elements.
  virtual void clear() = 0;
  // Insert an element at the front of the right partition.
  // Return true if successful, false if the list is full.
  virtual bool insert(const Elem&amp;) = 0;
  // Append an element at the end of the right partition.
  // Return true if successful, false if the list is full.
  virtual bool append(const Elem&amp;) = 0;
  // Remove the first element of right partition. Return
  // true if successful, false if right partition is empty.
  // The element removed is returned in the parameter.
  virtual bool remove(Elem&amp;) = 0;
  // Place fence at list start, making left partition empty
  virtual void setStart() = 0;
  // Place fence at list end, making right partition empty
  virtual void setEnd() = 0;
  // Move fence one step left; no change if already at start
  virtual void prev() = 0;
  // Move fence one step right; no change if already at end
  virtual void next() = 0;
  // Return length of left partition
  virtual int leftLength() const = 0;
  // Return length of right partition
  virtual int rightLength() const = 0;
  // If pos or more elements are in the list, set the size
  // of left partition to pos and return true.  Otherwise,
  // do nothing and return false.
  virtual bool setPos(int pos) = 0;
  // Return in first parameter the first element  of the
  // right partition.  Return true if successful, false
  // if the right partition is empty.
  virtual bool getValue(Elem&amp;) const = 0;
  // Print the contents of the list
  virtual void print() const = 0;
};

#endif</pre>
<pre>กก</pre>
<pre><b><font color="#FF0000"><span lang="en-ca">//AList.h  The array-based derived class</span></font></b></pre>
<pre>#ifndef ALIST_H
#define ALIST_H

// This is the file to include in your code if you want access to the
// complete AList template class

// First, get the declaration for the base list class
#include &quot;list.h&quot;

template &lt;class Elem&gt; // Array-based list implementation
class AList : public List&lt;Elem&gt; {
private:
  int maxSize;        // Maximum size of list
  int listSize;       // Actual number of elements in list
  int fence;          // Position of fence
  Elem* listArray;    // Array holding list elements
public:
  AList(int size=DefaultListSize) { // Constructor
    maxSize = size;
    listSize = fence = 0;
    listArray = new Elem[maxSize];
  }
  ~AList() { delete [] listArray; } // Destructor
  void clear() {
    delete [] listArray;
    listSize = fence = 0;
    listArray = new Elem[maxSize];
  }
  bool insert(const Elem&amp;);
  bool append(const Elem&amp;);
  bool remove(Elem&amp;);
  void setStart() { fence = 0; }
  void setEnd()   { fence = listSize; }
  void prev()     { if (fence != 0) fence--; }
  void next()     { if (fence &lt;= listSize) fence++; }
  int leftLength() const  { return fence; }
  int rightLength() const { return listSize - fence; }
  bool setPos(int pos) {
    if ((pos &gt;= 0) &amp;&amp; (pos &lt;= listSize)) fence = pos;
    return (pos &gt;= 0) &amp;&amp; (pos &lt;= listSize);
  }
  bool getValue(Elem&amp; it) const {
    if (rightLength() == 0) return false;
    else { it = listArray[fence]; return true; }
  }
  void print() const {
    int temp = 0;
    cout &lt;&lt; &quot;&lt; &quot;;
    while (temp &lt; fence) cout &lt;&lt; listArray[temp++] &lt;&lt; &quot; &quot;;
    cout &lt;&lt; &quot;| &quot;;
    while (temp&lt;listSize) cout &lt;&lt; listArray[temp++] &lt;&lt; &quot; &quot;;
    cout &lt;&lt; &quot;&gt;\n&quot;;
  }
};

template &lt;class Elem&gt; // Insert at front of right partition
bool AList&lt;Elem&gt;::insert(const Elem&amp; item) {
  if (listSize == maxSize) return false; // List is full
  for(int i=listSize; i&gt;fence; i--)      // Shift Elems up
    listArray[i] = listArray[i-1];       //   to make room
  listArray[fence] = item;
  listSize++;                       // Increment list size
  return true;
}

template &lt;class Elem&gt; // Append Elem to end of the list
bool AList&lt;Elem&gt;::append(const Elem&amp; item) {
  if (listSize == maxSize) return false;
  listArray[listSize++] = item;
  return true;
}

// Remove and return first Elem in right partition
template &lt;class Elem&gt; bool AList&lt;Elem&gt;::remove(Elem&amp; it) {
  if (rightLength() == 0) return false; // Nothing in right
  it = listArray[fence];                // Copy removed Elem
  for(int i=fence; i&lt;listSize-1; i++)   // Shift them down
    listArray[i] = listArray[i+1];
  listSize--;                           // Decrement size
  return true;
}

#endif</pre>
<pre><b><font color="#FF0000"><span lang="en-ca">//Link.h  A helper class for linklist</span></font></b></pre>
<pre>#ifndef LINK_H
#define LINK_H

// Singly-linked list node
template &lt;class Elem&gt; class Link {
public:
  Elem element;      // Value for this node
  Link *next;        // Pointer to next node in list
  Link(const Elem&amp; elemval, Link* nextval =NULL)
    { element = elemval;  next = nextval; }
  Link(Link* nextval =NULL) { next = nextval; }
};
#endif</pre>
<pre><b><font color="#FF0000"><span lang="en-ca">//LList.h Linklist-based derived class</span></font></b></pre>
<pre>#ifndef LLIST_H
#define LLIST_H

// This is the file to include in your code if you want access to the
// complete LList template class

// First, get the declaration for the base list class
#include &quot;list.h&quot;

// Linked list implementation
template &lt;class Elem&gt; class LList: public List&lt;Elem&gt; {
private:
  Link&lt;Elem&gt;* head;       // Pointer to list header
  Link&lt;Elem&gt;* tail;       // Pointer to last Elem in list 
  Link&lt;Elem&gt;* fence;      // Last element on left side
  int leftcnt;            // Size of left partition
  int rightcnt;           // Size of right partition
  void init() {           // Intialization routine
    fence = tail = head = new Link&lt;Elem&gt;;
    leftcnt = rightcnt = 0;
  }
  void removeall() {   // Return link nodes to free store
    while(head != NULL) {
      fence = head;
      head = head-&gt;next;
      delete fence;
    }
  }
public:
  LList(int size=DefaultListSize) { init(); }
  ~LList() { removeall(); }  // Destructor
  void clear() { removeall(); init(); }
  bool insert(const Elem&amp;);
  bool append(const Elem&amp;);
  bool remove(Elem&amp;);
  void setStart()
    { fence = head; rightcnt += leftcnt; leftcnt = 0; }
  void setEnd()
    { fence = tail; leftcnt += rightcnt; rightcnt = 0; }
  void prev();
  void next() {
    if (fence != tail) // Don't move fence if right empty
      { fence = fence-&gt;next; rightcnt--; leftcnt++; }
  }
  int leftLength() const  { return leftcnt; }
  int rightLength() const { return rightcnt; }
  bool setPos(int pos);
  bool getValue(Elem&amp; it) const {
    if(rightLength() == 0) return false;
    it = fence-&gt;next-&gt;element;
    return true;
  }
  void print() const;
};

template &lt;class Elem&gt; // Insert at front of right partition
bool LList&lt;Elem&gt;::insert(const Elem&amp; item) {
  fence-&gt;next = new Link&lt;Elem&gt;(item, fence-&gt;next);  
  if (tail == fence) tail = fence-&gt;next;  // New tail
  rightcnt++;
  return true;
}

template &lt;class Elem&gt; // Append Elem to end of the list
bool LList&lt;Elem&gt;::append(const Elem&amp; item) {
  tail = tail-&gt;next = new Link&lt;Elem&gt;(item, NULL);
  rightcnt++;
  return true;
}

// Remove and return first Elem in right partition
template &lt;class Elem&gt; bool LList&lt;Elem&gt;::remove(Elem&amp; it) {
  if (fence-&gt;next == NULL) return false; // Empty right
  it = fence-&gt;next-&gt;element;       // Remember value
  Link&lt;Elem&gt;* ltemp = fence-&gt;next; // Remember link node
  fence-&gt;next = ltemp-&gt;next;       // Remove from list
  if (tail == ltemp) tail = fence; // Reset tail
  delete ltemp;                    // Reclaim space
  rightcnt--;
  return true;
}

// Move fence one step left; no change if left is empty
template &lt;class Elem&gt; void LList&lt;Elem&gt;::prev() {
  Link&lt;Elem&gt;* temp = head;
  if (fence == head) return; // No previous Elem
  while (temp-&gt;next!=fence) temp=temp-&gt;next;
  fence = temp;
  leftcnt--; rightcnt++;
}

// Set the size of left partition to pos
template &lt;class Elem&gt; bool LList&lt;Elem&gt;::setPos(int pos) {
  if ((pos &lt; 0) || (pos &gt; rightcnt+leftcnt)) return false;
  fence = head;
  for(int i=0; i&lt;pos; i++) fence = fence-&gt;next;
  return true;
}

template &lt;class Elem&gt; void LList&lt;Elem&gt;::print() const {
  Link&lt;Elem&gt;* temp = head;
  cout &lt;&lt; &quot;&lt; &quot;;
  while (temp != fence) {
    cout &lt;&lt; temp-&gt;next-&gt;element &lt;&lt; &quot; &quot;;
    temp = temp-&gt;next;
  }
  cout &lt;&lt; &quot;| &quot;;
  while (temp-&gt;next != NULL) {
    cout &lt;&lt; temp-&gt;next-&gt;element &lt;&lt; &quot; &quot;;
    temp = temp-&gt;next;
  }
  cout &lt;&lt; &quot;&gt;\n&quot;;
}

#endif</pre>
<pre><b><font color="#FF0000"><span lang="en-ca">//Shuffle.cpp  The implementation of shuffle class and driver program</span></font></b></pre>
<pre>#include &quot;List.h&quot;
#include &quot;link.h&quot;
#include &quot;Alist.h&quot;
#include &quot;LList.h&quot;
#include &lt;iostream&gt;

using namespace std;

enum ListType
{ArrayList, LinkList};

char* suitString[4] = {&quot;spade&quot;, &quot;heart&quot;, &quot;diamond&quot;, &quot;club&quot;};


AList&lt;char*&gt; aSource;
AList&lt;char*&gt; aLeft;
AList&lt;char*&gt; aRight;

LList&lt;char*&gt; lSource;
LList&lt;char*&gt; lLeft;
LList&lt;char*&gt; lRight;

void initialize(ListType listType= ArrayList);
void splitDeck(ListType listType=ArrayList);
void shuffling(ListType listType=ArrayList, bool toShow=false);
void shuffle();

int main()
{
	shuffle();
	return 0;
}

void shuffle()
{
	initialize();
	cout&lt;&lt;&quot;This is for arrayList\n&quot;;
	for (int i=0; i&lt;6; i++)
	{
		cout&lt;&lt;&quot;\nbefore no.&quot;&lt;&lt;i+1&lt;&lt;&quot;times shuffle:&quot;;
		aSource.print();
		if (i==0)
		{
			shuffling(ArrayList, true);
		}
		else
		{
			shuffling(ArrayList, false);
		}
	}
	cout&lt;&lt;&quot;\nafter all shuffling\n&quot;;
	aSource.print();
	cout&lt;&lt;&quot;\nThis is for linkList\n&quot;;
	initialize(LinkList);
	for (i=0; i&lt;6; i++)
	{
		cout&lt;&lt;&quot;\nbefore no.&quot;&lt;&lt;i+1&lt;&lt;&quot;times shuffle:&quot;;
		lSource.print();
		if (i==0)
		{
			shuffling(LinkList, true);
		}
		else
		{
			shuffling(LinkList, false);
		}
	}
	cout&lt;&lt;&quot;\nafter all shuffling\n&quot;;
	lSource.print();
}

void shuffling(ListType listType, bool toShow)
{
	int number;
	bool leftEmpty=false, rightEmpty =false;
	List&lt;char*&gt;* ptr;
	List&lt;char*&gt;* pLeft;
	List&lt;char*&gt;* pRight;

	char* str;
	if (listType==ArrayList)
	{
		ptr = &amp;aSource;
		pLeft = &amp;aLeft;
		pRight = &amp;aRight;
	}
	else
	{
		ptr = &amp;lSource;
		pLeft = &amp;lLeft;
		pRight = &amp;lRight;
	}
	splitDeck(listType);

	while (!leftEmpty||!rightEmpty)
	{
		if (toShow)
		{
			cout&lt;&lt;&quot;\nresult before shuffle:&quot;;
			ptr-&gt;print();
			cout&lt;&lt;endl;

			cout&lt;&lt;&quot;\nleft before shuffle:&quot;;
			pLeft-&gt;print();
			cout&lt;&lt;endl;
		}
		number = rand()%6;
		for (int i=0; i&lt;number; i++)
		{
			if (pLeft-&gt;remove(str))
			{
				ptr-&gt;append(str);
			}
			else
			{
				leftEmpty = true;
				break;
			}
		}
		number = rand()%6;
		if (toShow)
		{
			cout&lt;&lt;&quot;\nright before shuffle:&quot;;
			pRight-&gt;print();
			cout&lt;&lt;endl;
		}
		for (i=0; i&lt;number; i++)
		{
			if (pRight-&gt;remove(str))
			{
				ptr-&gt;append(str);
			}
			else
			{
				rightEmpty = true;
				break;
			}
		}
		
	}

}


void  fillSuit(int suit, char* str, ListType listType)
{
	char buffer[20];
	char* ptr;
	strcpy(buffer, str);
	strcat(buffer, suitString[suit]);
	ptr = new char[strlen(buffer)+1];
	if (listType==ArrayList)
	{
		aSource.append(strcpy(ptr, buffer));
	}
	else
	{
		lSource.append(strcpy(ptr, buffer));
	}
}


void splitDeck(ListType listType)
{
	List&lt;char*&gt;* ptr;
	List&lt;char*&gt;* pLeft;
	List&lt;char*&gt;* pRight;

	char* str;
	if (listType==ArrayList)
	{
		ptr = &amp;aSource;
		pLeft = &amp;aLeft;
		pRight = &amp;aRight;
	}
	else
	{
		ptr = &amp;lSource;
		pLeft = &amp;lLeft;
		pRight = &amp;lRight;
	}
	ptr-&gt;setStart();
	for (int i=0; i&lt;52; i++)
	{
		ptr-&gt;remove(str);
		if (i&lt;26)
		{		
			pLeft-&gt;append(str);
		}
		else
		{
			pRight-&gt;append(str);
		}
	}
}
	


void initialize(ListType listType)
{
	void fillSuit(int suit, char* str, ListType listType);
	char buffer[10];
	for (int suit=0; suit&lt;4; suit++)
	{
		for (int card=0; card&lt;13; card++)
		{
		
			switch(card)
			{
			case 0:
				fillSuit(suit, &quot;Ace &quot;, listType);
				break;
			case 1:
				fillSuit(suit, &quot;King &quot;, listType);
				break;
			case 2:
				fillSuit(suit, &quot;Queen &quot;, listType);
				break;
			case 3:
				fillSuit(suit, &quot;Jack &quot;, listType);
				break;
			default:
				itoa(14 - card, buffer, 10);
				fillSuit(suit, buffer, listType);
				break;
			}
		}
	}
}

</pre>
<pre>
</pre>
<pre>กก</pre>
<pre><span lang="en-ca"><a name="result"></a><font size="3" color="#FF0000">The result is like following:</font></span></pre>
<pre><font size="3" color="#FF0000"><span lang="en-ca">I omitted all intermediate result as it is too many, if you really like to check</span>.</font></pre>
<pre><span lang="en-ca"><font size="3" color="#FF0000">......</font></span></pre>
<p>before no.2times shuffle:&lt; | Ace spade King spade Queen spade Jack spade Ace 
diamond King diamond Queen diamond Jack diamond 10spade 9spade 8spade 10diamond 
9diamond 8diamond 7diamond 6diamond 7spade 5diamond 4diamond 3diamond 6spade 
2diamond Ace club King club Queen club Jack club 5spade 4spade 3spade 10club 
9club 8club 2spade Ace heart King heart Queen heart Jack heart 7club 6club 5club 
4club 3club 10heart 2club 9heart 8heart 7heart 6heart 5heart 4heart 3heart 
2heart &gt;<br>
<br>
before no.3times shuffle:&lt; | Ace spade King spade Queen spade 5spade 4spade 
3spade 10club 9club 8club 2spade Ace heart King heart Queen heart Jack heart 
7club Jack spade Ace diamond King diamond Queen diamond 6club 5club 4club 3club 
10heart 2club 9heart 8heart 7heart 6heart Jack diamond 10spade 5heart 4heart 
3heart 9spade 2heart 8spade 10diamond 9diamond 8diamond 7diamond 6diamond 7spade 
5diamond 4diamond 3diamond 6spade 2diamond Ace club King club Queen club Jack 
club &gt;<br>
<br>
before no.4times shuffle:&lt; | 8heart 7heart 6heart Jack diamond 10spade Ace spade 
5heart 4heart 3heart 9spade King spade Queen spade 5spade 4spade 3spade 2heart 
8spade 10club 9club 8club 2spade Ace heart 10diamond 9diamond 8diamond King 
heart Queen heart 7diamond 6diamond 7spade 5diamond 4diamond Jack heart 3diamond 
6spade 7club Jack spade Ace diamond King diamond 2diamond Ace club King club 
Queen diamond 6club Queen club Jack club 5club 4club 3club 10heart 2club 9heart 
&gt;<br>
<br>
before no.5times shuffle:&lt; | 8heart Queen heart 7diamond 6diamond 7heart 6heart 
Jack diamond 10spade 7spade 5diamond 4diamond Jack heart 3diamond Ace spade 
5heart 6spade 7club 4heart 3heart Jack spade Ace diamond King diamond 2diamond 
Ace club 9spade King spade Queen spade 5spade 4spade King club Queen diamond 
3spade 6club 2heart 8spade 10club 9club Queen club Jack club 5club 4club 8club 
2spade 3club 10heart 2club 9heart Ace heart 10diamond 9diamond 8diamond King 
heart &gt;<br>
<br>
before no.6times shuffle:&lt; | 8heart Queen heart 7diamond Queen spade 5spade 
4spade King club 6diamond 7heart 6heart Jack diamond Queen diamond 3spade 6club 
10spade 7spade 5diamond 4diamond 2heart 8spade 10club 9club Queen club Jack 
heart 3diamond Ace spade 5heart Jack club 5club 6spade 7club 4heart 3heart Jack 
spade 4club 8club 2spade 3club 10heart Ace diamond King diamond 2diamond Ace 
club 9spade King spade 2club 9heart Ace heart 10diamond 9diamond 8diamond King 
heart &gt;<br>
<br>
after all shuffling<br>
&lt; | 8heart Queen heart 7diamond Queen spade 5spade 4spade King club 6diamond 
7heart 5heart Jack club 5club 6spade 7club 6heart Jack diamond Queen diamond 
3spade 6club 4heart 3heart 10spade 7spade Jack spade 4club 5diamond 4diamond 
2heart 8club 8spade 10club 9club Queen club Jack heart 3diamond Ace spade 2spade 
3club 10heart Ace diamond King diamond 2diamond Ace club 9spade King spade 2club 
9heart Ace heart 10diamond 9diamond 8diamond King heart &gt;<br>
</p>

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