Take the 2-minute tour ×

This is code for a linked list in the C programming language.

#include <stdio.h>    /* For printf */
#include <stdlib.h>   /* For malloc */

typedef struct node {
    int data;
    struct node *next; /* Pointer to next element in list */

LLIST *list_add(LLIST **p, int i);
void list_remove(LLIST **p);
LLIST **list_search(LLIST **n, int i);
void list_print(LLIST *n);

The code is not completed, but I think it's enough for my question. Here at the end of struct node "LLIST" is used, and it's also used as a return type in the prototyping of the function list_add. What is going on?

share|improve this question
add comment

5 Answers

up vote 5 down vote accepted

typedef creates a new "type" in your program, so the return value and types of parameters of those functions are just your struct. It is just shorthand for using struct node for the type.

If you were to create a new node, you could do it like this (using the type):

LLIST *node = malloc(sizeof(LLIST));
node->data = 4;
node->next = someOtherItem;
list_add(node, 1)

Also, with the function prototypes in your question, you don't really need the double pointers; since the data in your struct is just an int, you could do something like

LLIST *list_add(int data, int position);

then the list_add function would handle the allocation, copy the int into the struct and add it to the linked list.

Putting it in at a certain position is as simple as changing the next pointer in the node before it to the address of the newly allocated node, and the next pointer in the new node to point at the next one (the one the node before that one was originally pointing at).

Keep in mind that (given the rest of your function prototypes) you will have to keep track of pointers to every node you create in order to delete them all.

I'm not sure I understand how the search function will work. This whole thing could be implemented a lot better. You shouldn't have to provide the location of a node when you create it (what if you specify a higher number than there are nodes?), etc.

share|improve this answer
add comment

That's a typedef. It's actually doing two things at once. First, it defines a structure:

struct node {
    int data;
    struct node *next;

And then does a typedef:

typedef struct node LLIST;

That means LLIST is a type, just like int or FILE or char, that is a shorthand for struct node, your linked-list node structure. It's not necessary - you could replace LLIST with struct node in all of those spots - but it makes it a bit easier to read, and helps hide the implementation from pesky end-users.

share|improve this answer
add comment

LLIST is just another type name for the struct that has been created. In general, the following format will create a type "NAME" that is a "struct x":

typedef struct x { ... } NAME;
share|improve this answer
add comment

C requires that you reference structs with a "struct" prefix, so it's common to introduce a typedef for less verbose mention.

That is, the declaration of your struct has two parts, and can be rewritten as such:

struct node {
    int data;
    struct node *next; /* pointer to next element in list */

typedef struct node LLIST;

So, LLIST is just another name for struct node (thanks Chris Lutz).

share|improve this answer
add comment

LLIST* is a pointer to a structure defined by the LLIST struct.

You should do

LLIST* myList = malloc(sizeof(LLIST)*number_of_elements);

to have some memory allocated for this list. Adding and removing items requires you to reallocate the memory using realloc. I've already written some piece of code for lists (made with arrays).

I might post the code as soon as I'm home, which is currently not the case.

share|improve this answer
add comment

Your Answer


