Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- struct Employee{
- char firstName[256];
- char lastName[256];
- int age;
- int sinNumber;
- char status;
- };
- struct Node{
- struct Employee *employee;
- struct Node *next;
- };
- void initilizeList(struct Node** head){
- *head = (struct Node*) malloc(sizeof(struct Node));
- (*head)->next = NULL;
- (*head)->employee = (struct Employee*) malloc (sizeof(struct Employee));
- }
- //Given a list and the data of the new employee it adds it to the end of the list
- void addEmployeeEnd(struct Node** head, char firstName[256], char lastName[256], int age, int sinNum, char status){
- struct Node *newNode;
- initilizeList(&newNode);
- //initialize employee and the data
- //struct Employee *employee = (struct Employee*)malloc(sizeof(struct Employee));
- strcpy(newNode->employee->firstName, firstName);
- strcpy(newNode->employee->lastName, lastName);
- newNode->employee->age = age;
- newNode->employee->sinNumber = sinNum;
- newNode->employee->status = status;
- //link the node with the new employee struct
- //newNode->employee = employee;
- //if the list is empty make it the first element
- if (*head == NULL){
- *head = newNode;
- return;
- }
- //navigate to the end of the list
- struct Node* current;
- for(current = *head;current->next!=NULL;current=current->next);
- //add newNode to the end of the list
- current->next = newNode;
- }
- //Given a list and the data of the new employee it adds it to the front of the list
- void addEmployeeFront(struct Node** head, char firstName[256], char lastName[256], int age, int sinNum, char status){
- struct Node *newNode;
- initilizeList(&newNode);
- //initialize employee and the data
- //struct Employee *employee = (struct Employee*)malloc(sizeof(struct Employee));
- strcpy(newNode->employee->firstName, firstName);
- strcpy(newNode->employee->lastName, lastName);
- newNode->employee->age = age;
- newNode->employee->sinNumber = sinNum;
- newNode->employee->status = status;
- //link the node with the new employee struct
- //newNode->employee = employee;
- //if the list is empty make it the first element
- if (*head == NULL){
- *head = newNode;
- return;
- }
- //add it to the front of the list
- newNode->next = *head;
- *head = newNode;
- }
- /*
- int getnode(){
- }*/
- //Removes an (single?) employee of the given lastName or SIN number
- void removeEmployee(struct Node** _head, char _firstName [256], int _sinNum){
- struct Node* current = *_head;
- struct Node* temp;
- while(current!=NULL){
- //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
- if (strcmp(current->next->employee->firstName, _firstName) == 0 || current->next->employee->sinNumber == _sinNum){
- temp = current->next; //holds the node to be freed
- current->next = current->next->next; //deletes the employee
- free(temp);
- }
- //only if you don't delete an employee
- else{
- current = current ->next;
- }
- }
- }
- //Duplicates each nodes so that there is _times amount of each node
- void Reapeated(struct Node* _head, int _times){
- struct Node* current = _head;
- //holds the new list with the correct amount of each node
- struct Node* result;
- initilizeList(&result);
- while(current!=NULL){
- int i;
- //adds the current->employee to the end of the list _times-1 times
- //since there is already one already in the list
- for (i = 1;i<_times;i++){
- addEmployeeEnd(&result, current->employee->firstName, current->employee->lastName, current->employee->age, current->employee->sinNumber, current->employee->status);
- }
- current=current->next;
- }
- _head = result;
- }
- //Deletes the entire list by free each element and settign _head to NULL
- void deleteList(struct Node** _head){
- struct Node* current = *_head;
- struct Node* temp;
- while(current!=NULL){
- temp = current;
- current = current->next;
- //free(temp->employee);
- free(temp);
- //temp=NULL;
- }
- *_head = NULL;
- }
- //Deletes every elements except the one(s) with the given last name
- void deleteExpt(struct Node** _head, char _lastName[256]){
- //used to hold the new list with only the elements with the given last name
- struct Node *newHead;
- initilizeList(&newHead);
- //newHead = NULL;
- //used to hold the current position
- struct Node *current = *_head;
- while(current!=NULL){
- if (strcmp(current->employee->lastName, _lastName)==0){
- addEmployeeEnd(&newHead, current->employee->firstName, current->employee->lastName, current->employee->age, current->employee->sinNumber, current->employee->status);
- }
- current = current ->next;
- }
- //deletes old list
- deleteList(_head);
- //changes teh old list to be the new list
- *_head = newHead;
- }
- //Splits a single linked list into two double linked lists with even elements with the extra going in the first section
- void divideList (struct Node **source, struct Node** first, struct Node** second){
- struct Node* current = *source;
- //length function?
- int length = 0;
- while(current!=NULL){
- length++;
- current=current->next;
- }
- //for (current = source;current!=NULL;current=current->next){
- // length++;
- //}
- //length now contains the amount of elements in the list
- //if all elements are added to the first list
- if(length<2){
- *first = *source;
- *second = NULL;
- }
- else{
- //changes length so the first list has the extra element if needed
- length = (length -1)/2;
- current = *source;
- int i;
- //navigates untill the lengthth element which is where the lists are to be split
- for(i = 0;i<length;i++){
- current = current->next;
- }
- //first gets the whole list
- *first = *source;
- //second gets the list after current
- *second = current->next;
- //this splits the two lists
- current->next = NULL;
- }
- }
- //Displays all of the employess in the list
- void displayList(struct Node *_head){
- struct Node* current;
- for(current = _head;current!=NULL;current=current->next)
- 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"));
- //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);
- printf("\n");
- }
- //Displays all of the employees in the list that are older than _age
- void displayList1(struct Node* _head, int _age){
- struct Node* current;
- for (current = _head;current !=NULL; current= current->next){
- if (current->employee->age > _age)
- 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"));
- }
- printf("\n");
- }
- //displays all of the employees in the list that have the same status as _status
- void displayList2(struct Node* _head, char _status){
- struct Node* current = _head;
- while(current !=NULL){
- if (current->employee->status == _status)
- //displayEmployee
- 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"));
- current = current->next;
- }
- printf("\n");
- }
- int main (){
- //head of the employee records
- struct Node *head;
- initilizeList(&head);
- //used for user input
- int age;
- int sinNum;
- char firstName [256];
- char lastName [256];
- char status;
- char temp;//used to deal with scanfs extra 'enter' character
- //testing only
- //addEmployeeFront(&head, "real", "person", 19, 9182, 'a');
- //addEmployeeFront(&head, "foo", "bar", 22, 5555, 'a');
- //addEmployeeFront(&head, "test", "erson", 55, 9911, 's');
- //addEmployeeFront(&head, "forth", "toStart", 10, 6543, 't');
- //end
- int i;
- //loop to get user input for each employee and add them to the list
- for (i =1;;i++){
- printf("Enter the first name of Employee #%d. Enter 'q' to stop entering employees.\n", i);
- scanf("%s%c", firstName, &temp);
- //strcpy(firstName, "q");
- if (strcmp(firstName,"q")==0)
- break;
- printf("Enter his/her last name.\n");
- scanf("%s%c", lastName, &temp);
- printf("Enter his/her age\n");
- scanf("%d%c", &age, &temp);
- printf("Enter his/her SIN number\n");
- scanf("%d%c", &sinNum, &temp);
- printf("Enter the status of the employee 'a' active, 's' suspended, 't' terminated.\n");
- scanf("%c%c", &status, &temp);
- addEmployeeFront(&head, firstName, lastName, age, sinNum, status);
- //displayList(head);
- }
- displayList(head);
- /*printf("\n test\n");
- struct Node* front = (struct Node*) malloc (sizeof(struct Node));
- struct Node* back = (struct Node*) malloc (sizeof(struct Node));
- divideList(&head, &front, &back);
- displayList(front);
- displayList(back);*/
- //displayList1(head, 50);
- //displayList2(head, 'a');
- return 0;
- }
Add Comment
Please, Sign In to add comment