Guest User

Untitled

a guest
Jan 24th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. //Employee struct holds employee value
  6. struct Employee{
  7.     char firstName[256];
  8.     char lastName[256];
  9.     int age;
  10.     int sinNumber;
  11.     char status;
  12. };
  13.  
  14. //Node struct holds an Employee struct as data and a next node
  15. struct Node{
  16.     struct Employee *employee;
  17.     struct Node *next;
  18. };
  19.  
  20. //initilizes the List
  21. void initilizeList(struct Node** head){
  22.     *head = (struct Node*) malloc(sizeof(struct Node));
  23.     (*head)->next = NULL;
  24.     (*head)->employee = (struct Employee*) malloc (sizeof(struct Employee));
  25. }
  26.  
  27. //Given a list and the data of the new employee it adds it to the end of the list
  28. void addEmployeeEnd(struct Node** head, char firstName[256], char lastName[256], int age, int sinNum, char status){
  29.     struct Node *newNode;
  30.     initilizeList(&newNode);
  31.    
  32.     //initialize the employee data
  33.     strcpy(newNode->employee->firstName, firstName);
  34.     strcpy(newNode->employee->lastName, lastName);
  35.     newNode->employee->age = age;
  36.     newNode->employee->sinNumber = sinNum;
  37.     newNode->employee->status = status;
  38.    
  39.     //if the list is empty make it the first element
  40.     if (*head == NULL){
  41.         *head = newNode;
  42.         return;
  43.     }
  44.  
  45.     //navigate to the end of the list
  46.     struct Node* current;
  47.     for(current = *head;current->next!=NULL;current=current->next);
  48.  
  49.     //add newNode to the end of the list
  50.     current->next = newNode;
  51. }
  52.  
  53. //Given a list and the data of the new employee it adds it to the front of the list
  54. void addEmployeeFront(struct Node** head, char firstName[256], char lastName[256], int age, int sinNum, char status){
  55.     struct Node *newNode;
  56.     initilizeList(&newNode);
  57.    
  58.     //initialize the employee data
  59.     strcpy(newNode->employee->firstName, firstName);
  60.     strcpy(newNode->employee->lastName, lastName);
  61.     newNode->employee->age = age;
  62.     newNode->employee->sinNumber = sinNum;
  63.     newNode->employee->status = status;
  64.    
  65.     //if the list is empty make it the first element
  66.     if (*head == NULL){
  67.         *head = newNode;
  68.         return;
  69.     }
  70.  
  71.     //add it to the front of the list
  72.     newNode->next = *head;
  73.     *head = newNode;
  74. }
  75.  
  76. /*
  77. int getnode(){
  78.  
  79.  
  80. }*/
  81.  
  82. //Removes an (single?) employee of the given lastName or SIN number
  83. void removeEmployee(struct Node** head, char firstName [256], int sinNum){
  84.     if (*head == NULL)
  85.         return;
  86.     struct Node* current = *head;
  87.     struct Node* prev = current;
  88.     struct Node* temp = NULL;
  89.     do{
  90.         //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
  91.         if (current->employee->sinNumber == sinNum || strcmp(current->employee->firstName, firstName) == 0){
  92.             temp = current; //holds the node to be freed
  93.             prev->next = current->next; //deletes the employee
  94.             free(temp->employee);
  95.             free(temp);
  96.         }
  97.         prev = current;
  98.         current = current->next;
  99.     }while(current->next!=NULL);
  100. }
  101.  
  102. //Duplicates each nodes so that there is _times amount of each node
  103. void Repeated(struct Node** head, int times){
  104.     struct Node* current = *head;
  105.     //holds the new list with the correct amount of each node
  106.     struct Node* result;
  107.     initilizeList(&result);
  108.     result = NULL;
  109.  
  110.    
  111.     while(current!=NULL){
  112.         int i;
  113.         //adds the current->employee to the end of the list _times
  114.         for (i = 0;i<times;i++){           
  115.             addEmployeeEnd(&result, current->employee->firstName, current->employee->lastName, current->employee->age, current->employee->sinNumber, current->employee->status);
  116.         }
  117.         current=current->next;
  118.     }
  119.     *head = result;
  120. }
  121.  
  122. //Deletes the entire list by free each element and settign _head to NULL
  123. void deleteList(struct Node** head){
  124.     struct Node* current = *head;
  125.     struct Node* temp;
  126.     while(current!=NULL){
  127.         temp = current;
  128.         current = current->next;
  129.         free(temp->employee);
  130.         free(temp);
  131.         //temp=NULL;
  132.     }
  133.     *head = NULL;
  134. }
  135.  
  136. //Deletes every elements except the ones with the given last name
  137. void deleteExpt(struct Node** head, char lastName[256]){
  138.     //used to hold the new list with only the elements with the given last name
  139.     struct Node *newHead;
  140.     initilizeList(&newHead);
  141.     newHead = NULL;
  142.  
  143.     //used to hold the current position
  144.     struct Node *current = *head;
  145.        
  146.     while(current!=NULL){
  147.         if (strcmp(current->employee->lastName, lastName)==0){
  148.             addEmployeeEnd(&newHead, current->employee->firstName, current->employee->lastName, current->employee->age, current->employee->sinNumber, current->employee->status);
  149.         }
  150.         current = current ->next;
  151.     }
  152.     //deletes old list
  153.     deleteList(head);
  154.     //changes the old list to be the new list
  155.     *head = newHead;
  156. }
  157.  
  158. //converts a linked list into a circular linked list
  159. void toCircular(struct Node **head){
  160.     struct Node* current = *head;
  161.     if (*head == NULL)
  162.         return;
  163.     while(current->next!=NULL){
  164.         current = current->next;
  165.     }
  166.  
  167.     current ->next = *head;
  168. }
  169.  
  170. //Splits a circular linked list into two circular linked lists with even elements with the extra going in the first section
  171. void divideList (struct Node **source, struct Node** first, struct Node** second){
  172.     struct Node* current = *source;
  173.    
  174.     int length = 0;
  175.     while(current!=NULL){
  176.         length++;
  177.         current=current->next;
  178.     }
  179.     //length now contains the amount of elements in the list
  180.  
  181.     //if all elements are added to the first list
  182.     if(length<2){
  183.         *first = *source;
  184.         *second = NULL;
  185.     }
  186.     else{
  187.         //changes length so the first list has the extra element if needed
  188.         length = (length -1)/2;
  189.        
  190.         current = *source;
  191.         int i;
  192.         //navigates untill the lengthth element which is where the lists are to be split
  193.         for(i = 0;i<length;i++){
  194.             current = current->next;
  195.         }
  196.         //first gets the whole list
  197.         *first = *source;
  198.         //second gets the list after current
  199.         *second = current->next;
  200.         //this splits the two lists
  201.         current->next = NULL;
  202.     }
  203. }
  204.  
  205. //Displays all of the employess in the list
  206. void displayList(struct Node *head){
  207.     struct Node* current;
  208.     for(current = head;current!=NULL;current=current->next)
  209.         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"));
  210.     printf("\n");
  211. }
  212.  
  213. //Displays all of the employees in the list that are older than _age
  214. void displayList1(struct Node* head, int age){
  215.     struct Node* current;
  216.     for (current = head;current !=NULL; current= current->next){
  217.         if (current->employee->age > age)
  218.             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"));
  219.     }
  220.     printf("\n");
  221. }
  222.  
  223. //displays all of the employees in the list that have the same status as _status
  224. void displayList2(struct Node* head, char status){
  225.     struct Node* current = head;
  226.     while(current !=NULL){
  227.         if (current->employee->status == status)
  228.             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"));
  229.         current = current->next;
  230.     }
  231.     printf("\n");
  232. }
  233.  
  234. int main (){
  235.     //head of the employee records
  236.     struct Node *head;
  237.     initilizeList(&head);
  238.     head= NULL;
  239.     //used for user input
  240.     int age;
  241.     int sinNum;
  242.     char firstName [256];
  243.     char lastName [256];
  244.     char status;
  245.  
  246.     char temp;//used to deal with scanfs extra 'enter' character
  247.    
  248.     //testing only
  249.     addEmployeeFront(&head, "real", "person", 19, 9182, 'a');
  250.     addEmployeeFront(&head, "foo", "bar", 22, 5555, 'a');
  251.     addEmployeeFront(&head, "test", "erson", 55, 9911, 's');
  252.     addEmployeeFront(&head, "forth", "toStart", 10, 6543, 't');
  253.     //end
  254.    
  255.     int i;
  256.     //loop to get user input for each employee and add them to the list
  257.     for (i =1;;i++){
  258.         printf("Enter the first name of Employee #%d. Enter 'q' to stop entering employees.\n", i);
  259.         scanf("%s%c", firstName, &temp);
  260.         //strcpy(firstName, "q");
  261.         if (strcmp(firstName,"q")==0)
  262.             break;
  263.        
  264.         printf("Enter his/her last name.\n");
  265.         scanf("%s%c", lastName, &temp);
  266.  
  267.         printf("Enter his/her age\n");
  268.         scanf("%d%c", &age, &temp);
  269.  
  270.         printf("Enter his/her SIN number\n");
  271.         scanf("%d%c", &sinNum, &temp);
  272.  
  273.         printf("Enter the status of the employee 'a' active, 's' suspended, 't' terminated.\n");
  274.         scanf("%c%c", &status, &temp);
  275.        
  276.         addEmployeeEnd(&head, firstName, lastName, age, sinNum, status);
  277.     }
  278.  
  279.     printf("\nDisplaying all Employees in the List\n");
  280.     displayList(head);
  281.  
  282.     printf("Displaying all Employees above the age of 50 in the List\n");
  283.     displayList1(head, 50);
  284.     printf("Displaying all Employees with the status of Active in the List\n");
  285.     displayList2(head, 'a');
  286.  
  287.     int method;
  288.     printf("To REMOVE an Employee from the list first select the criteria\n");
  289.     printf("0 - Do not REMOVE an Employee\n");
  290.     printf("1 - by first name\n");
  291.     printf("2 - by SIN number\n");
  292.     scanf("%d%c", &method, &temp);
  293.  
  294.     if (method ==1){
  295.         printf("Enter the first name of the employee you would like to REMOVE\n");
  296.         scanf("%s%c", firstName, &temp);
  297.         removeEmployee(&head, firstName, -1);
  298.     }
  299.     else if (method == 2){
  300.         printf("Enter the SIN number of the employee you would like to REMOVE\n");
  301.         scanf("%d%c", &sinNum, &temp);
  302.         removeEmployee(&head, NULL, sinNum);
  303.     }
  304.    
  305.     displayList(head);
  306.    
  307.     int times;
  308.     printf("Enter the number of times you would like the elements in the list to be repeated\n");
  309.     scanf("%d%c", &times, &temp);
  310.  
  311.     Repeated(&head, times);
  312.  
  313.     displayList(head);
  314.    
  315.     printf("To REMOVE ALL Employees except for those with a specified last name enter 1\n");
  316.     printf("0 - Do not REMOVE any Employees\n");
  317.     printf("1 - specify last name and delete all other employees\n");
  318.     scanf("%d%c", &method, &temp);
  319.  
  320.     if (method ==1){
  321.         printf("Enter the last name name of the employee you would like to KEEP\n");
  322.         scanf("%s%c", lastName, &temp);
  323.         deleteExpt(&head, lastName);
  324.     }
  325.    
  326.     displayList(head);
  327.    
  328.     //converts the list into a circular linked list
  329.     printf("Now converting the linked list into a circular linked list\n");
  330.     toCircular(&head);
  331.     //two new lists to hold the front and back of the entire list head after it is divided
  332.     struct Node* front = (struct Node*) malloc (sizeof(struct Node));
  333.     struct Node* back = (struct Node*) malloc (sizeof(struct Node));
  334.  
  335.     //splits the double linked list head into the double linked lists front and back
  336.     printf("Now splitting the circular linked list into two lists\n");
  337.     divideList(&head, &front, &back);
  338.    
  339.     printf("\nDisplaying the Front List\n");
  340.     displayList(front);
  341.     printf("\nDisplaying the Back List\n");
  342.     displayList(back);
  343.  
  344.     //free everything
  345.     deleteList(&head);
  346.     deleteList(&front);
  347.     deleteList(&back);
  348.  
  349.     return 0;
  350. }
Add Comment
Please, Sign In to add comment