So, I am writing a program which reads inputs from a text file. The prgoram plants a plant at (x,y) location which is read from a file as PLANT 3,4 for example. It also puts the plant in flames from command FLAME 3,4. So, there will be multiple plants and fire in the end. There can also be fire even if a plant is not there. And when does it outputs an image on the screen. I m thinking of using linked list to store plants and flame's location. Would i need to create 2 linked lists or could it be done with 1 linked list? I am not sure. I think I need to use 1 linked list, cuz i have to iterate through to create a image, which will be at correct coordinates as it was specified by a tester. Any suggestions/ ideas?

Thanks

How are you going to arrange a list in 2D ? You would have to make a list of lists. With the first list just holding the heads of the list starting at that position. Something like this :

|L1 -> List L1 follows...
|L2 -> List L2 follows...
|L3 -> List L3 follows...
|L4 -> List L4 follows...

Now each of this list will have individual nodes carrying data (plant/flames) So for planting a plant at (3,4) you need to insert the data "P" at the 4th node of the 3rd list (L3). Same for the flames. So you would need two data elements in each node.

Alternatively you can also use a 2D array of Strings, manipulating which, according to me, would be far more easier. Whenever you have a plant to be planted at location (3,4) you write the string "P" at the index [3][4] of the array. IF you need a flame too on the same location you have a string "PF" for only a flame you can use "F".

Traversing the lists, in the first approach and the array in the second approach would give you the current snapshot of the field.

The only thing where the second approach is restrictive is the disadvantage that arrays present to you, you need to know the maximum number of locations (rows & columns) in advance, if you are going to be knowing this in advance, I suggest you use the second approach.

This article has been dead for over six months. Start a new discussion instead.