/* Listwrapper.c */ /* Chris Kern * * This is the "wrapper" class for the low-level linklist.c. The wrapper class deals * with lists as a whole rather than nodes. It uses the concept of cursored lists. * * You are supposed to use CreateList before you use any of these functions. * If any fields of the list are ever NULL, then the behavior of these * functions is undefined unless specified. These functions will never cause the fields of a * list to become NULL; that can only be done by direct user tampering. * * AddToFront provides behavior for null lists -- this is included *only* for quicksort's use. */ #include #include #include #include "linklist.h" /* Creates a new list with one node given a string. The string is used for the * contents field of the node, and a pointer to the list is returned, or NULL if * the memory was not allocated correctly. */ struct list *CreateList(char *contents) { /* Create a new list and node. */ struct list *newlist; struct node *newnode; /* Allocate memory. */ newlist = malloc(sizeof(struct list)); newnode = malloc(sizeof(struct node)); newnode->item = malloc(sizeof(1 + strlen(contents))); /* If the memory was not allocated correctly, return the NULL pointer. */ if (!newlist || !newnode || !newnode->item) return NULL; /* Build the node. */ strcpy(newnode->item, contents); newnode->next = NULL; newnode->previous = NULL; /* Build the list. */ newlist->first = newnode; newlist->last = newnode; newlist->cursor = newnode; /* Return the pointer to the list. */ return newlist; } /* CreateList */ /* Adds an item to the end of the list. Takes a pointer to the list and a string * representing the item to add. Returns 0 if the add failed, 1 if it was succesful. * The position of the cursor does not change. */ int AddToEnd(char *contents, struct list *list) { /* Attempt to at the item to the end of the list. */ struct node *temp = add_to_end(contents, list->first); /* If it fails, return 0. */ if (!temp) return 0; /* Since it did not fail, assign the "last" pointer to the node. */ list->last = temp; /* Success */ return 1; } /* AddToEnd */ /* Adds an item to the front of the list. Takes a pointer to the list and a string * representing the item to add. Returns 0 if the add failed, 1 if it was successful. * The position of the cursor does not change. */ int AddToFront(char *contents, struct list *list) { /* Attempt to create a node. */ struct node *temp; temp = malloc(sizeof(struct node)); temp->item = malloc(1 + strlen(contents)); /* If the allocation failed, return 0. */ if (!temp) return 0; /* Build the node. */ strcpy(temp->item, contents); temp->next = list->first; temp->previous = NULL; /* If the list was null, make this the first element added. */ if (!list->first) { list->first = temp; list->last = temp; list->cursor = temp; } /* Set it to the first element. */ else { list->first->previous = temp; list->first = temp; } /* Return 1 for success. */ return 1; } /* AddToFront */ /* Returns the value of the item pointed to by the cursor. Takes a pointer to the list * as the parameter. */ char *GetValue(struct list *list) { return list->cursor->item; } /* GetValue */ /* Sets the value of the current node to the string specified as the first parameter. * Returns 1 if the set was succesful and 0 if it was not. */ int SetValue(char *new_contents, struct list *list) { /* Attempt to assign memory for the new string. */ char *temp = malloc(1 + strlen(new_contents)); if (!temp) return 0; /* Free the memory from the string. */ free(list->cursor->item); /* Assign the new memory to the node and then copy the contents over. */ list->cursor->item = temp; strcpy(list->cursor->item, new_contents); /* Success. */ return 1; } /* SetValue */ /* Advance the list cursor to the next element. If the cursor is already at the end of * the list, it does not move, and the function returns 0. Otherwise it returns 1 for * success. */ int Advance(struct list *list) { /* If the cursor is on the last element of the list, return failure. */ if (!list->cursor->next) return 0; /* Otherwise, move the cursor and return 1. */ list->cursor = list->cursor->next; return 1; } /* Advance */ /* Move the list cursor to the previous element. If the cursor is at the beginning of * the list, then the cursor does not move and the function returns 0. Otherwise it returns 1 * for success. */ int GoBack(struct list *list) { /* If the cursor is on the first element of the list, return 0 for failure. */ if (!list->cursor->previous) return 0; /* Otherwise, move the cursor and return 1. */ list->cursor = list->cursor->previous; return 1; } /* GoBack */ /* Move the cursor to the first element of the list. */ void Reset(struct list *list) { list->cursor = list->first; } /* Reset */ /* Search the list for an element, and return 0 if the element does not exist * and 1 if it does. If the element does not exist, the cursor position does not * change. Otherwise the cursor moves to the element. */ int FindElement(char *toFind, struct list *list) { /* Attempt to find the string. */ struct node *temp = find_node(toFind, list->first); /* If the string does not exist, return 0 without changing cursor position. */ return 0; /* If the string exists, change the cursor position and return 1. */ list->cursor = temp; return 1; } /* FindElement */ /* This function would delete an arbitrary element of the list. */ int DeleteElement(char *toBeDeleted, struct list *list) { return 0; } /* DeleteElement */ int DeleteCurrentElement(struct list *list) { /* Run the low level function, including the somewhat redundant search. */ struct node *temp = list->cursor->previous; struct node *temp2 = list->cursor->next; delete_node(list->cursor->item, list->first); printf("Node deleted\n"); /* Move the cursor. */ /* Behavior if the item was the first item. */ if (!temp) { list->cursor = temp2; list->first = list->cursor; } /* Behavior if the item was the last item. */ else if (temp->next) { list->cursor = temp; list->last = list->cursor; } /* Behavior if the item was in the middle. */ else list->cursor = temp; return 0; } /* DeleteCurrentElement */ int WriteList(struct list *list) { /* Call the low level function. */ write_list(list->first); return 0; } /* WriteList */ int SaveList(char *filename, struct list *list) { /* Call the low level function. */ f_write_list(filename, list->first); return 0; } /* SaveList */ struct list *LoadList(char *filename) { /* Call the low level function. */ struct node *temp = f_read_list(filename); /* Set first, last, and cursor. */ struct list *list; list->first = temp; list->cursor = temp; if (!temp) printf("File loading failed.\n"); printf("Called the low level function.\n"); printf("%s\n", temp->item); /* Step through and assign last. */ while (list->cursor->next) { list->cursor = list->cursor->next; } list->last = list->cursor; return list; } /* LoadList */