0

I'm trying to create a simple list where i can add members of a club (where data is val member id number, name, last name, age).

I'm having trouble populating the nodes with data of the members in the main function, when using the add_to_list function.

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


// Creating structure for node
struct test_struct
{
int val;         // val is member id number
char name;
char lastn;
int age;
struct test_struct *next;
};




// declaring global head and curr pointers
struct test_struct *head = NULL;
struct test_struct *curr = NULL;






// creating a list
struct test_struct* create_list(int val, char* name, char* lastn, int age)
{
printf("\n creating list with head node as [%d] [%s] [%s] [%d] \n", val, name, lastn, age);

struct test_struct *ptr = malloc(sizeof(struct test_struct)); // creating list
if(NULL == ptr)
{
  printf("\n Node creation failed \n");
  return NULL;
}

ptr->val = val;
ptr->name = *name;
ptr->lastn = *lastn;
ptr->age = age;
ptr->next = NULL;

head = curr = ptr;

return ptr;

}





// add member to list
struct test_struct* add_to_list(int val, char *name, char *lastn, int age, bool add_to_end)
{
if(NULL == head)
{
    return (create_list(val, name, lastn, age));
}


if(add_to_end)
{
printf("\n Adding node to end of list with data [%d] [%s] [%s] [%d] \n", val, name, lastn, age);
}
else
{
printf("\n Adding node to beginning of list with data [%d] [%s] [%s] [%d] \n", val, name, lastn, age);
}

struct test_struct *ptr = malloc(sizeof(struct test_struct));

if(NULL == ptr)
{
    printf("\n Node creation failed \n");
    return NULL;
}

ptr->val = val;
ptr->name = *name;
ptr->lastn = *lastn;
ptr-> age = age;
ptr-> next = NULL;

if (add_to_end)
{
    curr-> next = ptr;
    curr = ptr;
}
else
{
    ptr -> next = head;
    head = ptr;
}

return ptr;
} 






//search a name in created list
struct test_struct* search_in_list(char name, char lastn, struct test_struct **prev)
{
struct test_struct *ptr = head;
struct test_struct *tmp = NULL;
bool found = false;

printf("\n Searching the list for the value [%s][%s]\n", name, lastn);

while(ptr != NULL)  // searching loop
{
    if(ptr->name == name && ptr->lastn == lastn)
    {
        found = true;
        break;
    }
    else
    {
        tmp = ptr;
        ptr = ptr->next;
    }
}
return ptr;

if(true == found)
{
    if(prev)
    {
    *prev = tmp;
    return ptr;
    }

    else
    {
    return NULL;
    }

  }
 }






//printing the list
void print_list(void)
{
struct test_struct *ptr = head;

printf("\n -----Printing list Start----- \n");

while(ptr != NULL)
{
    printf("\n [%d] \n", ptr -> val);
    printf("\n [%s] \n", ptr -> name);
    printf("\n [%s] \n", ptr -> lastn);
    printf("\n [%d] \n", ptr -> age);
    ptr = ptr->next;
}

printf("\n -----Printing list end---- \n");

return;
}




//printing the list 2 is for printing age only
void print_list2(void)
{
struct test_struct *ptr = head;

printf("\n -----Printing list Start----- \n");

while(ptr != NULL)
{
    printf("\n [%d] \n", ptr -> age);
    ptr = ptr->next;
}

printf("\n -----Printing list end---- \n");

return;
}




