So i've slaved away some more at this and i don't understand where i'm going wrong.
My program allows the user to enter a name and an age, multiple times to create the list.
I then sort the list by name and by age with two different pointers.
i have littered my code with printf's to check where the adding of users function is stopping.
it makes sense where it stops after the first record is entered as it is the start of the list but when a second record is added the program stops at the exact same place in the function!
i cannot for the life of me figure out why it's stopping, and not creating the list. It obviously has something to do with the, head == NULL, and i'm guessing my nextName and nextAge pointers. but i just don't know how to fix it.
Here is my code, anyone got any ideas as to what might be going on?
Thanks, ALain
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LEN 250 typedef struct node { char *name; int age; struct node *nextName; struct node *nextAge; }NODE; int drain_stdin() { int c; while((c = getchar()) != '\n' && c != EOF); return c; } NODE * insertNode(NODE *head) { NODE *new; NODE *temp; NODE *prev; new = malloc(sizeof(NODE)); new->name = malloc(sizeof(char)*MAX_LEN + 1); if (drain_stdin() != EOF) { printf("Enter name: "); fgets(new->name, MAX_LEN + 1, stdin); printf("Enter age: "); scanf("%d", &new->age); printf("1\n"); new->nextName = NULL; new->nextAge = NULL; printf("2\n"); if (head == NULL) { printf("3\n"); return new; /* -------------------PROGRAM STOPS HERE ---------------------*/ printf("4\n"); } temp = head; prev = NULL; printf("5\n"); while(1) { printf("6\n"); if (strcmp(temp->name, new->name) < 0) { printf("7\n"); prev = temp; temp = temp->nextName; continue; } else if(strcmp(temp->name, new->name) >= 0) { printf("8\n"); new->nextName = temp; printf("9\n"); if (prev != NULL) { printf("10\n"); prev->nextName = new; } else { printf("11\n"); head = new; break; } } } } else { printf("12\n"); printf("An Error Has Occured\n"); } printf("13\n"); return head; } NODE * removeStruct(NODE *head) { NODE *prev; NODE *root; prev = NULL; root = head; char nameRemove[MAX_LEN + 1]; if (drain_stdin() != EOF ) { printf("Enter Name Of Desired Record To Be Removed: "); fgets(nameRemove, MAX_LEN + 1, stdin); while (head != NULL) { if(strcmp(nameRemove, root->name) == 0) { printf("Found The Record\n"); if (prev != NULL) { prev->nextName = head->nextName; prev->nextAge = head->nextAge; } else { root = head->nextName; root = head->nextAge; } free(head); break; } else { prev = head; head = head->nextName; head = head->nextAge; } } } else { printf("An Error Has Occured\n"); } return head; } void printName(NODE *head) { if (head == NULL) { printf("List is empty.\n") ; return ; } else { while (head != NULL) { printf("%s %d\n", head->name, head->age); head = head->nextName; } } } void printAge(NODE *head) { if (head == NULL) { printf("List is empty.\n") ; return ; } else { while (head != NULL) { printf("%s %d\n", head->name, head->age); head = head->nextAge; } } } void freeProg(NODE *head) { free(head->name); free(head); } int main(void) { int choice; NODE *head; head = NULL; do { printf("1. Add structure\n"); printf("2. Remove structure\n"); printf("3. Print names\n"); printf("4. Print ages\n"); printf("5. Exit\n"); scanf("%d", &choice); if(choice == 1) { insertNode(head); } else if(choice == 2) { removeStruct(head); } else if(choice == 3) { printName(head); } else if(choice == 4) { printAge(head); } else if(choice == 5) { freeProg(head); } else { return 0; } }while(choice != 5); return 0; }



LinkBack URL
About LinkBacks



