/* ================== buildHead ================= This function creates the header structure that contains pointers to the tree and the hash table. It also calls other functions to read in the data file. Pre pHeader - pointer to HEAD structure fileInput - name of the file Post both the tree and the hash table are created. Return pointer to create HEAD structure */ HEAD* buildHead (HEAD* pHeader, char* fileInput) { // Local Declarations DATA* newAirport; FILE* fpIn; // Statements fpIn = fopen(fileInput, "r"); if (!fpIn) { printf("Error opening input file\n"); exit(101); } if ((pHeader = (HEAD*) malloc(sizeof(HEAD)))) { pHeader->pHash = buildHash(2 * countLines(fileInput)); pHeader->pTree = BST_Create(compareCode); } else{ printf("Memory allocation error\n"); exit(100); } while (getData(pHeader->pTree, fpIn, &newAirport)) { BST_Insert(pHeader->pTree, newAirport); insertHash(pHeader->pHash, newAirport); } return pHeader; } // buildHead
/* ================== addAirport ================= This function adds another element of data into both the tree and hash table, through the user's input from keyboard. Pre pHeader - pointer to HEAD structure Post Return true if success false if fails */ bool addAirport (HEAD* pHeader) { // Local Declarations bool result = false; DATA tempAirport; DATA* newAirport = NULL; char tempCode [28]; char tempName [128]; int i; printf ("Enter airport code: "); scanf (" %s", tempCode); if (strlen(tempCode) != 3) { printf("Your airport code has to have 3 characters\n"); return result; } for (i = 0; i<strlen(tempCode); i++) { tempCode[i] = toupper(tempCode[i]); } strcpy(tempAirport.arpCode, tempCode); newAirport = findHash(pHeader->pHash, &tempAirport); if (newAirport == NULL) { if (!(newAirport = (DATA*) malloc(sizeof(DATA)))) { printf("Error allocating new airport\n"); exit(100); } strcpy(newAirport->arpCode, tempCode); printf("Enter airport city: "); scanf(" %[^\n]", tempName); newAirport->city = (char*) calloc(strlen(tempName) + 1, sizeof(char)); strcpy(newAirport->city, tempName); printf("Enter airport latitude: "); while(!(scanf("%f", &newAirport->latitude))) { printf("Invalid input, please try entering the latitude again: "); while(getchar() != '\n'); } printf("Enter airport longitude: "); while(!(scanf("%f", &newAirport->longitude))) { printf("Invalid input, please try entering the longitude again: "); while(getchar() != '\n'); } insertHash(pHeader->pHash, newAirport); BST_Insert(pHeader->pTree, newAirport); result = true; } else{ printf("This airport already exists\n"); processScreen(newAirport); } return result; } // addAirport
int main(void){ BST_TREE* BSTRoot; int* dataPtr; int dataIn = dataIn + 1; printf("Begin BST Demonstration\n"); BSTRoot = BST_Create(compareInt); printf("Enter a list of positive integers\n"); printf("Enter a negative number to stop.\n"); do{ printf("Enter a number:"); scanf("%d", &dataIn); if (dataIn > -1){ dataPtr = (int*) malloc (sizeof (int)); if (!dataPtr){ printf("Memory overflow"); exit (100); } *dataPtr = dataIn; BST_Insert (BSTRoot, dataPtr); } } while (dataIn > -1); printf("\nBST contains:\n"); BST_Traverse(BSTRoot, printBST); printf("\nEND BST Demonstration\n"); return 0; }
void addStu (BST_TREE* list, char name[], char phone[]){ // Local Definitions STUDENT* stuPtr; // Statements stuPtr = (STUDENT*)malloc (sizeof (STUDENT)); if (!stuPtr) printf("Memory Overflow in add\n"), exit(101); strcpy(stuPtr->phone, phone); strcpy(stuPtr->name, name); BST_Insert (list, stuPtr); } // addStu
/* ===== readFile ===== This function inputs the data from the file Pre hash, list, cars Post nothing */ void readFile(HASH *hash, D_LIST *list, BST_TREE *cars){ CAR car; FILE *fp; CAR *fill; D_NODE *pPre = list->tail; char input[250]; fp = fopen("Cars.txt","r"); if(!fp){ printf("Error. Cannot Read File"); exit(100); } while((fgets(input,sizeof(input), fp))){ car = stringToCar(input); insert(hash,car); insertDNode(list, car); pPre = pPre->back; if(!BST_Retrieve(cars, &car)) { fill = (CAR*)malloc(sizeof(CAR)); if(!fill) { printf("Memory FULL\n"), exit(101); } fill->name = allocateString(car.name); fill->brand = allocateString(car.brand); fill->cost = car.cost; strcpy(fill->plate, car.plate); BST_Insert(cars, fill); cars->count++; } } }// readFile