Hey guys, I've been working on this program for a class and I'm getting a really strange error. Basically we had to create a dynamically linked list and then delete, modify, and insert elements. The list needs to stay sorted by part number. By that I mean if structure 1 has part number 1234, structure 2 has part number 2345, and structure 3 has part number 2000, structure 1 must point to structure 3, which must point to structure 2.
Anyway, the highest value for part number that I have "hard coded" into the program is 3456, and the lowest is 1234. It seems that when I try to insert a value greater than 3456 at runtime the program crashes and I get a segmentation fault.
Here's the program. I'm using Code::Blocks 8.02 w/ the default compiler, under Ubuntu 9.10.
It is attached, or the code is below...
On a sidenote, I am aware that using system() is poor practice, this is just the rough version as of now though...
Code:#include <stdio.h> #include <stdlib.h> struct PartsInfo{ int part_number; int quantity; float price; struct PartsInfo *next; }; void createData(struct PartsInfo *); void printData(struct PartsInfo *); void insertPart(struct PartsInfo *); void modifyPart(struct PartsInfo *); void deletePart(struct PartsInfo *); int main(){ int choice = 0; struct PartsInfo *first = (struct PartsInfo *) malloc(sizeof(struct PartsInfo)); createData(first); do { printf("\nWhat would you like to do?\n"); printf("1. Search for a part\n"); printf("2. Create a new part\n"); printf("3. Modify an existing part\n"); printf("4. Delete an existing part\n"); printf("0. Exit\n\n"); printf("->"); scanf("%d",&choice); //system("clear"); switch (choice){ case 1: printData(first); break; case 2: insertPart(first); break; case 3: modifyPart(first); break; case 4: deletePart(first); break; default: ; } } while (choice != 0); return 0; } void createData(struct PartsInfo *data_struct){ struct PartsInfo *temp_addr; int i; data_struct->part_number = 1234; data_struct->quantity = 5; data_struct->price = 5.25; for (i = 0 ; i <= 1; ++i){ if ((data_struct->next = (struct PartsInfo *) malloc(sizeof(struct PartsInfo))) != (struct PartsInfo *) NULL){ temp_addr = data_struct; data_struct = data_struct->next; data_struct->part_number = temp_addr->part_number + 1111; data_struct->quantity = temp_addr->quantity + 2; data_struct->price = temp_addr->price + 2.25; } else printf("Initial Memory Allocation Failed!"); } data_struct->next = (struct PartsInfo *) NULL; } void printData(struct PartsInfo *data_struct){ int part_number; printf("\n\nPlease enter the four digit part number(enter 0 for all parts): "); scanf("%d",&part_number); do { if (data_struct->part_number == part_number || part_number == 0){ printf("Part Data\n"); printf("Part Num : %d\n",data_struct->part_number); printf("Part Qty : %d\n",data_struct->quantity); printf("Part Cost : %.2f\n\n",data_struct->price); if (part_number != 0) break; } data_struct = data_struct->next; } while (data_struct != (struct PartsInfo *) NULL); if (data_struct == (struct PartsInfo *) NULL && part_number != 0) printf("Error! Record for part number %d not found!",part_number); } void insertPart(struct PartsInfo *data_struct){ struct PartsInfo *next_addr, *new_addr; int part_number; printf("\n\nCreate New Part\n"); printf("Please enter the part number: "); scanf("%d",&part_number); if ((new_addr = (struct PartsInfo *) malloc(sizeof(struct PartsInfo))) != (struct PartsInfo *) NULL){ do { if (data_struct->next != (struct PartsInfo *) NULL) next_addr = data_struct->next; else next_addr = (struct PartsInfo *) NULL; if (part_number > data_struct->part_number && ((next_addr->part_number > part_number) || next_addr == (struct PartsInfo *) NULL)){ data_struct->next = new_addr; data_struct = new_addr; data_struct->part_number = part_number; printf("Please enter the qty : "); scanf("%d",&data_struct->quantity); printf("Please enter the cost : "); scanf("%f",&data_struct->price); data_struct->next = next_addr; break; } data_struct = data_struct->next; } while (data_struct != (struct PartsInfo *) NULL); } else printf("Error! Allocation Failed!"); } void modifyPart(struct PartsInfo *data_struct){ int part_number; printf("\n\nModify an Existing Part\n\n"); printf("Please enter the four digit part number: "); scanf("%d",&part_number); do { if (data_struct->part_number == part_number){ printf("Please enter part qty : "); scanf("%d",&data_struct->quantity); printf("Please enter part cost : "); scanf("%f",&data_struct->price); break; } data_struct = data_struct->next; } while (data_struct != NULL); if (data_struct == NULL) printf("Error! Record for part number %d not found!",part_number); } void deletePart(struct PartsInfo *data_struct){ int part_number; struct PartsInfo *temp_addr = NULL; printf("\n\nDelete a Part\n\n"); printf("Please enter the four digit part number: "); scanf("%d",&part_number); do { temp_addr = data_struct; data_struct = data_struct->next; if (data_struct->part_number == part_number){ printf("Deleting structure from list..."); temp_addr->next = data_struct->next; free(data_struct); printf("Done!"); break; } } while (data_struct != NULL); if (data_struct == NULL) printf("Error! Record for part number %d not found!",part_number); }