Guest User

Untitled

a guest
Jan 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.72 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 = NULL;
  21.     //_head->employee = initEmployee
  22. }
  23.  
  24. //given a list and the data of the new employee it adds it to the end of the list
  25. int addEmployeeEnd(struct Node** _head, char _firstName[256], char _lastName[256], int _age, int _sinNum, char _status){
  26.     struct Node *newNode;
  27.     newNode = (struct Node*)malloc (sizeof(struct Node));
  28.    
  29.     //initialize employee
  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 1;
  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.  
  55.     return 1;
  56. }
  57.  
  58. //given a list and the data of the new employee it adds it to the front of the list
  59. int addEmployeeFront(struct Node** _head, char _firstName[256], char _lastName[256], int _age, int _sinNum, char _status){
  60.     struct Node *newNode;
  61.     newNode = (struct Node*)malloc (sizeof(struct Node));
  62.    
  63.     //initialize employee
  64.     struct Employee *employee = (struct Employee*)malloc(sizeof(struct Employee));
  65.    
  66.     strcpy(employee->firstName, _firstName);
  67.     strcpy(employee->lastName, _lastName);
  68.     employee->age = _age;
  69.     employee->sinNumber = _sinNum;
  70.     employee->status = _status;
  71.  
  72.     //link the node with the new employee struct
  73.     newNode->employee = employee;
  74.    
  75.     //if the list is empty make it the first element
  76.     if (*_head == NULL){
  77.         *_head = newNode;
  78.         return 1;
  79.     }
  80.  
  81.     //add it to the front of the list
  82.     newNode->next = *_head;
  83.     *_head = newNode;
  84.  
  85.     return 1;
  86. }
  87.  
  88. /*
  89. int getnode(){
  90.  
  91.  
  92. }*/
  93.  
  94. //removes an (single?) employee of the given lastName or SIN number
  95. void removeEmployee(struct Node** _head, char _firstName [256], int _sinNum){
  96.     struct Node* current = *_head;
  97.     struct Node* temp;
  98.     while(current!=NULL){
  99.         if (strcmp(current->next->employee->firstName, _firstName) == 0 || current->next->employee->sinNumber == _sinNum){
  100.             temp = current->next;
  101.             current->next = current->next->next;
  102.             free(temp);
  103.         }
  104.         else{
  105.             current = current ->next;
  106.         }
  107.     }
  108. }
  109.  
  110. //duplicates each nodes so that there is _times amount of each node
  111. int Reapeated(struct Node* _head, int _times){
  112.     struct Node* current = _head;
  113.     struct Node* result = (struct Node*) malloc (sizeof(struct Node));
  114.     result = NULL;
  115.     //int count = 0;
  116.     while(current!=NULL){
  117.         int i;
  118.         for (i = 1;i<_times;i++){
  119.             //adds the current->employee to the end of the list _times-1 times
  120.             //since there is already one already in the list
  121.             addEmployeeEnd(&result, current->employee->firstName, current->employee->lastName, current->employee->age, current->employee->sinNumber, current->employee->status);
  122.         }
  123.         current=current->next;
  124.         //count++;
  125.     }
  126.     _head = result;
  127.     return 1;
  128. }
  129.  
  130. //deletes every elements except the one(s) with the given last name
  131. void deleteExpt(struct Node** _head, char _lastName[256]){
  132.     struct Node *newHead;
  133.     newHead = (struct Node*) malloc (sizeof(struct Node));
  134.    
  135.     struct Node *current;
  136.     current = (struct Node*) malloc (sizeof(struct Node));
  137.  
  138.     int count = 0;
  139.  
  140.     while((*_head)->next!=NULL){
  141.         if (strcmp((*_head)->employee->lastName, _lastName)==0 && count ==0){
  142.             newHead = *_head;
  143.             newHead->next = current;
  144.             count++;
  145.         }
  146.         else if (strcmp((*_head)->employee->lastName, _lastName)==0){
  147.             //struct Node *temp;
  148.             //temp = (struct Node*) malloc (sizeof(struct Node));
  149.             current->next = *_head;
  150.             current = current ->next;
  151.         }
  152.     }
  153.  
  154.     *_head = newHead;
  155. }
  156.  
  157. //deletes the entire list by free each element and settign head to NULL
  158. void deleteList(struct Node** _head){
  159.     struct Node* current = *_head;
  160.     struct Node* temp;
  161.     while(current!=NULL){
  162.         temp = current;
  163.         current = current->next;
  164.         //free(temp->employee);
  165.         free(temp);
  166.         //temp=NULL;
  167.     }
  168.     *_head = NULL;
  169. }
  170.  
  171. //splits a single linked list into two linked lists with even elements with the extra going in the first section
  172. void divideList (struct Node *source, struct Node** first, struct Node** second){
  173.     struct Node* current;
  174.    
  175.     //length function?
  176.     int length = 0;
  177.     for (current = source;current!=NULL;current=current->next){
  178.         length++;
  179.     }
  180.  
  181.     if(length<2){
  182.         *first = source;
  183.         *second = NULL;
  184.     }
  185.     else{
  186.         //changes length so the first list has the extra element if needed
  187.         length = (length -1)/2;
  188.         current = source;
  189.         int i;
  190.         for(i = 0;i<length;i++){
  191.             current = current->next;
  192.         }
  193.  
  194.         *first = source;
  195.         *second = current->next;
  196.         current->next = NULL;
  197.     }
  198. }
  199.  
  200. //displays all of the employess in the list
  201. void displayList(struct Node *_head){
  202.     struct Node* current;
  203.     for(current = _head;current!=NULL;current=current->next)
  204.         //printf("%s, %s\n%d\n%d\n%c\n", current->employee.lastName, current->employee.firstName, current->employee.age, current->employee.sinNumber, current->employee.status);
  205.         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);
  206.     printf("\n");
  207. }
  208.  
  209. //displays all of the employees in the list that are older than _age
  210. void displayList1(struct Node* _head, int _age){
  211.     struct Node* current;
  212.     for (current = _head;current !=NULL; current= current->next){
  213.         if (current->employee->age > _age)
  214.             //displayEmployee
  215.             printf("%s, %s\n%d\n%d\n%c\n", current->employee->lastName, current->employee->firstName, current->employee->age, current->employee->sinNumber, current->employee->status);
  216.     }
  217.     printf("\n");
  218. }
  219.  
  220. //displays all of the employees in the list that have the same status as _status
  221. void displayList2(struct Node* _head, char _status){
  222.     struct Node* current = _head;
  223.     while(current !=NULL){
  224.         if (current->employee->status == _status)
  225.             //displayEmployee
  226.             printf("First Name: %s\nLast Name: %s\nAge: %d\nSIN Number: %d\nStatus: %s\n", current->employee->lastName, current->employee->firstName, current->employee->age, current->employee->sinNumber, current->employee->status=='a'?"Active":(current->employee->status=='s'?"Suspended":"Retired"));
  227.         current = current->next;
  228.     }
  229.     printf("\n");
  230. }
  231.  
  232.  
  233. int main (){
  234.     struct Node *head;
  235.    
  236.     head = (struct Node*) malloc (sizeof(struct Node));
  237.     head = NULL;
  238.     //initilizeList(head);
  239.  
  240.     int age;
  241.     int sinNum;
  242.     char firstName [256];
  243.     char lastName [256];
  244.     char status;
  245.  
  246.     char temp;
  247.    
  248.     int i;
  249.     for (i =1;;i++){
  250.         printf("Enter the first name of Employee #%d. Enter 'q' to stop entering employees.\n", i);
  251.         scanf("%s%c", firstName, &temp);
  252.        
  253.         if (strcmp(firstName,"q")==0)
  254.             break;
  255.        
  256.         printf("Enter his/her last name.\n");
  257.         scanf("%s%c", lastName, &temp);
  258.  
  259.         printf("Enter his/her age\n");
  260.         scanf("%d%c", &age, &temp);
  261.  
  262.         printf("Enter his/her SIN number\n");
  263.         scanf("%d%c", &sinNum, &temp);
  264.  
  265.         printf("Enter the status of the employee 'a' active, 's' suspended, 't' terminated.\n");
  266.         scanf("%c%c", &status, &temp);
  267.        
  268.         addEmployeeFront(&head, firstName, lastName, age, sinNum, status);
  269.         //displayList(head);
  270.     }
  271.  
  272.     displayList(head);
  273.  
  274.     //displayList1(head, 50);
  275.     //displayList2(head, 'a');
  276.  
  277.     return 0;
  278. }
Add Comment
Please, Sign In to add comment