<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-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Container</title>
</head>

<body>

<div align="center">
  <center>
  <pre><b><font color="#FF0000" size="6">Container---<span lang="zh-cn">二维</span>的动态数组</font></b></pre>
  </center>
</div>

<div align="left">
  <pre><b><font color="#FF0000" size="5">A.第<span lang="zh-cn">二</span>版</font></b></pre>
</div>
<div align="left">
  <pre><b>这个小程序<span lang="zh-cn">应该</span>是<span lang="zh-cn">动态数组的第二个</span>版本<span lang="zh-cn">吧，因为，我所作的并不多，只是加了一个极其简单的remove的方法</span>。<span lang="zh-cn">有对模板类作了两个子类，分别是</span></b></pre>
</div>
<div align="left">
  <pre><span lang="zh-cn"><b>整数子类IntList和整数子类的指针所组成的子类PtrList。这样就可以组成一个动态二维数组，而且每个数组都是动态的不定长度，你知道这个东</b></span></pre>
</div>
<div align="left">
  <pre><span lang="zh-cn"><b>西有什么用吗？我还没有想好，不过，我是经常很讨厌为不定长度的多个数据用多维数组预分配巨大的内存的，同时，这个冬冬实际上可以作为搜索</b></span></pre>
</div>
<div align="left">
  <pre><span lang="zh-cn"><b>的存储，比如BFS,或DFS，你总要来存，可是子节点长度不一，不是很讨厌吗？不过，话说回来了，那个时候，用这种复杂的类浪费内存可能比预分</b></span></pre>
</div>
<div align="left">
  <pre><span lang="zh-cn"><b>配数组浪费的还要多。反正我也不知道为什么要写他，就算为了好玩吧，我现在什么东西都写不出来，实在很郁闷！</b></span></pre>
</div>
<div align="left">
  <pre><b>1。 程序基本说明：学计算机的人应该都用过数组吧，如果是静态数组，长度是固定的，一旦元素个数超过就麻烦了，而且数组元素类型是声</b></pre>
</div>
<div align="left">
  <pre><b>	明的时候确定的实在不方便。Delphi里的TList还有排序的功能，每添加一个新元素自动按照顺序进行插入，非常的方便。本程序就是</b></pre>
</div>
<div align="left">
  <pre><b>	采用模板技术的类，使得类型灵活，数组长度动态增加，不受限制，增加查找功能，新增元素自动按照升序排列自动进行插入动作。</b></pre>
</div>
<div align="left">
  <pre><b>2。 程序思路：我以前在正航时候看过Delphi的TList的源代码，其实不复杂，只不过，每次增加长度时候，Delphi增加的是2的n次方。<span lang="zh-cn">模板类</span></b></pre>
</div>
<div align="left">
  <pre><span lang="zh-cn"><b>	继承的时候，必须传递子类的类型参数给父类，否则，你说教编译器怎么编法？这个道理，我写继承的时候才体会到的，算是收获吧。</b></span></pre>
</div>
<div align="left">
  <pre><b>3。 主要函数介绍：</b></pre>
</div>
<div align="left" style="width: 892; height: 102">
  <pre><b>	A. <span lang="zh-cn">这个函数是做什么用的？你知道数组里的元素如果是复杂的类或结构，在删除的时候，应该释放内存，这个就是做这个工作的地方了</span>。</b></pre>
  <pre>	virtual void onRemove(T ptr); //
</pre>
  <blockquote>
    <pre><b><span lang="zh-cn">   </span>B. <span lang="zh-cn">你想显示每个元素吗？那么在访问每个元素作显示的时候，具体显示什么就是在这里定义的</span>。</b></pre>
    <pre><span lang="zh-cn">   </span>virtual void onDisplay(T ptr); //</pre>
    <pre><b><span lang="zh-cn">   C</span>. <span lang="zh-cn">这是子类container的遍历所有子节点方法</span>。</b></pre>
    <pre><span lang="zh-cn">   void Container::traverse()</span></pre>
  </blockquote>
  <pre><b>4。 不足之处：</b></pre>
  <pre><b>	A. <span lang="zh-cn">要能够继续嵌套，以便超过二维，达到无限维</span>。</b></pre>
  <pre><b>	B. <span lang="zh-cn">原来的基类里元素采用自然的大小比较，结果加一个指针进去，他去比较指针地址的大小，闹得很搞笑，结果加的元素位置乱七八糟。</span></b></pre>
  <pre><span lang="zh-cn"><b>	不过，也没有犯什么错就是了。不行，下个版本一定要让用户定义自己的比较大小的方法，类似回调函数那种。</b></span></pre>
