Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct DATA
- {
- char firstName[10];
- char lastName[20];
- int N; //number of phone numbers
- char update[1]; //checks to see if a person should be added/removed
- char number[50];
- struct DATA* next;
- struct DATA* prev;
- }DATA;
- struct DATA *head;
- struct DATA* GetNewNode(char first[10], char last[20], char update[1], int N, char number[50]) {
- struct DATA* newNode = (struct DATA*)malloc(sizeof(struct DATA));
- newNode->N = N;
- strcpy(newNode->firstName, first);
- strcpy(newNode->lastName, last);
- strcpy(newNode->update, update);
- newNode->number;
- newNode->prev = NULL;
- newNode->next = NULL;
- return newNode;
- }
- void InsertAtHead(char first[], char last[], char update[], int N, char number[]) {
- struct DATA* newNode = GetNewNode(first, last, update, N, number);
- if(head == NULL) {
- head = newNode;
- return;
- }
- head->prev = newNode;
- newNode->next = head;
- head = newNode;
- }
- //load the data from the hw3.data file
- DATA* loadData(FILE* fp, int *size)
- {
- int i = 0;
- DATA *record = NULL;
- //needed for string token
- char *token;
- const char s[2] = " "; //delimiter for string token
- //needed for finding the contents of each line
- char * line = NULL;
- size_t len = 0;
- ssize_t read;
- int tokCount = 0;
- char *tempN; //used as a temporary storage for N before converting it to an int
- char *checkEOF = "Q";
- int N, numberCount = 0;
- //used to copy the data
- char firstName[10];
- char lastName[20];
- int intN; //number of phone numbers
- char update[1]; //checks to see if a person should be added/removed
- char number[50] = "blarg";
- while ((read = getline(&line, &len, fp)) != -1)
- {
- token = strtok(line, s);
- while( token != NULL )
- {
- tokCount = 0;
- if (tokCount == 0)
- {
- strcpy(update, token);
- printf("%s\n\n", token);
- token = strtok(NULL, s);
- printf("%s\n\n", token);
- token = token + '\0';
- //strcpy(firstName, token);
- token = strtok(NULL, s);
- tokCount++;
- }
- /* else if (tokCount == 1)
- {
- strcpy(firstName, token);
- printf("%s\n\n\n\n\n\n", token);
- token = strtok(NULL, s);
- tokCount++;
- }
- else if (tokCount == 2)
- {
- strcpy(lastName,token);
- printf("%s\n\n\n\n\n\n", token);
- token = strtok(NULL, s);
- tokCount++;
- }
- else if (tokCount == 3)
- {
- tempN = token;
- intN = atoi(tempN);
- printf("%s\n\n\n\n\n\n", token);
- token = strtok(NULL, s);
- tokCount++;
- }*/
- /*
- else if (tokCount >= 4)
- {
- while( token != NULL )
- {
- strcpy(number, token);
- token = strtok(NULL, s);
- numberCount = numberCount+7;
- number[numberCount] = " "; //so we can delimit the numbers by spaces.
- numberCount++;
- }
- }*/
- else if (strcmp(update, "R") == 0)
- {
- }
- else if (strcmp(update, line) == 0)
- {
- exit(0);
- }
- InsertAtHead(firstName, lastName, update, intN, number);
- token = NULL;
- //tokCount++;
- }
- i++;
- }
- /* *size = i;
- return (record);
- if (line)
- {
- free(line);
- }*/
- return record;
- }
- void DISPLAY_INORDER() {
- struct DATA* temp = head;
- while(temp != NULL) {
- printf("lastName: \t%s\n",temp->lastName);
- printf("firstName: \t%s\n",temp->firstName);
- printf("update: \t%s\n", temp->update);
- printf("N: \t%d\n", temp->N);
- printf("number: \t%s\n", temp->number);
- temp = temp->next;
- }
- printf("\n");
- }
- int main(void)
- {
- FILE * fp;
- DATA *record = NULL;
- int size;
- fp = fopen("hw8data.txt", "r");
- if (fp == NULL)
- exit(EXIT_FAILURE);
- else
- record = loadData(fp, &size);
- DISPLAY_INORDER();
- fclose(fp);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement