Guest User

Untitled

a guest
Jan 24th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.21 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(newNode->employee->firstName, firstName);
  33.     strcpy(newNode->employee->lastName, lastName);
  34.     newNode->employee->age = age;
  35.     newNode->employee->sinNumber = sinNum;
  36.     newNode->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. }
  54.  
  55. //Given a list and the data of the new employee it adds it to the front of the list
  56. void addEmployeeFront(struct Node** head, char firstName[256], char lastName[256], int age, int sinNum, char status){
  57.     struct Node *newNode;
  58.     initilizeList(&newNode);
  59.    
  60.     //initialize employee and the data
  61.     //struct Employee *employee = (struct Employee*)malloc(sizeof(struct Employee));
  62.    
  63.     strcpy(newNode->employee->firstName, firstName);
  64.     strcpy(newNode->employee->lastName, lastName);
  65.     newNode->employee->age = age;
  66.     newNode->employee->sinNumber = sinNum;
  67.     newNode->employee->status = status;
  68.  
  69.     //link the node with the new employee struct
  70.     //newNode->employee = employee;
  71.    
  72.     //if the list is empty make it the first element
  73.     if (*head == NULL){
  74.         *head = newNode;
  75.         return;
  76.     }
  77.  
  78.     //add it to the front of the list
  79.     newNode->next = *head;
  80.     *head = newNode;
  81. }
  82.  
  83. /*
  84. int getnode(){
  85.  
  86.  
  87. }*/
  88.  
  89. //Removes an (single?) employee of the given lastName or SIN number
  90. void removeEmployee(struct Node** _head, char _firstName [256], int _sinNum){
  91.     struct Node* current = *_head;
  92.     struct Node* temp;
  93.     while(current!=NULL){
  94.         //if one of the other is true don't need to check which one the user wants to use because they will not equal anywyas
  95.         if (strcmp(current->next->employee->firstName, _firstName) == 0 || current->next->employee->sinNumber == _sinNum){
  96.             temp = current->next; //holds the node to be freed
  97.             current->next = current->next->next; //deletes the employee
  98.             free(temp);
  99.         }
  100.         //only if you don't delete an employee
  101.         else{
  102.             current = current ->next;
  103.         }
  104.     }
  105. }
  106.  
  107. //Duplicates each nodes so that there is _times amount of each node
  108. void Reapeated(struct Node* _head, int _times){
  109.     struct Node* current = _head;
  110.     //holds the new list with the correct amount of each node
  111.     struct Node* result;
  112.     initilizeList(&result);
  113.    
  114.     while(current!=NULL){
  115.         int i;
  116.         //adds the current->employee to the end of the list _times-1 times
  117.         //since there is already one already in the list
  118.         for (i = 1;i<_times;i++){          
  119.             addEmployeeEnd(&result, current->employee->firstName, current->employee->lastName, current->employee->age, current->employee->sinNumber, current->employee->status);
  120.         }
  121.         current=current->next;
  122.     }
  123.     _head = result;
  124. }
  125.  
  126. //Deletes the entire list by free each element and settign _head to NULL
  127. void deleteList(struct Node** _head){
  128.     struct Node* current = *_head;
  129.     struct Node* temp;
  130.     while(current!=NULL){
  131.         temp = current;
  132.         current = current->next;
  133.         //free(temp->employee);
  134.         free(temp);
  135.         //temp=NULL;
  136.     }
  137.     *_head = NULL;
  138. }
  139.  
  140. //Deletes every elements except the one(s) with the given last name
  141. void deleteExpt(struct Node** _head, char _lastName[256]){
  142.     //used to hold the new list with only the elements with the given last name
  143.     struct Node *newHead;
  144.     initilizeList(&newHead);
  145.     //newHead = NULL;
  146.  
  147.     //used to hold the current position
  148.     struct Node *current = *_head;
  149.        
  150.     while(current!=NULL){
  151.         if (strcmp(current->employee->lastName, _lastName)==0){
  152.             addEmployeeEnd(&newHead, current->employee->firstName, current->employee->lastName, current->employee->age, current->employee->sinNumber, current->employee->status);
  153.         }
  154.         current = current ->next;
  155.     }
  156.     //deletes old list
  157.     deleteList(_head);
  158.     //changes teh old list to be the new list
  159.     *_head = newHead;
  160. }
  161.  
  162. //Splits a single linked list into two double linked lists with even elements with the extra going in the first section
  163. void divideList (struct Node **source, struct Node** first, struct Node** second){
  164.     struct Node* current = *source;
  165.    
  166.     //length function?
  167.     int length = 0;
  168.     while(current!=NULL){
  169.         length++;
  170.         current=current->next;
  171.     }
  172.     //for (current = source;current!=NULL;current=current->next){
  173.     //  length++;
  174.     //}
  175.  
  176.     //length now contains the amount of elements in the list
  177.  
  178.     //if all elements are added to the first list
  179.     if(length<2){
  180.         *first = *source;
  181.         *second = NULL;
  182.     }
  183.     else{
  184.         //changes length so the first list has the extra element if needed
  185.         length = (length -1)/2;
  186.        
  187.         current = *source;
  188.         int i;
  189.         //navigates untill the lengthth element which is where the lists are to be split
  190.         for(i = 0;i<length;i++){
  191.             current = current->next;
  192.         }
  193.         //first gets the whole list
  194.         *first = *source;
  195.         //second gets the list after current
  196.         *second = current->next;
  197.         //this splits the two lists
  198.         current->next = NULL;
  199.     }
  200. }
  201.  
  202. //Displays all of the employess in the list
  203. void displayList(struct Node *_head){
  204.     struct Node* current;
  205.     for(current = _head;current!=NULL;current=current->next)
  206.         printf("First Name: %s\nLast Name: %s\nAge: %d\nSIN Number: %d\nStatus: %s\n\n", current->employee->firstName, current->employee->lastName, current->employee->age, current->employee->sinNumber, current->employee->status=='a'?"Active":(current->employee->status=='s'?"Suspended":"Terminated"));
  207.         //printf("%s, %s\n%d\n%d\n%c\n", current->employee->lastName ? current->employee->lastName : "NULL", current->employee->firstName ? current->employee->firstName : "NULL", current->employee->age, current->employee->sinNumber, current->employee->status);
  208.     printf("\n");
  209. }
  210.  
  211. //Displays all of the employees in the list that are older than _age
  212. void displayList1(struct Node* _head, int _age){
  213.     struct Node* current;
  214.     for (current = _head;current !=NULL; current= current->next){
  215.         if (current->employee->age > _age)
  216.             printf("First Name: %s\nLast Name: %s\nAge: %d\nSIN Number: %d\nStatus: %s\n\n", current->employee->firstName, current->employee->lastName, current->employee->age, current->employee->sinNumber, current->employee->status=='a'?"Active":(current->employee->status=='s'?"Suspended":"Terminated"));
  217.         }
  218.     printf("\n");
  219. }
  220.  
  221. //displays all of the employees in the list that have the same status as _status
  222. void displayList2(struct Node* _head, char _status){
  223.     struct Node* current = _head;
  224.     while(current !=NULL){
  225.         if (current->employee->status == _status)
  226.             //displayEmployee
  227.             printf("First Name: %s\nLast Name: %s\nAge: %d\nSIN Number: %d\nStatus: %s\n\n", current->employee->firstName, current->employee->lastName, current->employee->age, current->employee->sinNumber, current->employee->status=='a'?"Active":(current->employee->status=='s'?"Suspended":"Retired"));
  228.         current = current->next;
  229.     }
  230.     printf("\n");
  231. }
  232.  
  233.  
  234. int main (){
  235.     //head of the employee records
  236.     struct Node *head;
  237.    
  238.     initilizeList(&head);
  239.  
  240.     //used for user input
  241.     int age;
  242.     int sinNum;
  243.     char firstName [256];
  244.     char lastName [256];
  245.     char status;
  246.  
  247.     char temp;//used to deal with scanfs extra 'enter' character
  248.    
  249.     //testing only
  250.     //addEmployeeFront(&head, "real", "person", 19, 9182, 'a');
  251.     //addEmployeeFront(&head, "foo", "bar", 22, 5555, 'a');
  252.     //addEmployeeFront(&head, "test", "erson", 55, 9911, 's');
  253.     //addEmployeeFront(&head, "forth", "toStart", 10, 6543, 't');
  254.     //end
  255.  
  256.  
  257.     int i;
  258.     //loop to get user input for each employee and add them to the list
  259.     for (i =1;;i++){
  260.         printf("Enter the first name of Employee #%d. Enter 'q' to stop entering employees.\n", i);
  261.         scanf("%s%c", firstName, &temp);
  262.         //strcpy(firstName, "q");
  263.         if (strcmp(firstName,"q")==0)
  264.             break;
  265.        
  266.         printf("Enter his/her last name.\n");
  267.         scanf("%s%c", lastName, &temp);
  268.  
  269.         printf("Enter his/her age\n");
  270.         scanf("%d%c", &age, &temp);
  271.  
  272.         printf("Enter his/her SIN number\n");
  273.         scanf("%d%c", &sinNum, &temp);
  274.  
  275.         printf("Enter the status of the employee 'a' active, 's' suspended, 't' terminated.\n");
  276.         scanf("%c%c", &status, &temp);
  277.        
  278.         addEmployeeFront(&head, firstName, lastName, age, sinNum, status);
  279.         //displayList(head);
  280.     }
  281.  
  282.     displayList(head);
  283.     /*printf("\n test\n");
  284.     struct Node* front = (struct Node*) malloc (sizeof(struct Node));
  285.    
  286.     struct Node* back = (struct Node*) malloc (sizeof(struct Node));
  287.  
  288.     divideList(&head, &front, &back);
  289.     displayList(front);
  290.     displayList(back);*/
  291.  
  292.     //displayList1(head, 50);
  293.     //displayList2(head, 'a');
  294.  
  295.     return 0;
  296. }
Add Comment
Please, Sign In to add comment