tags:

views:

331

answers:

6

This is a code for linked list in 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;

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 its enough for my question. Here at the end of struct node "LLIST" is used and its also used as a return type in the prototyping of the function list_add. Please, somebody explain it in details.

+4  A: 

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;
Amber
A: 

Get this book and read chapter 10 'Structures and List Programming' which has the exact same code.

JBRWilkinson
+9  A: 

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.

Chris Lutz
+4  A: 

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).

Kim Gräsman
Technically, `LLIST` is another name for `struct node`, but close enough.
Chris Lutz
Thanks, edited.
Kim Gräsman
A: 

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 wrote 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.

regards

Atmocreations
A: 

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.

Carson Myers