View Single Post
  #2   (View Single Post)  
Old 20th August 2008
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 3,319
Default

Quote:
Originally Posted by 18Googol2 View Post
Im thinking about struct and array, but not sure if its the right method. Also, I have no idea how to read the file and write to memory with such format.
There is no single right answer. Everything in programming is a compromise attempting to balance corporate bureaucracy, hardware constraints, time constraints, & considering the poor slob who gets to inherit your code once you leave the project or company.

That said, the format described dictates an easy representation:
  • The first character read names the node itself.
  • The next value read defines the number of dependencies/adjacencies...
  • ...followed by an enumeration of all the dependencies'/adjacencies' names.
The question ultimately becomes how to represent the variable list of names. If you know the upper bound of how many dependencies/adjacencies to expect, then you can use an array, however as a precaution, I would recommend you implement the error logic needed to ensure that the array bounds are not exceeded.

If this is how you implement the solution, you will need to a structure which:
  • stores the node's name.
  • stores the number of dependencies/adjacencies.
  • stores the names of all dependencies/adjacencies in an array.

A better solution would be to implement a linked list which gets away from the constraints of an array. This approach simplifies what needs to be stored given that the linked list is only restricted by available heapspace:
  • Again, store the node's name.
  • Store the names of all dependencies/adjacencies in a linked list.
  • Maintaining the number of dependencies/adjacencies is optional.
Perhaps you haven't gotten into dynamic memory allocation yet in your studies. If not, then the second approach is not an option.

So, this only defines a single record. Either you know the maximum number of records to expect, or you don't. If you do, then you can create an array of records, but you will also require an external index counting how many records have been read thus far. This describes one approach.

Again, a better approach is to create a linked list of records for the same reasons as before. Yes, this means managing nested linked lists, but if you test incrementally as you write the code, this won't be as daunted as it may appear.
Reply With Quote