// main function
int main(void)
{
char n, l;
struct test_struct *ptr = NULL;


// for adding member to list
    add_to_list(123, "william", "shakespeare", 30, true);
    add_to_list(124, "william", "gibson", 35, true);
    add_to_list(125, "chuck", "palahniuk", 40, true);
    add_to_list(126, "mario", "puzio", 50, true);
    add_to_list(127, "umberto", "eco", 60, true);
    add_to_list(128, "ezra", "pound", 125, true);

    print_list();




// for searching name in list
    ptr = search_in_list(n, l,  NULL);
    if(NULL == ptr)
    {
        printf("\n Search [name = %s] [lastn = %s] failed, no such element found \n", n, l);
    }
    else
    {
        printf("\n Search passed [name = %s] [lastn = %s] \n", ptr->name, ptr->lastn);
    }


    print_list();



return 0;
}
Mimic01
  • 107
  • 1
  • 12
  • 1
    [You should not cast the result of `malloc()`](http://stackoverflow.com/a/605858/1983495). And try not to mix declarations with statements. And try to be consistent, either use the ugly `x -> y` or `x->y` but not both. And don't leave so many empty lines in the code, it's makes it to sparse to link it's pieces back. Code readability matters "*a lot*". And read a book about c, your code doesn't even compile. I don't know what to do with your question. – Iharob Al Asimi Sep 24 '15 at 01:44
  • 1
    @iharob "Elegance is not optional" - Richard O’Keefe – Parappa Sep 24 '15 at 01:49
  • @Parappa I agree with you 100%. – Iharob Al Asimi Sep 24 '15 at 01:50

3 Answers3

0

When using a linked list in C I always like to use the sys/queue.h library. There are different implementations you can use within queue.h, but here is the list example off of the man page:

  LIST_HEAD(listhead, entry) head;
    struct listhead *headp;                 /* List head. */
    struct entry {
        ...
        LIST_ENTRY(entry) entries;          /* List. */
        ...
    } *n1, *n2, *np;

    LIST_INIT(&head);                       /* Initialize the list. */

    n1 = malloc(sizeof(struct entry));      /* Insert at the head. */
    LIST_INSERT_HEAD(&head, n1, entries);

    n2 = malloc(sizeof(struct entry));      /* Insert after. */
    LIST_INSERT_AFTER(n1, n2, entries);
                                            /* Forward traversal. */
    for (np = head.lh_first; np != NULL; np = np->entries.le_next)
        np-> ...

    while (head.lh_first != NULL)           /* Delete. */
        LIST_REMOVE(head.lh_first, entries);

I would comment this as I know it is not what you are looking for in an answer, but I HIGHLY recommend using this library.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
httpNick
  • 2,524
  • 1
  • 22
  • 34
  • 1. `sys/queue.h` is not a library. 2. This is unrelated to the problem, did you read the code? This does not answer the question, the question cannot be answered, the OP lacks the basic knowledge required to write even a simple c program, I have no idea how it is possible that this OP is trying linked lists if he/she does not understand the basic rules of the c language. – Iharob Al Asimi Sep 24 '15 at 01:49
  • Yes I would comment if I had the rep, but I am still short. queue.h would solve the issue the OP has for needing a linked list and that is why I am suggesting it. Not everyone knows about queue.h so I thought it might be helpful. – httpNick Sep 24 '15 at 01:51
  • You will not get any more reputation by giving bad answers, please be patient. Also `queue.h` is not standard the OP is very likely using MS Windows and I don't think there is a `queue.h` there, and I doubt any professional programmer using it at all. And please read the code carefuly, the linked list is not a problem with this code, *the code is not even valid c*. – Iharob Al Asimi Sep 24 '15 at 01:54
0

Your linked list implementation looks fine except below problems

  • Your linked list node member variable "name" and "lname" are of type char but you are trying to add char* to the list.
  • search_in_list is failing because of input argument "prev". Looks like it's not needed. Just add "return ptr" after the while loop.
Anil Nayak
  • 46
  • 3
  • "name" and "lastn" are char, what would be the difference between char and char*? EDIT: oh nothing im reading about it, thanks! – Mimic01 Sep 24 '15 at 03:03
-1

Is the compiler giving you errors? It doesn't seem like you are calling add_to_list correctly. It should look like this,

add_to_list(123, "william", "shakespeare", 30, true);

Also it looks like add_to_list is unimplemented. If the list is empty it will make a call to create the first entry, but otherwise it does not add the new data to the list.

Parappa
  • 7,566
  • 3
  • 34
  • 38
  • you mean that there should be at least one NULL value so it creates the first node? i'm still confused. When i run it, there's 0 errors but lots of warnings "passing argument 2 of add_to_list makes integer from pointer without a cast." And the program crashes. – Mimic01 Sep 24 '15 at 02:02
  • @Mimic01 Maybe it crashes because you ignore the warnings! Please post the actual program, the one that compiles. – Iharob Al Asimi Sep 24 '15 at 02:07