<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>
Dependency</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><font size="3">This is </font><span lang="en-ca"><font size="3">first edition of my dependency class.</font></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>Can you imagine what is a dependency problem? </b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>1. Suppose you have a sequence of courses to take and each course has 0 or more pre-requisite </b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>courses. I want you to list all dependency relations.</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>2. Suppose you are writing a compiler, one of basic part is how to solve the forward references. </b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>That is A is defined by B, B is defined by C... So, how to book keep the dependency relations. </b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>3. Suppose you are writing a file to implement the functionality of &quot;makefile&quot;. You need to establish</b></font></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>a dependency tree of all files.</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>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>It is actually a kind of edge list implementation of graph. Each node of list is actually a struct</b></font></span></pre>
  <pre><span lang="en-ca"><font size="3"><b>which has a string as data, a link list of other strings which depends on it. </b></font></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><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 MaxListSize=40;

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

struct Vertex
{
	char* str;
	Link* depend;
};

class Depend
{
private:
	Vertex* lst[MaxListSize];
	int size;
	void addNode(const char* str);
	void addSon(int index, int son);

public:
	Depend();
	~Depend();
	int getSize() { return size;}
	int find(const char* str);
	bool append(const char* str);
	bool remove(const char* str);
	void readFromFile(const char* fileName);
	void addDepend(int index, const char* str);
	void print();

};


int main()
{
	Depend D;
	D.readFromFile(&quot;c:\\depend.txt&quot;);
	D.print();
	return 0;
}

void Depend::print()
{
	Link* ptr;
	for (int i=0; i&lt;size; i++)
	{
		cout&lt;&lt;&quot;node no.&quot;&lt;&lt;i&lt;&lt;&quot;:&quot;&lt;&lt;lst[i]-&gt;str&lt;&lt;&quot; : &quot;;
		ptr=lst[i]-&gt;depend;
		while (ptr!=NULL)
		{
			cout&lt;&lt;lst[ptr-&gt;data]-&gt;str&lt;&lt;&quot;  &quot;;
			ptr=ptr-&gt;next;
		}
		cout&lt;&lt;endl;
	}
}

void Depend::addSon(int index, int son)
{
	Link* ptr;
	ptr= lst[index]-&gt;depend;
	if (ptr==NULL)
	{
		ptr=new Link;
		ptr-&gt;data= son;
		ptr-&gt;next=NULL;
		lst[index]-&gt;depend=ptr;
		return;
	}
	while (ptr-&gt;next!=NULL)
	{
		if (ptr-&gt;next-&gt;data==son)
		{
			return;//already there
		}
		ptr=ptr-&gt;next;
	} 
	//add new link
	ptr-&gt;next= new Link;
	ptr-&gt;next-&gt;data=son;
	ptr-&gt;next-&gt;next=NULL;
}

void Depend::addDepend(int index, const char* str)
{
	int pos= find(str);
	Link* ptr;
	if (pos==-1)
	{
		append(str);
		pos=size-1;//size++
		addSon(index, pos);
	}
	else
	{
		addSon(index, pos);
		ptr= lst[pos]-&gt;depend;
		while (ptr!=NULL)
		{
			addSon(index, ptr-&gt;data);
			ptr=ptr-&gt;next;
		} 
	}
}

void Depend::addNode(const char* str)
{
	char buffer[100];
	int index;
	strcpy(buffer, str);
	char* ptr=buffer;
	ptr=strtok(ptr, &quot; :\n&quot;);

	index=find(ptr);
	if (index==-1)
	{
		append(ptr);
		index=size-1;
	}
	ptr=NULL;
	ptr=strtok(ptr, &quot; :\n&quot;);
	while (ptr!=NULL)
	{
		addDepend(index, ptr);
		ptr=NULL;
		ptr=strtok(ptr, &quot; :\n&quot;);
	}
}


void Depend::readFromFile(const char* fileName)
{
	FILE* stream;
	char buffer[100];
	if ((stream=fopen(fileName, &quot;r&quot;))==NULL)
	{
		cout&lt;&lt;&quot;fail to read file &quot;&lt;&lt;fileName&lt;&lt;endl;
		return;
	}
	while (fgets(buffer, 100, stream)!=NULL)
	{
		addNode(buffer);
	}
	fclose(stream);
}

Depend::~Depend()
{
	for (int i=0; i&lt;size; i++)
	{
		delete [] lst[i]-&gt;str;
		delete lst[i];
	}
}

Depend::Depend()
{
	size=0;
}

int Depend::find(const char* str)
{
	for (int i=0; i&lt;size; i++)
	{
		if (strcmp(str, lst[i]-&gt;str)==0)
		{
			return i;
		}
	}
	return -1;
}


bool Depend::append(const char* str)
{
	if (find(str)!=-1)
	{
		return false;
	}	
	lst[size]=new Vertex;
	lst[size]-&gt;depend=NULL;
	lst[size]-&gt;str=new char[strlen(str)+1];
	strcpy(lst[size]-&gt;str, str);
	size++;
	return true;
}

bool Depend::remove(const char* str)
{
	int index = find(str);
	if (index==-1)
	{
		return false;
	}
	delete [] lst[index]-&gt;str;
	lst[index]-&gt;str=NULL;
	delete lst[index];
	size--;
	for (int i=index; i&lt;size; i++)
	{
		lst[i]=lst[i+1];
	}
	return true;
}


</pre>
<pre><font color="#0000FF"><b>Here is the result:</b></font></pre>
<pre>node no.0:238 : 228
node no.1:228 :
node no.2:229 : 228 248
node no.3:248 :
node no.4:239 : 238 228
node no.5:249 : 238 228 248
node no.6:352 : 239 238 228 249 248
node no.7:348 : 249 238 228 248
node no.8:335 : 239 238 228 249 248
Press any key to continue</pre>
<pre><font color="#0000FF"><b>Here is the input file:</b></font></pre>
<pre></pre>
<pre>238 : 228
229 : 228 248
239 : 238
249 : 238 248
352 : 239 249
348 : 249
335 : 239 249</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>