Like Tree1Likes

Declaring linked list inside linked list

This is a discussion on Declaring linked list inside linked list within the C Programming forums, part of the General Programming Boards category; 1) What I mean is don't cast whatever malloc returns. some_ptr = malloc(blah); NOT some_ptr = (some_ptr*)malloc(blah); 2) fflush(stdin) is ...

  1. #16
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) What I mean is don't cast whatever malloc returns.
    some_ptr = malloc(blah);
    NOT
    some_ptr = (some_ptr*)malloc(blah);

    2) fflush(stdin) is UNDEFINED behaviour, don't use it.

    3) Fix the issues I pointed out in my previous post before continuing. When you don't know, Google, then come here to ask if you couldn't find your answer.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  2. #17
    Registered User
    Join Date
    Apr 2012
    Location
    Malang, Indonesia
    Posts
    22
    I still can't input anything if I don't use fflush(stdin) before fgets(). Can you help me?

    Anyway, why in point 1 I can't cast malloc return? I need to know because my lecture usually declare malloc like that

  3. #18
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by blueboyz View Post
    I still can't input anything if I don't use fflush(stdin) before fgets(). Can you help me?

    Anyway, why in point 1 I can't cast malloc return? I need to know because my lecture usually declare malloc like that
    Read here:
    FAQ > Casting malloc - Cprogramming.com

    and here:

    FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com

    Generally, learn to Google answers to simple questions.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #19
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by blueboyz View Post
    Can you explain it more detailed? I bit confused with void *data. With an example of program maybe?
    The point I was trying to make was that, the nodes and your list are containers for your data. Your data itself does not need to, and should not contain links. Using void* only means that any type of data can be attached to the list, not just student structs.

    Here is an example with an insert in front function.

    Code:
    struct student {
        char name[128];
        char age;
    };
    
    struct node {
        void *data;
        struct node *next;
    };
    
    int insert_node(struct node **list, void *data) {
        struct node *n = calloc(1, sizeof(struct node));
        if(!n) return 0;
    
        n->data = data;
    
        if(*list == NULL) {
            *list = n;
            return 1;
        }
    
        n->next = *list;
        *list = n;
        return 1;
    }
    Then you could use it like this for example:

    Code:
        struct node *list = NULL;
        struct student *student = NULL;
        int retval = 0;
    
        student = calloc(1, sizeof(struct student));
        printf("Enter name: ");
        scanf("%127s", student->name);
        printf("Enter age: ");
        scanf("%d", student->age);
    
        retval = insert_node(&list, student);
        if(retval == 0) // error
    You could probably get some ideas by reading this as well. http://cslibrary.stanford.edu/103/LinkedListBasics.pdf

  5. #20
    Registered User
    Join Date
    Apr 2012
    Location
    Malang, Indonesia
    Posts
    22
    Can I used that method to store 2 or more student in 1 node?
    With what? Array?

  6. #21
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by blueboyz View Post
    Can I used that method to store 2 or more student in 1 node?
    With what? Array?
    No, but each node are linked to each other, it's a linked list. If you would repeat the above a second time or in a loop, then for each iteration "student" would be unique since calloc assigns new memory to the pointer. The insert_list function would take the new student, create a new node, add a reference to "student" and link the new node to the rest of the list.

  7. #22
    Registered User
    Join Date
    Apr 2012
    Location
    Malang, Indonesia
    Posts
    22
    OK I got it now.
    But what I need is putting 2 or more linked list in 1 node. Maybe I need to change my code and use student as array

  8. #23
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Well, I don't know what you need I'm going by the first post in the thread where you asked how you could contain a struct inside a linked list.

  9. #24
    Registered User
    Join Date
    Apr 2012
    Location
    Malang, Indonesia
    Posts
    22
    Now you know my case. What I mean is like struct node which is linked list with other node contain struct student which is linked list with another student.But now I know I can't limit struct student (and other linked list student) to just make in 1 node. It just make 2 linked list. I got a problem when I want to print the list. Can you help me fix that? Or do I must declare struct student as array?

  10. #25
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    Would you mind explaining why a simple linked list of students won't work? I mean, to me, this new requirement is just needlessly complicated.

    If the data you want to put in the list is already in some other list, then just copy it, if you really have to.

  11. #26
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    My impression is that besides the language barrier your problem is that you are typedef-ing without understanding what it does, so you are typedef-ing your node to be a list. The solution is STOP USING TYPEDEF.
    Declare your data structures as follows:

    Code:
    struct student{
        /* blah blah student data */
    };
    
    struct node{
        struct student data; /* or if you prefer struct student* data; */
        struct node* next;
    };
    
    struct list{
      struct node* head_node;
      unsigned int num_nodes;
      /* if you want to get fancy you can also keep track of the tail, depending where you want to insert new nodes */
    };
    Last edited by claudiu; 04-18-2012 at 02:59 AM.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #27
    Registered User
    Join Date
    Apr 2012
    Location
    Malang, Indonesia
    Posts
    22
    Quote Originally Posted by whiteflags View Post
    Would you mind explaining why a simple linked list of students won't work? I mean, to me, this new requirement is just needlessly complicated.

    If the data you want to put in the list is already in some other list, then just copy it, if you really have to.
    Well it works. But I got confused when trying to print the node and the student. Basically I want to put some struct student to struct node. But I got realised now struct student and struct node is 2 linked list. The problem was shown in the printlist function. Example : I have 2 node (a and b). And I want put student in node a. When I try to print list, student will also printed in node b. I can' get rid of it for now.

    Quote Originally Posted by claudiu View Post
    My impression is that besides the language barrier your problem is that you are typedef-ing without understanding what it does, so you are typedef-ing your node to be a list. The solution is STOP USING TYPEDEF.
    Declare your data structures as follows:

    Code:
    struct student{
        /* blah blah student data */
    };
    
    struct node{
        struct student data; /* or if you prefer struct student* data; */
        struct node* next;
    };
    
    struct list{
      struct node* head_node;
      unsigned int num_nodes;
      /* if you want to get fancy you can also keep track of the tail, depending where you want to insert new nodes */
    };
    Whats wrong with my tipedef? typedef just for aliasing, right?

  13. #28
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I looked again at the code you posted initially. I really don't understand what you are trying to do. I don't understand your variable names, so just by looking at structure it looks to me like mhs is your node, and "kelas" is your list. If so why do you have another "next" in the list?

    You don't seem to understand what linked list is, I suggest you Google it, read it, understand it, write a simple linked list program so that you can figure out what is going on and then come back to your problem.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  14. #29
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Now that you have solved the problem... albeit in a kludgy way, I suggest you try finding a data structure more suitable for this.

    I'm not sure, but S-expressions, though made for a different purpose, seem to fit the bill.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  15. #30
    Registered User
    Join Date
    Apr 2012
    Location
    Malang, Indonesia
    Posts
    22
    @claudiu
    kelas is my node and mhs is my list. I point *next on my list is because I want to put 2 and more list in 1 node. Thats why I consider to change my list as array. I already making simple linked list. I understand a little

    @manasij7479
    I never know about s-expression but I will try to look at it.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list inside a structure
    By jerepops in forum C Programming
    Replies: 5
    Last Post: 12-14-2009, 07:22 PM
  2. Linked List inside Linked List
    By de4th in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2006, 11:17 AM
  3. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 05:37 PM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  5. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 04:16 PM

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