General Questions About Linked Lists in C

Hi guys, I've just got a few basic questions here for you.

I'm currently working with Linked Lists in C (just starting out) and just came from Java.

First off, I have a file list.c which contains all of the functions that let me add a new node to a list and remove them, prepend, append, etc. Also, I have the list.h file which contains the function prototypes.

My question is, I want to make a function in another .c file which returns a linked list that I create inside the function. So for example if I have:

1
2
3
4
5
6
7
8
9
10
struct newFunction() {
		struct node{
		int x; 
		int y;
		char name[20]; //store a string
		struct node * next;
	}

      //rest of code here
}


Is this how I would go about returning a linked list from a function. I apologize for my arrogance, I'm just having a hard time getting started.

Also, I want my nodes to be able to store two integers and a string. Is how I declared the list above how I would go about doing that. And then when I added the data to the nodes I would just use my function from the list.c file new_node(1,1,"Hello"); for example?

Thanks for all the help. Sorry if I'm way off track here, I'm just finding it hard to jump onto this mystical creature that is C coming straight from Java.
Last edited on
As long as your using both functions in one main you shouldn't really have a problem, or if your calling a function in another .c file all you need to do is include the .h file with your other includes.

Read this for information on linked lists http://richardbowles.tripod.com/cpp/linklist/linklist.htm
Question: why create your own linked list when the list container serves a similar purpose?
Or maybe there isn't such a thing in C.
Last edited on
tummychow wrote:
Question: why create your own linked list when the list container serves a similar purpose? Or maybe there isn't such a thing in C.


I am not aware of anything like that. Just Googled a bit and came up with nothing.

Anyways, thanks for the replies guys. I have another question:

If I have a function where I create a linked list, and load it with data, how do I return it so that the linked list can be passed into another (different) function? Do I simply write:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct function1() {
      struct node{ 
            struct node *next;
            int x;
            int y;
      };
      //other code here
}

//and then...

struct function2(struct node *head) //or do I pass in the whole list?
{ 
        //more code here which uses the linked list
       //created in function1()
} 


Ie: do I simply pass a pointer to the head of the linked list created in function1 into function2, which will allow me to use the linked list, or do I somehow return the whole linked list? I realize this is a terribly amateur question, but I'd appreciate the help getting started :) Thanks.

Edit: A second thought. Perhaps since *head is a pointer to a memory location, would it be safe to assume that function1() could somehow return the head pointer, which could be used to reference the linked lists memory location in function2()? Thanks


Last edited on
There are no classes in C, and no templates, hence no linked lists (or any containers, really).

Anyway, you seem to be confusing how to return structures. You can't just return a struct, you have to tell it what type it is, e.g. struct node* function2/*...*/.

I would do it something like this:

1
2
3
4
5
6
7
8
9
10
struct LLNode {
    int data; //or whatever you are using as data
    struct LLNode* next;
    //possibly other stuff like prev
};

struct LinkedList {
    struct LLNode* head;
    //other variables, like size if you want it
};


struct LLNode
Thanks firedraco, that helped a lot. I'll post back here if I have further questions. Thanks again.
Topic archived. No new replies allowed.