I need to implement a function which ads elements to a single linked list:
-if the head(first element) of the list doesn't exist(if head is NULL), then the added(inserted) element becomes the head(the the data entered is of string type)
-if the head already exists, insert it after the last element(for example |A|*->|B|*->NULL, after adding element C, the list looks like this: |A|*->|B|*->|C|*->NULL)
-the information member holds string type information(pointer to char to more precise)

We're not going to do your homework for you.

(1) if the head is NULL -- this is a simple assignment statement. allocate memory for the new node then assign its pointer to the list's head.

(2) head already exists: You want to add the new node to the tail of the list. The function needs to iterate through the nodes to find the last node, then assign the new node to the next pointer of the tail node. That makes the new node the new tail node.

(3) It doesn't matter what the node contains, the above two methods are always done the same way.

the problem is that I need it in another project, so this is how it should look like?

void add2list(List *AnotherList,char *key)
{
List *CurrentList,*NewList;
CurrentList=AnotherList;
if(CurrentList==NULL)
	  {
       printf("Head-less list");
	   NewList=(List *) malloc(sizeof(List));
	   NewList->key=(char *)malloc(sizeof(key));
	   strcpy(NewList->key,key);
	   CurrentList=NewList;
	  }
	  else
	  {
       printf("List has head going to last element");
	   while(CurrentList->next !=NULL)
		   CurrentList=CurrentList->next;
	   NewList=(List *)malloc(sizeof(List));
	   NewList->key=(char *)malloc(sizeof(key));
	   strcpy(NewList->key,key);
	   NewList->next=NULL;
	   CurrentList->next=NewList;
	  }
}

>> NewList->key=(char *)malloc(sizeof(key));

sizeof() operator does not return the same thing as strlen() function. All sizeof will give you for a pointer is the size of the pointer, not what the pointer contains. So what you want is NewList->key=(char *)malloc(strlen(key)+1);

@Ancient Dragon,What do you mean by "All sizeof will give you for a pointer is the size of the pointer, not what the pointer contains" ?

>What do you mean by "All sizeof will give you for a pointer is the size of the pointer, not what the pointer contains" ?
I'll answer your question with another question: Will the following code print the same number twice?

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

int main(void)
{
    struct test {
        char data[256];
    } obj;
    struct test *p = &obj;

    printf("%lu\n", (unsigned long)sizeof obj);
    printf("%lu\n", (unsigned long)sizeof p);

    return 0;
}

first it prints the size of the structure, then the size of the pointer(so different numbers will be shown; because size of structure is greater than size of pointer), am I right?

You are right, and that's exactly what I meant. It is not possible to use sizeof() to get the size of an object from a pointer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.