Hi, there i didn't even know is that how its called but can you help me and tell me what is that LLIST from the structure into the type function and why its used in this case:

typedef struct node {
	int data;
	struct node *next; /* pointer to next element in list */
} LLIST;
 
 
LLIST *list_add(LLIST **p, int i)
{
	if (p == NULL)           /
		return NULL;
 
	LLIST *n = malloc(sizeof(LLIST));   
	if (n == NULL)
		return NULL;
 
	n->next = *p; /
	*p = n;      
	n->data = i;
 
	return *p;
}

int main(void)
{
	LLIST *n = NULL;

	list_add(&n, 0);

	return 0;
}

Thanks in advanced :)

I don't completely understand your question, but lines 4->7 define the LLIST structure, which appears to be a linked list. Line 28 creates a pointer for the first element of a chain of LLIST's. list_add() appears to create some memory for an instance of an element of a linked list at line 15, initializes the structure in lines 19->21, then returns and exits the program at line 23 & 32.

What exactly do you not understand? Do you understand what a linked list is and how it works?

Edited 6 Years Ago by N1GHTS: n/a

Why its used

LLIST *list_add

not like normal

int list_add ????

and what that actually means that LLIST *list_add ???

Thanks in advance

Edited 6 Years Ago by westony: n/a

:|

Are you saying int list_add() is normal but LLIST *list_add() is not?

If so, you need to learn about functions before trying to understand this code.

Specifically learn about the return values in C functions and look into pointers and how they work.

Without knowing those basics you won't really understand my explanations, and I don't have much time to write a tutorial on C functions.

Noo man you dont understand me right ... I do not even know how to say it right ... I never seen before code like " LLIST *list_add " for a functions don't get me wrong i worked with four star pointers and not having problems with them, just for now ...... can you help me understand those functions ...


I just need to understand why, they are using those kind of functions, why not int, void or others ?!?!
I can write the same thing with int functions ...

Edited 6 Years Ago by westony: n/a

LLIST is just like int or void or char or any other type. There is nothing special about it.

An int is called a type. The return values of any function must return a valid type or void to mean "no return type".

Line #4 of your pasted code saids "typedef struct". What that does is it creates a brand new type, like int or void or char, except this is a LLIST. It comes up with this name on line #7.

So since its a type just like any other, you can return it from a function or pass it to a function parameter or even use it as seasoning for your pasta.

In C, there is nothing "normal" or "special"; I cant say the same thing for other languages.

So you are telling me that type from structure i can be using it for functions to ???
And there is no matter I type int or LLIST in this case its just about having return ?

And there is no matter I type int or LLIST in this case its just about having return ?

It does matter what type is returned. It has to match the type of information you want to return from the function. In this case LLIST returns a pointer to a LLIST instance. Other times you might want it to be something else.

The point is, functions can return anything, but it can only be one thing (unless you use the parameters as return values, but thats a different subject). Since you can only return one thing from a function, using structs like LLIST allows you to return more data because those variables are inside the returned structure. So it really depends on what you want to return for that function.

aha :) thanks man :) i get it :)

and last question...

Do you think that this is the optimal way for linked list or ? Can you give some advice :)

Edited 6 Years Ago by westony: n/a

Well I guess. That particular linked list code doesn't really have an application. If it did I could suggest optimizations, but in that context its pretty much a museum piece.

Like I said, I don't know since its outside of any useful context. Linked Lists are not very practical, usually used in low-level hardware event queues and artificial intelligence.

If you want some optimization suggestions, find an application for this code.

Edited 6 Years Ago by N1GHTS: n/a

Like I said, I don't know since its outside of any useful context. Linked Lists are not very practical, usually used in low-level hardware event queues and artificial intelligence.

If you want some optimization suggestions, find an application for this code.

well low-level hardware ?!?!?! is that the point of C ???

I'm talking about the linked lists, not the language itself. C is definitely made for low level hardware code.

...

Even if I randomly pointed out things to optimize from the code you showed me, I have a feeling we would never be quite on the same page.

sorry i though that you are working only with C and you would be C professional /well you might be but i mean about your occupation/

I am just negotiating them because I did not used them quite a lot time ... and i am afraid that i might forget them ...

Thanks again for the help :)

I'm also sorry because I don't know how to speak your native language. I am sure this whole conversation would have gone differently if we had spoken the language you are most comfortable with.

aha :) thanks man :) i get it :)

and last question...

Do you think that this is the optimal way for linked list or ? Can you give some advice :)

I dont think the code you have written is correct. What do you wan to do in the add function ? Add to a singly linked list or circularly linked list ?

LLIST *list_add(LLIST **p, int i)
{
	if (p == NULL)           // This should be *p
		return NULL;     // If the head of the list in NULL then you have to create a head. If you return then your linked list will never get created  
 
	LLIST *n = malloc(sizeof(LLIST));   
	if (n == NULL)
		return NULL;
 
	n->next = *p; /
	*p = n;                   // What do you want to do here ?    
	n->data = i;
 
	return *p;
}

Edited 6 Years Ago by abhimanipal: n/a

Well this is not my code ... It is a copy from wikipedia...

@abhimanipal

Nice, you were tasked to fix code found in wikipedia. I had a feeling it was copied from somewhere based on the quality of his question which is why I didn't even bother correcting it and instead tried to answer his specific question. Good suggestions though.

if you ask me would code it like this:

#include <stdio.h>
#include <stdlib.h>

struct node {
       int value;
       struct node *next;
};
typedef struct {
       struct node *first;
} LLIST;


int list_add(LLIST *list, int n)
{
       struct node *item;
       int i;

       for(i = 0; i < n; i++) {
               item = malloc(sizeof(struct node));
               if (!item)
                       return -1;

               item->value = i;

               item->next = list->first;
               list->first = item;
       }

       return 0;
}

void list_del(LLIST *list)
{
       struct node *item;

       while ((item = list->first)) {
               list->first = item->next;
               free(item);
       }
}


int main(void)
{
       LLIST list = { NULL };
       struct node *item;

       if (list_add(&list, 10))
               return 1;

       for (item = list.first; item; item = item->next)
               printf("value=%d\n", item->value);

       list_del(&list);
       return 0;
}

I have added my suggestions as comments to your code

if you ask me would code it like this:

#include <stdio.h>
#include <stdlib.h>

struct node {
       int value;
       struct node *next;
};
typedef struct {
       struct node *first;
} LLIST;                               //Why do you need this struct


int list_add(LLIST *list, int n)
{
       struct node *item;
       int i;

       for(i = 0; i < n; i++) {
               item = malloc(sizeof(struct node));
               if (!item)
                       return -1;

               item->value = i;
                                                    
               item->next = list->first;        // In the first iteration list will be NUL. Attempt to dereference a NULL pointer will give a seg fault 
               list->first = item;
       }

       return 0;
}

void list_del(LLIST *list)
{
       struct node *item;

       while ((item = list->first)) {
               list->first = item->next;
               free(item);
       }
}


int main(void)
{
       LLIST list = { NULL };
       struct node *item;

       if (list_add(&list, 10))                  
               return 1;

       for (item = list.first; item; item = item->next)
               printf("value=%d\n", item->value);

       list_del(&list);
       return 0;
}
This article has been dead for over six months. Start a new discussion instead.