Linked list help

This is a discussion on Linked list help within the C Programming forums, part of the General Programming Boards category; I'm writing a program that stores employee information which includes their first name, last name, and 3-digit id number. The ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    21

    Linked list help

    I'm writing a program that stores employee information which includes their first name, last name, and 3-digit id number. The program is supposed to go through the list then display the information for the employee with the lowest id number. Currently I can input the employee information without an issue, but the program can't handle finding the correct employee and displaying it.
    Code:
    #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      typedef struct employee
      {char first_name[20];
       char last_name[20];
       int id;
       struct employee *next;
      }emp;
    
      emp* insertit(emp *first, char firstn[], char last[], int empid);
    
      int main(void)
      {
        emp *head,
            *p;
        char empf[20],
             empl[20];
        int emplid, 
            j,
            no_emps,
            k,
            l;
        char ch,
             junk,
             nl;
        head=NULL;
        printf("Enter the number of employees: ");
        scanf("%d", &no_emps);
        scanf("%c", &junk);
        for(j=0;j<20;j++)
          empf[j]=' ';
        for(k=0;k<20;k++)
          empl[k]=' ';
        for(l=0;l<no_emps ;l++)
         {printf("Enter first name of employee:");
          gets(empf);
          printf("Enter last name of employee:");
          gets(empl);
          printf("Enter the 3 digit id number of employee:");
          scanf("%d", &emplid);
          scanf("%c", &nl);
        head=insertit (head, empf, empl, emplid);
        for(j=0;j<20;j++)
          empf[j]=' ';
        for(k=0;k<20;k++)
          empl[k]=' ';
         }
        p=head;  
        while (p!=NULL)
         {printf("The name of the employee is %s %s", p->first_name, 
    p->last_name);
          printf("The id number of the employee is %d\n", p->id);
          p=p->next;
         }
        printf("Hit any character to continue");
        scanf("%c", &ch);
      }
      emp* insertit (emp *first, char firstn[], char last[], int empid)
      {
       emp *p, *q, *newp;
       int found, len, i, id;
       found=0;
       q=first;
       p=first;
       while ((p!=NULL) && (!found))
         {if (p->id<id)
           {q=p;
            p=p->next;
           }
          else
            found=1;
         }
       newp=(emp*) malloc(sizeof (emp));
       newp->id=empid;
       strncpy(newp->first_name, first, 20);
       strncpy(newp->last_name, last, 20);
       newp->next=p;
       if(q!=p)
         q->next=newp;
       else 
         first=newp;
       return (first);
    }
    Where's the issue?

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    >> the program can't handle finding the correct employee and displaying it.
    maybe because of this
    Code:
       strncpy(newp->first_name, firstn, 20);
    Kurt

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    21
    So what are you suggesting 'firstn' be replaced with?

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I'm suggesting to replace first ( a pointer to emp ) with firstn ( a string ).
    Kurt

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    I guess I would try to use more functions. Make one function to allocate the nodes, one to free the nodes, one to print the nodes, and one to insert data into the nodes. Maybe something like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct employee {
        char first_name[20];
        char last_name[20];
        int id;
        struct employee *next;
    };
    
    struct employee* insertit(struct employee *first, char firstn[], char last[], int empid);
    struct employee *make_node(int);
    void free_node(struct employee*);
    
    int main(void) {
        int no_emp;
        int emplid;
        struct employee *list;
    
        printf("Enter the number of employees: \n");
        if( (scanf("%d", &no_emp)) == 1) 
    	list = make_node(no_emp);
           
        /*printf("Enter first name of employee:");
          printf("Enter last name of employee:");*/
    
        printf("Enter the 3 digit id number of employee:");
        if( (scanf("%d", &emplid)) == 1)
            printf("the emp number is: %d\n", emplid);
    
    
        return 0;
    }
    
    
    struct employee *make_node(int number) {
        struct employee *head = malloc(sizeof(struct employee));
    
        if(head == NULL) {
    	fprintf(stderr, "Out of memory");
        }
    
        while( --number > 0) {
    	head->next = malloc(sizeof *head->next);
    	if(head->next == NULL) {
    	    /*free_node(head);*/
    	    return NULL;
    	}
    
    	else {
    	head = head->next;
    	}
        }
    
        return head;
    }
    At the risk of veering off topic, there are a couple of ways to free the nodes. If you only have a few employees, using a while() loop would work. However, if are planning on like 10,000 employees, it might be better to use recursion to free the nodes.
    Last edited by cdalten; 04-22-2006 at 11:06 AM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > However, if are planning on like 10,000 employees, it might be better to use recursion to free the nodes.
    Ugh!, that's horrible.
    Not only is it more expensive than a loop, the larger the list the more likely you are to run out of stack space.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Quote Originally Posted by Salem
    > However, if are planning on like 10,000 employees, it might be better to use recursion to free the nodes.
    Ugh!, that's horrible.
    Not only is it more expensive than a loop, the larger the list the more likely you are to run out of stack space.
    Want to hear something amusing about the recursion thingy? I know you do, don't deny it. I was told to use recursion for "deep lists" from an Economics Professor at MIT. So maybe even the mega-smart screw it up from time to time?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > I was told to use recursion for "deep lists" from an Economics Professor at MIT
    WTF would an economics professor know about programming?
    Apparently, not much.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,262
    I've heard similar things from similar people. Just because they're a professor of something other than a computing subject, do not assume that somehow they've learnt every other discipline in their spare time.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    21
    Quote Originally Posted by ZuK
    I'm suggesting to replace first ( a pointer to emp ) with firstn ( a string ).
    Kurt
    This change allowed the program to fully run. Right now, however, it simply displays the inputted information in the order it was received. I need it to only display the employee with the lowest id number. How do I go about doing this?

  11. #11
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    try something like this
    Code:
    emp* find_lowest_id( emp * p ) {
       emp * ret = p;
       while ( p )    {
             if ( p->id < ret->id ) 
                   ret = p;
             p=p->next;
        }
        return ret;
    }
    Kurt

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    21
    Quote Originally Posted by ZuK
    try something like this
    Code:
    emp* find_lowest_id( emp * p ) {
       emp * ret = p;
       while ( p )    {
             if ( p->id < ret->id ) 
                   ret = p;
             p=p->next;
        }
        return ret;
    }
    Kurt
    Where in the program should I be implementing this and when?

  13. #13
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I would put that function before main and call it from main like this

    Code:
    p = find_lowest_id( head );
    if ( p ) {  // just in case head would be 0
          printf(" The employee with the lowest id is %s %s\n", p->first_name, p->last_name);
    Kurt

  14. #14
    Registered User
    Join Date
    Oct 2005
    Posts
    21
    Thanks alot. I really appreciate your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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