1

FIX: I wasn't saving the locations back into world so I was just leaking the information. Credit to Skeeto.

while(!feof(fp)){
    loc = readLocation(fp);
    join(loc,world);
    }

should actually be

while(!feof(fp)){
    loc = readLocation(fp);
    world = join(loc,world);
    }

EDIT: As mentioned in the comments, yes I am a student, but I'm not looking for someone to do my work for me. I'm simply trying to locate possible logic errors because if I could fill this list properly, I can finish my project very easily. This is just a small portion of a very immersive project, helping with this will only allow me to continue the project, not complete it and turn it in. I only provided so much detail because 1) I've never posted here before so didn't know any better, and 2) wanted the reader to understand the workings of this in order to aid them in assisting me. Also, with any concerns as to skype, if that ended up being where successful help was given, I would provide the fix above this 'edit' as well as crediting the stackoverflow user for helping.

TLDR: Yes I'm a student, No im not trying to have someone do my project. This is a very small portion and will only allow me to continue, not complete. If help was given via skype I would update this post with the fix as well as credit the helper.

Hello and thank you for any help in advance.

I am trying to create a linked list that holds objects of type Location *. I have Location defined as

typedef struct location{
char *name;
char *longer;
char *shorter;
char *north;
char *south;
char *east;
char *west;
char *logic;
int visited;
char *items[20];
} Location;

Furthermore I can succesfully read in all the values for the location and display all attributes so that is not an issue.

In the 'engine' of my game (the main), i attempt to read all the locations into a list as seen in the following (I'm certain readLocation works correctly because I threw a print statement into the loop printing the name of the locations using the loc variable)

world = 0;
FILE *fp = fopen("world.in","r");
char *garb = readToken(fp);
free(garb); //garbage token at begging of world.in just to check file exists
int count = 0; //used later, ignore for now
while(!feof(fp)){
    loc = readLocation(fp);
    join(loc,world);
    }

world is global variable declared as Node * and initialized to 0 (I think i need to do that but am not sure)

In olist.h I create the node structure as

typedef struct node
{
Location *place;
struct node *next;
} Node;

and in olist.c this is how i construct the Node as well as join the nodes

//place is the attribute of the Node that holds the location and next points to the next Node in the list
Node *newNode(Location *loc,Node *next)
    {
    Node *n = malloc(sizeof(Node));
    if (n == 0)
        {
        fprintf(stderr,"newNode: out of memory!\n");
        exit(1);
        }
    n->place = loc;
    n->next = next;
    return n;
    }

//s is the location i wish to join to the list and rest is list I'm joining to
Node *join(void *s,Node *rest)
    {
    return newNode(s,rest);
    }

Unfortunately, after successfully reading in all locations, world is still an empty list. Thanks for any help and I will be happy to provide further information via this forum or skype: F3V3Rz_MoDz (its a very old name)

iF3V3RzXD
  • 31
  • 2
  • 2
    Please avoid using Skype so that others can benefit from any answers you get along the way. – Alex W Apr 19 '15 at 02:25
  • They way you've posed this question, it sounds a lot like a student asking for StackOverflow to do his homework for him. You'll probably have more success if your question were more specific and something whose answer may benefit others as well as yourself. – Andrew Miner Apr 19 '15 at 02:33
  • Looks like `newNode` has a memory leak. – Fiddling Bits Apr 19 '15 at 02:36
  • The compiler should have given at least a warning for the `if ( n == 0 )` part. In C, you should use `NULL`; 0 is used in C++ (for whatever reason this was made it into the standard). – too honest for this site Apr 19 '15 at 03:32
  • @Olaf isn't NULL a preprocessor definition for 0? – Corb3nik Apr 19 '15 at 03:51
  • 1
    @Cubia: That would be C++. In C, it is (void *)0. Common mistake from C++ users. I always wonder why that has been changed in C++. I actually prefer comparing a pointer with a pointer, not an integer (yes, I know how this is defined in C++) – too honest for this site Apr 19 '15 at 03:56
  • Just another flaw: Why do you declare `s` in `join()` `void *`? That should actually be Location *s. This way, you keep your compiler from helping you detect type errors. – too honest for this site Apr 19 '15 at 04:03
  • @Olaf You're correct, thank you! Thinking about that also helped me realize errors in other functions I wrote to manipulate Nodes – iF3V3RzXD Apr 19 '15 at 04:19
  • Welcome! You should always enable as many warnings as reasonable. gcc for example will even warn you on comon coercion problems if activated. Other compilers (exspecially commercial one for embedded systems) are not that helpful with this. Still wonder why the compiler did not complain; did you enable C99/C11 mode? It might be no complain in K&R or C++ mode. – too honest for this site Apr 19 '15 at 13:04

1 Answers1

0

If I'm not mistaken, your problem is in the following line of code :

n->next = next;

The head of your linked list is world, but your newNode() function keeps sending world to the back of the list (n->next = next places next at the back). What you want here is to append at the end of your linked list.

Here is an example of code that you can use to do this :

Node *lastNode = next;
while (lastNode->next != NULL)
    lastNode = lastNode->next;

lastNode->next = n;

Basically, you iterate through your linked list until you get to the end, then you append the newly created node.

EDIT : The problem you were having is that the world variable is in the end of your linked list. Every time you call join() you are pushing world at the back of the list. Here's a representation of your list :

Before join() :

world -> null

After join() :

newnode -> world -> null

Therefore, everytime you try to iterate through the list, world does not see the newly created nodes that are before him.

My solution does the following :

world -> newnode -> null

Which basically keeps your world variable in front. So you don't have to do world = join(loc, world)

Corb3nik
  • 1,177
  • 7
  • 26
  • I think there may have been some confusion (most likely on my side). The way I intended this to function is 'world' IS my linked list that i can map through at later time to access various Locations that it is storing and so it does not matter that the list is backwards from how it was read in. I don't believe the list is having any Locations added in the first place because I tried to simply read through the list by accessing the head and reassigning the list to the tail but even though the loop runs successfully, nothing occurs. Thank you for helping! – iF3V3RzXD Apr 19 '15 at 03:07
  • Sorry for the double post, I realized that the program never even enters the while loop: while(world != 0){ – iF3V3RzXD Apr 19 '15 at 03:13
  • @iF3V3RzXD The head is the variable "world" right? If so, then my code corrects this issue. In your current code, since you are sending "world" at the back of the list, when you try to iterate through your linked list, world->next will always be null, because you never gave it a value. – Corb3nik Apr 19 '15 at 03:53
  • @iF3V3RzXD Have you found a solution for your issue? – Corb3nik Apr 19 '15 at 04:28
  • world is my linked list that of nodes that holds all the locations. For my particular needs it didn – iF3V3RzXD Apr 19 '15 at 11:16
  • world is my linked list that of nodes that holds all the locations. For my particular needs it didn't matter that the order of the list was reversed though. Using the join function joins the new location to the front of the list "world" which is okay because it points to the previous location added. I corrected join to pass in a Location * instead of a void and now world->next only points to null when the at that last element of the list which is ideal for me. My main issue though was that I never reassigned the list, i just joined it, so It kept adding the new location to nothing. – iF3V3RzXD Apr 19 '15 at 11:22
  • I think you are missing the point of my solution! I edited my answer so you can undestand. Have you tried my solution? – Corb3nik Apr 19 '15 at 13:34