Thread: Help - Linked List

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    105

    Help - Linked List

    Hello everyone!

    I'm now having some problems with linked lists. Here is the exercise:

    EMPL: employee
    antig: here it says how many years the employee has been working in this company

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct EMPL{
        char NAME[20];
        int antig;
        struct EMPL *next;
        struct EMPL *nextAntig;
    }TEMPL;
    
    struct COMPANY{
        char PLACE[10];
        TEMPL *PRIM;
    };
    
    COMPANY VE[100];
    TEMPL *LPA = NULL;
    
    void create_list_empl(COMPANY v[], TEMPL **L);
    
    
    int main()
    {
        INS_LISTS_EMPL(VE);
        create_list_empl(VE, &LPA);
    }
    But when I try to run this, it has 2 errors:
    Code:
    COMPANY VE[100];
    Code:
    void create_list_empl(COMPANY v[], TEMPL **L);
    it says 'unknown type name COMPANY' in both cases


    What I have to do is to call the function
    Code:
    create_list_empl(VE, &LPA);
    where LPA points to the list of employees sorted by antig using the pointer nextAntig


    I can't use auxiliar structs (arrays, lists or auxiliar files)


    Here's a draw of the exercise:

    Help - Linked List-coloquio2014-jpg


    This what I think I have to do (I don't know if I'm right). I don't know with the pointers where I put a '?'. And I also don't know how to do the sort like I draw.

    Help - Linked List-coloquio2014_creo-jpg


    I know how to work with double pointers and I know how to do some easy exercises, but this is too difficult for me..

    Can you please help me, telling me from where can I study or how to do something?

    Thanks!

    Juan
    Last edited by juanjuanjuan; 02-21-2014 at 10:41 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The typename COMPANY is unknown to me as well, since you didn't define it in the code posted. You defined a type "struct COMPANY", so maybe you intended to use that.

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Yes! In the draw you can see where is VE.

    COMPANY is the one that is in struct COMPANY

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by juanjuanjuan View Post
    COMPANY is the one that is in struct COMPANY
    I believe you have completely missed the point here. You define a thing called "struct COMPANY". This is the name you therefore need to use for that type moving forward:
    Code:
    struct COMPANY VE[100];
    If you want to make "COMPANY" an alias for "struct COMPANY", then you need to use typedef to create the new typename:
    Code:
    typedef struct COMPANY COMPANY;

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    I copied the exercise as I received it. I'm having a lot of troubles with it, that's why I'm asking you

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by juanjuanjuan View Post
    I copied the exercise as I received it. I'm having a lot of troubles with it, that's why I'm asking you
    If you mean the code you posted at the very beginning, the snippet that is 26 lines long, was given to you as is and cannot be changed, then you're sunk. If you wrote it yourself, you need to fix the errors you introduced on lines 16 and 19 as per above.

  7. #7
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    I have to fix it because it doesn't work like it is now. Can you help me?

  8. #8
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Maybe, I don't have to use that line in that way.. Can't I declare an array of 100 elements of the type COMPANY in the main function? If I can do that, I would solve both problems


    What about the other problems? Can you help me? I'm watching a lot of videos and lectures on YouTube and a lot of tutorials I found on Google but I don't catch it.
    Thanks!
    Last edited by juanjuanjuan; 02-22-2014 at 12:09 AM.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Maybe, I don't have to use that line in that way.. Can't I declare an array of 100 elements of the type COMPANY in the main function? If I can do that, I would solve both problems
    C's parser works such that it requires the struct tag for any structure type, unless you typedef it. In other words

    Code:
    typedef struct {
        char PLACE[10];
        TEMPL *PRIM;
    } COMPANY;
    That is the type COMPANY; versions without the typedef are not strictly speaking the type COMPANY.

    What about the other problems? Can you help me? I'm watching a lot of videos and lectures on YouTube and a lot of tutorials I found on Google but I don't catch it.
    You might be at the point in your study that youtube/tutorials are inadequate.

    Still I think the assignment wants you to do a linked list technique called threading. A thread is just another order of the same list, but you have to follow a special pointer to access it. Since nextAntig is sorted, every element that you insert into the linked list is going to have a place in the nextAntig thread.

    The way I would do it:

    Invent a helper function that simply inserts nodes. That means you have a simple goal like:
    Code:
    // Insert n after item:
       if (item == NULL) {
         list = n;
         list->next = NULL;
       }
       else {
         n->next = item->next;
         item->next = n;
       }
    Put that into a function that the linked list, not other programmers, will use.

    Then you have to design a bunch of logic in a function that programmers actually use. On one hand you have to think of the normal order, and on the other you have nextAntig order.


    Code:
    // Rough pseudo code -- I don't actually care about what the return 
    // value is or how this is used
    //
    // Node is a fully initialized employee instance, with next == NULL
    // list is either part of a linked list being added on or a NULL pointer (for a new linked list)
    void insertNode (TEMP* node, TEMP* list)
        {
        if (list == NULL)
           {
           list = node;
           list->nextAntig = node;
           return;
           }
        
        TEMPL* head = list->nextAntig; // head of thread sorted by Antig
        TEMPL* tail = list;
        while (tail->next != NULL) 
           {
           tail = tail->next; 
           }
        insert_n_after (node, tail);
        
        if (head->antig > node->antig)
           {
           node->next = head;
           head = list->nextAntig = node;
           return;
           }
    
        while (head->next != NULL)
            {
            if (head->next->antig < node->antig)
               head = head->next;
           else
               break;
            }
        insert_n_after (node, head);
        return;
        }
    It would be something like that.

    Further reading:
    Last edited by whiteflags; 02-22-2014 at 02:25 AM.

  10. #10
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Thanks for your answers

    I understand all the basics programs for linked lists, but I don't know what to do if I have this:

    I have 3 people and I know how much money they have, how old are them and how many children they have. I have the list like this:

    Help - Linked List-tosort-jpg

    and I want to sort it by the age, like this:

    Help - Linked List-sortedage-jpg

    The program has to print:

    ANDY 15 3000 JOHN 22 100 PETE 30 500


    Please, help me! I'm going to cry. I read all the links you posted here, 3 books and I watched more than 5 hours of YouTube videos, but they didn't help me.

    Thanks
    Last edited by juanjuanjuan; 02-23-2014 at 12:25 PM.

  11. #11
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Please, anyone can help me?

  12. #12
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Looking at the original post code, first fix line 11 by putting typedef before the word struct. The the code shows an array 100 COMPANY's, each with a primary list pointer to the employee structure, and only a single pointer for a list sorted by antig (? what does this stand for). The code does not show where INS_LISTS_EMP is getting the employee data from. Is there a file that your'e supposed to read this data from? Assuming that you've completed the first function and that the COMPANY array is now setup, the next step is to get employee nodes from the company list, one a at a time, and then insert them in sorted order, using the list pointer LPA as the starting pointer to nodes sorted by antig.

  13. #13
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Quote Originally Posted by rcgldr View Post
    Looking at the original post code, first fix line 11 by putting typedef before the word struct. The the code shows an array 100 COMPANY's, each with a primary list pointer to the employee structure, and only a single pointer for a list sorted by antig (? what does this stand for). The code does not show where INS_LISTS_EMP is getting the employee data from. Is there a file that your'e supposed to read this data from? Assuming that you've completed the first function and that the COMPANY array is now setup, the next step is to get employee nodes from the company list, one a at a time, and then insert them in sorted order, using the list pointer LPA as the starting pointer to nodes sorted by antig.
    Thanks!

    Can you help me with the last problem I posted? The one that has 3 names, 3 ages, etc. ?

  14. #14
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by juanjuanjuan View Post
    Can you help me with the last problem I posted? The one that has 3 names, 3 ages, etc. ?
    Create a function that retrieves a node one at a time from the company's lists (it returns NULL when all nodes from all lists have been retrieved). Create another function that inserts nodes in sorted (by antig) order into the list that starts with LPA.

  15. #15
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    I know I have to do that, but I don't know how.

    Please, help me! I have an exam in 8 hours and I don't know how to do this.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 09-22-2013, 10:34 PM
  2. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM