linked - list -> 2 var' in the struct ( add function)

This is a discussion on linked - list -> 2 var' in the struct ( add function) within the C Programming forums, part of the General Programming Boards category; Hi I'm trying to build a link list with the enclosed code. could you please help me to create an ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    58

    Cool linked - list -> 2 var' in the struct ( add function)

    Hi

    I'm trying to build a link list with the enclosed code.
    could you please help me to create an "add function" to my struct (list) with 2 components (int Y & int X)
    Code:
    typedef struct list
    {
        int Y;
        int X;
        struct list *next;
    }list;
    
    void add ( list *head, int number)
    {
        list *new_list;
        new_list=(list*)malloc(sizeof(list));
        new_list ?? number
    }
    
    thanks
    Ilan

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    Well you could do this in the function body:

    Code:
    new_list = malloc (sizeof *new_list);
    new_list->X = number;
    new_list->Y = number;
    new_list->next = NULL;
    return new_list;
    Among other things... I'm working on very little information. If that's wrong then I don't know what's right.

  3. #3
    Registered User dariyoosh's Avatar
    Join Date
    Nov 2012
    Location
    Iran / France
    Posts
    33
    Quote Originally Posted by ilans11il View Post
    Hi

    I'm trying to build a link list with the enclosed code.
    could you please help me to create an "add function" to my struct (list) with 2 components (int Y & int X)

    Hello there,


    If you use a void function, then you will need a pointer of pointer to your struct.

    you can try the following:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct list
    {
        int Y;
        int X;
        struct list *next;
    }list;
    
    typedef struct list * LinkedList;
    
    
    void add (LinkedList* param_linkedListPtr, int param_number)
    {
        if (*param_linkedListPtr == NULL)
        {
            *param_linkedListPtr = (LinkedList)malloc(sizeof(struct list));
            (*param_linkedListPtr)->X = param_number;
            (*param_linkedListPtr)->Y = param_number;
            (*param_linkedListPtr)->next = NULL;
        }
        else
        {
            LinkedList newList = (LinkedList)malloc(sizeof(list));
            newList->X = param_number;
            newList->Y = param_number;
            newList->next = NULL;
            
            // Add the new element at the end of the list
            // So let's go first, to the end of the list
            LinkedList tmpList = *param_linkedListPtr;
            while (tmpList->next != NULL)
                tmpList = tmpList->next;
            
            // Now add the new element at the end of the list
            LinkedList *tmpListPtr = &tmpList;
            (*tmpListPtr)->next = newList;
        }
    }
    
    
    void printList(LinkedList param_LinkedList)
    {
        LinkedList tmpList = param_LinkedList;
        while (tmpList != NULL)
        {
            printf("%d \t %d \n", tmpList->X, tmpList->Y);
            tmpList = tmpList->next;
        }
    }
    
    
    int main(int argc, char *argv[])
    {
        // start testing with an empty list
        LinkedList linkedList = NULL;
            
        add(&linkedList, 12);
        add(&linkedList, 14);
        add(&linkedList, 100);    
        add(&linkedList, 300);
        add(&linkedList, 400);
        add(&linkedList, 500);
    
        printList(linkedList);
        
        return 0;
    }

    Code:
    $ gcc -Wall testscript.c -o testscript
    $ ./testscript
    12       12 
    14       14 
    100      100 
    300      300 
    400      400 
    500      500 
    $
    Regards,
    Dariyoosh

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    58
    thanks ,but:
    why to use :new_list = malloc (sizeof *new_list)
    inteade of: new_list=(list*)malloc(sizeof(list));

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    58
    thanks

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    There are two reasons:

    1. You do not need to cast malloc in C since the void * type is assignable to any other pointer type.

    See also, FAQ > Casting malloc - Cprogramming.com

    2. The sizeof expression is not evaluated at run time, but at compile time, so you always will get the correct type from it. It's supposed to be a convenience and less prone to errors. Again, see the faq.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list of struct
    By palauan73 in forum C Programming
    Replies: 9
    Last Post: 10-11-2012, 12:18 AM
  2. Replies: 10
    Last Post: 04-19-2012, 10:46 AM
  3. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  4. Struct/Linked List Problems
    By valaris in forum C Programming
    Replies: 13
    Last Post: 08-01-2008, 10:25 AM
  5. Linked list and Struct Help!!!
    By Dragoncaster131 in forum C Programming
    Replies: 1
    Last Post: 04-25-2004, 08:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21