Guest User

Untitled

a guest
Jan 24th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. struct Employee{
  6.     char firstName[256];
  7.     char lastName[256];
  8.     int age;
  9.     int sinNumber;
  10.     char status;
  11. };
  12.  
  13. struct Node{
  14.     struct Employee *employee;
  15.     struct Node *next;
  16. };
  17.  
  18. void initilizeList(struct Node** head){
  19.     *head = (struct Node*) malloc(sizeof(struct Node));
  20.     (*head)->next = NULL;
  21.     //(*head)->employee = (struct Employee*) malloc (sizeof(struct Employee));
  22. }
  23.  
  24. //Given a list and the data of the new employee it adds it to the end of the list
  25. void addEmployeeEnd(struct Node** head, char firstName[256], char lastName[256], int age, int sinNum, char status){
  26.     struct Node *newNode;
  27.     initilizeList(&newNode);
  28.    
  29.     //initialize employee and the data
  30.     struct Employee *employee = (struct Employee*)malloc(sizeof(struct Employee));
  31.    
  32.     strcpy(employee->firstName, firstName);
  33.     strcpy(employee->lastName, lastName);
  34.     employee->age = age;
  35.     employee->sinNumber = sinNum;
  36.     employee->status = status;
  37.  
  38.     //link the node with the new employee struct
  39.     newNode->employee = employee;
  40.    
  41.     //if the list is empty make it the first element
  42.     if (*head == NULL){
  43.         *head = newNode;
  44.         return;
  45.     }
  46.  
  47.     //navigate to the end of the list
  48.     struct Node* current;
  49.     for(current = *head;current->next!=NULL;current=current->next);
  50.  
  51.     //add newNode to the end of the list
  52.     current->next = newNode;
  53.     newNode->next = NULL;
  54. }
Add Comment
Please, Sign In to add comment