</div>
<pre>#include &lt;iostream&gt;
#include &lt;iomanip&gt;

using namespace std;

template&lt;class T&gt;
class mylist
{
private:
	T *flist;
	int LENGTH;
	int SIZE;
	int counter;
protected:
	void uninitialize();
	void initialize();
	bool checksize();
	void expand();
	int locate(T ptr);
	virtual void onRemove(T ptr);  //
	virtual void onDisplay(T ptr);  //derived class must implement their own methods

public:	
	mylist();
	~mylist();
	void add(T ptr);
	bool find(T ptr);
	void display();
	int count();
	T&amp; items(int index);
	void insert(int index, T ptr);
	void remove(int index);
};


class IntList: public mylist&lt;int&gt;
{
protected:
	void onDisplay(int ptr);
};
	
class PtrList: public mylist&lt;IntList*&gt;
{
protected:
	void onRemove(IntList* ptr);
};

class Container
{
private:
	void onVisitItem(int data);
	void onVisitNode(IntList* ptr);
	PtrList lst;
public:
	int nodeCount();
	int itemCount(int node);
	void addItem(int node, int data);
	void addNode();
	int getItem(int node, int index);
	void eachNode();
	void eachItem(int node);
	void traverse();
};



int main()
{
	int array[20] = {21,31,24,51,42,3,23,5,45,6567,76,88,99,11,234, 3435,566,78,56,20};
	Container R;
	int count=0;
	for (int i=0; i&lt;4; i++)
	{
		int bound;
		array[i]=i;
		R.addNode();
		bound = rand()%15;
		for (int j=0; j&lt;bound; j++)
		{	
			count++;
			if (count==20)
			{
				break;
			}
			R.addItem(0, array[count]);
		}
	}
	R.traverse();

	return 0;
}

void Container::traverse()
{
	for (int i=0; i&lt;lst.count(); i++)
	{
		eachItem(i);
	}
}

void Container::eachItem(int node)
{
	cout&lt;&lt;&quot;node is:&quot;&lt;&lt;node&lt;&lt;endl;
	cout&lt;&lt;&quot;and itemcount is:&quot;&lt;&lt;lst.items(node)-&gt;count()&lt;&lt;endl;
	onVisitNode(lst.items(node));
}

void Container::onVisitItem(int data)
{
	cout&lt;&lt;data&lt;&lt;endl;
}

void Container::onVisitNode(IntList* ptr)
{
	for (int i=0; i&lt; ptr-&gt;count(); i++)
	{		
		onVisitItem(ptr-&gt;items(i));
	}
}

int Container::itemCount(int node)
{
	return lst.items(node)-&gt;count();
}

int Container::nodeCount()
{
	return lst.count();
}

void PtrList::onRemove(IntList* ptr)
{
	delete ptr;
}

void Container::addNode()
{
	IntList* ptr= new IntList;
	lst.add(ptr);
}

void Container::addItem(int node, int data)
{
	if (node &lt;lst.count())
	{
		lst.items(node)-&gt;add(data);
	}
	else
	{
		cout&lt;&lt;&quot;The node number is exceeding max!&quot;&lt;&lt;endl;
	}
}

int Container::getItem(int node, int index)
{
	return lst.items(node)-&gt;items(index);
}

void IntList::onDisplay(int data)
{
	cout&lt;&lt;data;
}

template&lt;class T&gt;
void mylist&lt;T&gt;::onRemove(T ptr)
{
}

template&lt;class T&gt;
void mylist&lt;T&gt;::remove(int index)
{
	int temp= index;
	if (index&lt;counter)
	{
		onRemove(items(index));
		while (temp&lt;counter-1)
		{	
			
			items(temp) = items(temp+1);
			temp++;	
		}
		counter--;
	}
}

template&lt;class T&gt;
void mylist&lt;T&gt;::insert(int index, T ptr)
{
	if (!checksize())
		expand();

	if (counter == 0)
	{
		items(0) = ptr;
		counter++;
	}
	else
	{
		if (index&gt;=0&amp;&amp; index&lt;=counter)
		{
			int i=index;
			T hold1 = items(index), hold2= items(index+1);
			while (i&lt;counter)
			{	
				hold2 = items(i+1);
				items(i+1) = hold1;
				hold1 = hold2;				
				i++;
			}
			items(index) = ptr; //any exception trap???
			counter++;
		}
	}
}
			
template&lt;class T&gt;
int mylist&lt;T&gt;::locate(T ptr)
{
	int index = 0;
	while (items(index) &lt;ptr &amp;&amp;index &lt;counter)
	{
		index++;
	}
	return index;
}



template&lt;class T&gt;
bool mylist&lt;T&gt;::find(T ptr)
{
	int index = 0;

	index = locate(ptr);
	if (index == counter)
	{
		return false;
	}
	else
	{
		return (items(index) == ptr);
	}
}


template&lt;class T&gt;
int mylist&lt;T&gt;::count()
{
	return counter;
}

template&lt;class T&gt;
T&amp; mylist&lt;T&gt;::items(int index)
{
	return flist[index];
}

template&lt;class T&gt;
void mylist&lt;T&gt;::onDisplay(T ptr)
{
//	cout&lt;&lt;ptr;
}


template&lt;class T&gt;
void mylist&lt;T&gt;::display()
{
	cout&lt;&lt;setiosflags(ios::showpoint|ios::fixed);
	for (int i = 0; i &lt; counter; i ++)
	{
		cout&lt;&lt;&quot;Number &quot;&lt;&lt;i&lt;&lt;&quot; item is:&quot;;
		onDisplay(flist[i]);
		cout&lt;&lt;endl;
	}
}

template&lt;class T&gt;
void mylist&lt;T&gt;::uninitialize()
{
	for (int i=0; i&lt;counter; i++)
	{
		onRemove(items(i));
	}
	free(flist);
}

template&lt;class T&gt;
mylist&lt;T&gt;::~mylist()
{
	uninitialize();
}


template&lt;class T&gt;
void mylist&lt;T&gt;::add(T ptr)
{ 
	int index;
	index = locate(ptr);
	if (items(index)!=ptr)
	{
		insert(index, ptr);
	}
}

template&lt;class T&gt;
void mylist&lt;T&gt;::initialize()
{
	LENGTH = 10;
	SIZE = LENGTH;
	if ((flist =(T*)(malloc(sizeof(T) * SIZE)))==NULL)
	{
		//exception need to be handled here!!
		cout&lt;&lt;&quot;Unable malloc memory for size of &quot;&lt;&lt;SIZE&lt;&lt;endl;  		
	}
	counter = 0;
}

template&lt;class T&gt;
bool mylist&lt;T&gt;::checksize()
{
	return (counter &lt; SIZE);
}

template&lt;class T&gt;
void mylist&lt;T&gt;::expand()
{
	SIZE += LENGTH;
	if ((flist = (T*)(realloc(flist, sizeof(T) * SIZE)))== NULL)
	{
		cout&lt;&lt;&quot;Unable realloc memory for mylist of size &quot;&lt;&lt;SIZE&lt;&lt;endl;
	}
}

template&lt;class T&gt;
mylist&lt;T&gt;::mylist()
{
	initialize();
}
</pre>
<pre>
</pre>
<pre>　</pre>
<pre>int array[20] = {21,31,24,51,42,3,23,5,45,6567,76,88,99,11,234, 3435,566,78,56,20};</pre>
<pre><span lang="zh-cn">The following is the output which is quite unpleasing.</span></pre>
<pre><a name="result"></a>node is:0
and itemcount is:8
11
20
56
78
99
234
566
3435
node is:1
and itemcount is:11
3
5
23
24
31
42
45
51
76
88
6567
node is:2
and itemcount is:0
node is:3
and itemcount is:0




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

</body>

</html>