Public Group

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

## 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 *end;
} nodew;


##### Share on other sites
Quote:
 Original post by red-dragonXhello 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:#directivesstruct 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: endIt'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 on other sites
Quote:
 Original post by Conner McCloudYou 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 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 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 :)

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 9
• 12
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
633348
• Total Posts
3011459
• ### Who's Online (See full list)

There are no registered users currently online

×