Thread: Bad outputs from linked list

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    6

    Bad outputs from linked list

    I'm trying to get this program to print out the employee with the highest ID number. But instead, it prints out all of the employees, with the last name is in the first name position, and the last name position never gets filled. I've been working on this for a while now. I'm new to programming and i am having a hard time with linked lists. Any help would be appreciated.
    Code:
    #include <stdio.h>
    #include <stdlib.h>       
    #include <string.h>
    #define NO_APPTS 4
    
    
    typedef struct work
      {char first_name[21];
       char last_name[21];
       int id;
       struct work *next;
      }APPT;
    
    
    
    
    APPT* insertit(APPT*first, char first_name[], char last_name[], int id);
    
    
    int main(void)
    {
      APPT *head,                
           *p;                   
      char fname[21];
      char lname[21];
      int  ident,
           j,                    
           k;
      char ch,                   
           nl;                   
      head=NULL;
      for(j=0;j<21;j++)
      {fname[j]=' ';
      lname[j]=' ';
      }
     for(k=0;k<NO_APPTS;k++)
       {printf("Enter the first name of the employee\n");
        printf("is to be made...max 20 characters  :");
        gets(fname);
        printf("Enter the last name of the employee  ");
        printf("is to be made...max 20 characters  :");
        gets(lname);
        printf("Enter the 3 digit employee id number:  ");
        scanf("%d", &ident);
        scanf("%c", &nl);
        head=insertit (head,fname,lname,ident);
     
        for (j=0;j<21;j++)
          {fname[j]=' ';
          lname[j]= ' ';}
       }                        
    
    
       p=head;               
       while (p!=NULL)
         {printf("The first name of the employee is: %s  ", p->first_name);
          printf("The last name of the employee is: %s ", p->last_name);
          printf("The 3 digit id number of the empoyee is: %d\n",p->id);
          p=p->next;
         }
       printf("Hit any character to continue");
       scanf("%c",&ch);
    }
    
    
    APPT* insertit (APPT *first, char first_name[], char last_name[], int ident)
    
    
    { APPT *p,
           *q,
           *newp;
     int found,
          len,
          i;
      found=0;
      q=first;
      p=first;
    
    
      
    
    
      while ((p!=NULL) && (!found))
        { if (p->id < ident)
              {
               q=p;
               p=p->next;
              }
          else
            found=1;
        }
    
    
      newp=(APPT*) malloc(sizeof(APPT));   
      newp->id=ident;
      strncpy(newp->first_name, last_name, 21);
      newp->next=p;
      if(q!=p)
        q->next=newp;  
     else                                
       first=newp;
      return(first);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > with the last name is in the first name position, and the last name position never gets filled.
    You only have ONE strcpy, and it is first_name = last_name
    strncpy(newp->first_name, last_name, 21);
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread