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 = NULL;
- //_head->employee = initEmployee
- }
- //given a list and the data of the new employee it adds it to the end of the list
- int addEmployeeEnd(struct Node** _head, char _firstName[256], char _lastName[256], int _age, int _sinNum, char _status){
- struct Node *newNode;
- newNode = (struct Node*)malloc (sizeof(struct Node));
- //initialize employee
- struct Employee *employee = (struct Employee*)malloc(sizeof(struct Employee));
- strcpy(employee->firstName, _firstName);
- strcpy(employee->lastName, _lastName);
- employee->age = _age;
- employee->sinNumber = _sinNum;
- 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 1;
- }
- //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;
- newNode->next = NULL;
- return 1;
- }
- //given a list and the data of the new employee it adds it to the front of the list
- int addEmployeeFront(struct Node** _head, char _firstName[256], char _lastName[256], int _age, int _sinNum, char _status){
- struct Node *newNode;
- newNode = (struct Node*)malloc (sizeof(struct Node));
- //initialize employee
- struct Employee *employee = (struct Employee*)malloc(sizeof(struct Employee));
- strcpy(employee->firstName, _firstName);
- strcpy(employee->lastName, _lastName);
- employee->age = _age;
- employee->sinNumber = _sinNum;
- 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 1;
- }
- //add it to the front of the list
- newNode->next = *_head;
- *_head = newNode;
- return 1;
- }
- /*
- 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 (strcmp(current->next->employee->firstName, _firstName) == 0 || current->next->employee->sinNumber == _sinNum){
- temp = current->next;
- current->next = current->next->next;
- free(temp);
- }
- else{
- current = current ->next;
- }
- }
- }
- //duplicates each nodes so that there is _times amount of each node
- int Reapeated(struct Node* _head, int _times){
- struct Node* current = _head;
- struct Node* result = (struct Node*) malloc (sizeof(struct Node));
- result = NULL;
- //int count = 0;
- while(current!=NULL){
- int i;
- for (i = 1;i<_times;i++){
- //adds the current->employee to the end of the list _times-1 times
- //since there is already one already in the list
- addEmployeeEnd(&result, current->employee->firstName, current->employee->lastName, current->employee->age, current->employee->sinNumber, current->employee->status);
- }
- current=current->next;
- //count++;
- }
- _head = result;
- return 1;
- }
- //deletes every elements except the one(s) with the given last name
- void deleteExpt(struct Node** _head, char _lastName[256]){
- struct Node *newHead;
- newHead = (struct Node*) malloc (sizeof(struct Node));
- struct Node *current;
- current = (struct Node*) malloc (sizeof(struct Node));
- int count = 0;
- while((*_head)->next!=NULL){
- if (strcmp((*_head)->employee->lastName, _lastName)==0 && count ==0){
- newHead = *_head;
- newHead->next = current;
- count++;
- }
- else if (strcmp((*_head)->employee->lastName, _lastName)==0){
- //struct Node *temp;
- //temp = (struct Node*) malloc (sizeof(struct Node));
- current->next = *_head;
- current = current ->next;
- }
- }
- *_head = newHead;
- }
- //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;
- }
- //splits a single linked list into two 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;
- //length function?
- int length = 0;
- for (current = source;current!=NULL;current=current->next){
- length++;
- }
- 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;
- for(i = 0;i<length;i++){
- current = current->next;
- }
- *first = source;
- *second = current->next;
- 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("%s, %s\n%d\n%d\n%c\n", current->employee.lastName, current->employee.firstName, current->employee.age, current->employee.sinNumber, current->employee.status);
- 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)
- //displayEmployee
- 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);
- }
- 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", current->employee->lastName, current->employee->firstName, current->employee->age, current->employee->sinNumber, current->employee->status=='a'?"Active":(current->employee->status=='s'?"Suspended":"Retired"));
- current = current->next;
- }
- printf("\n");
- }
- int main (){
- struct Node *head;
- head = (struct Node*) malloc (sizeof(struct Node));
- head = NULL;
- //initilizeList(head);
- int age;
- int sinNum;
- char firstName [256];
- char lastName [256];
- char status;
- char temp;
- int i;
- 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);
- 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);
- //displayList1(head, 50);
- //displayList2(head, 'a');
- return 0;
- }
Add Comment
Please, Sign In to add comment