Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct node{ //Linked List Node
- char *firstname;
- char *lastname;
- struct node *next;
- }node;
- void add(node **head, char* fnme, char* lnme){ //Function for adding to
- node *new_node; //the linked list
- new_node = (node*)malloc(sizeof(node));
- if(new_node == NULL){
- printf("Error");
- return;
- }
- new_node->firstname = (char*)malloc(100*sizeof(char));
- if(new_node->firstname == NULL){
- printf("Error");
- return;
- }
- new_node->lastname = (char*)malloc(100*sizeof(char));
- if(new_node->lastname == NULL){
- printf("Error");
- return;
- }
- strcpy(new_node->firstname, fnme);
- strcpy(new_node->lastname, lnme);
- if (*head == NULL){
- *head = new_node;
- new_node->next = NULL;
- return;
- }
- node *current;
- current = *head;
- while(current->next != NULL){
- current = current->next;
- }
- current->next = new_node;
- new_node->next = NULL;
- }
- void print(node *head){ //Function for printing the list
- node *current;
- current = head;
- while(current != NULL){
- printf("%s %sn", current->firstname, current->lastname);
- current = current->next;
- }
- }
- int main() {
- node *head = NULL;
- char character;
- FILE *fp;
- fp = fopen("input.txt", "r");
- while ((character = fgetc(fp)) != EOF) {
- char *fnme, *lnme;
- fnme = (char*)malloc(100 * sizeof(char));
- if(fnme == NULL){
- printf("Error");
- return -1;
- }
- lnme = (char*)malloc(100 * sizeof(char));
- if(lnme == NULL){
- printf("Error");
- return -1;
- }
- int i = 0;
- while (character != ' ') { //Reading the letters until the
- fnme[i++] = character; //first space (first name)
- character = fgetc(fp);
- }
- fnme[++i] = '