Jump to content
  • Advertisement
Sign in to follow this  
red-dragonX

need Linked List help

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello peeps, so i'm working on linked list basics. basically, it's in C. i have a struct before main:
typedef struct node {
int             data;
struct node*    next;
struct node *head; 
struct node *end; 
} nodew;
now, i'm adding a function Add, which will (as you can guess) add another node at the end of the list. my biggest problem is, i'm defining this function after main like this: #directives struct node{ } int main() { } Add function() { } Well, within that Add node function, I use these few lines: if(head == NULL) head = new; end->next = new; new->next = NULL; end = new; When I use the cc compiler, it gives me error messages complaining about using head and end: "ll.c", line 45: undefined symbol: head "ll.c", line 47: undefined symbol: end It's been a while since I worked with LList and in C especially. What might be the problem with me using head and end in the Add function after main??

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by red-dragonX
hello peeps,
so i'm working on linked list basics. basically, it's in C. i have a struct before main:


typedef struct node {
int data;
struct node* next;
struct node *head;
struct node *end;
} nodew;


now, i'm adding a function Add, which will (as you can guess) add another node at the end of the list. my biggest problem is, i'm defining this function after main like this:

#directives

struct node{ }

int main() { }

Add function() { }

Well, within that Add node function, I use these few lines:
if(head == NULL)
head = new;
end->next = new;
new->next = NULL;
end = new;

When I use the cc compiler, it gives me error messages complaining about using head and end:

"ll.c", line 45: undefined symbol: head
"ll.c", line 47: undefined symbol: end


It's been a while since I worked with LList and in C especially.
What might be the problem with me using head and end in the Add function after main??

You forgot the end-> on each of those lines. *edit: Misread your code. Same basic problem, different explanation. You don't specify what structure head and end are a part of. thisNode->head or whatever.

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
You forgot the end-> on each of those lines. *edit: Misread your code. Same basic problem, different explanation. You don't specify what structure head and end are a part of. thisNode->head or whatever.

CM


well, within main, I have this code:

int main() {

struct node *head = NULL;
struct node *end = NULL;

head = malloc(sizeof(struct node));
end = malloc(sizeof(struct node));

head->data = 1;
head->next = end;

end->data = 2;
end->next = NULL;
...
..
.


Sorry, I didn't include that in original post. Is that what you were referring to when you said that I hadn't specified what structure head and end part of, McCloud? If not, can someone give me specific code, obviously I'm forgetting something here :(

Share this post


Link to post
Share on other sites
Probably not the answer but are the values of head and end passed to the add function? If they are declared in main then they are out of scope in the Add function.

Share this post


Link to post
Share on other sites
I think you are very confused about where things are going.

Your list has only one head and one end. They shouldn't go into the node structure, because then every node in the list will have those pointers. You could do that extra linking-up, but it wouldn't really get you anywhere. If you want to make more than one list, make an actual List struct, which contains the head and end pointers.

The 'head' and 'end' pointers in main are different from the head and end in any particular node, and are only in scope within main. In the node-adding function, there is no indication of which pointers you want to work with.

So let's set it up:


// First, we need to say what a list is. A list contains nodes, so we want it to
// have a pointer to head and end nodes. Ok, so really we need to say what a
// node is first :)
typedef struct node_t {
int data;
struct node_t* next;
} node;

typedef struct list_t {
node* head;
node* end;
} list;

// Now let's forget about main for a while, and just make a node adding
// function.
// Nodes are added to lists, so we need to tell the function which list to
// work with. And we need to pass a pointer so that the input list can be
// modified (via the C rules). And we need to say what kind of node to add.
// Actually creating the node is something we can do right here, so that
// we only have that code in one place, but we have to at least say what value
// to use. Finally, let's return something useful: the passed-in pointer, so
// that we can chain the function calls. (The function will modify the (directly
// or indirectly) pointed-at data, but not the pointer itself.)
// Putting all of that together:
list* addNodeTo(list* l, int value) {
// first, allocate a node and set its value appropriately.
node* n = createNode(value); // you implement that
// next, add that node to the list. For example, we may add it to the front:
n->next = l->head; // point at the beginning of the list. If there was
// nothing in the list, then we point at null, and all is well.
l->head = n; // next, inform the list that this is the new head.
if (!l->end) l->end = n; // finally, if this is the first item, it's also
// at the end.
}

// As mentioned, you'll want to implement a node creation function - that's
// something that you want to wrap up in a function, because the malloc work is
// a little hairy, and you can also use the function to make sure the pointers
// are initialized.
// Similarly, you'll want a list creation function.
// And there's lots of other stuff missing here too, of course. I can't just
// write it all for you. ;)
// Finally, in main, we can actually create some linked lists and do something
// useful with them :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!