#include "dllnode.h" #include #define MAX_DATA_LENGTH 256 //+----------------------------------------------+ //| List input and output functions | //+----------------------------------------------+ /** * Read a list from a file written using the * write_list_to_file function. * @param fp The file pointer from which to read the list. * @return A pointer to the first dllnode in the list. */ struct dllnode *read_list_from_file(FILE *); /** * Write a list to a stream, so that it can be read later by * the read_list_from_file function. * @param stream The stream to which to write the list. * @param first_n The first dllnode in the list to write to the stream. * @return 0 if successful, 1 if not */ int write_list_to_file(FILE *, struct dllnode *); /** * Write the details of a list to a specified location. * @param stream The stream to which to write the list. * @param first_n The first node in the list to write to the stream. * @return 0 if successful, 1 if not */ int write_list_details(FILE *, struct dllnode *); //+----------------------------------------------+ //| List manipulation functions | //+----------------------------------------------+ /** * Add an element to the front of a list. * @param first_n The first dllnode of the list. * @param element The string to add to the list. * @return 0 if successful, 1 if not */ int add_element_to_list(struct dllnode *, char *); /** * Remove an element from a list. * @param first_n The list from which to remove node num. * @param num The node to remove from the list. * @return The first node in the new list which has n removed. */ struct dllnode *remove_element_from_list(struct dllnode *, int); /** * This variable arity function takes one or more lists as arguments * and concatenates the lists. It returns the new list (which is the same * as the first list passed to the function. * @param one The first list to cat. * @param ... Zero or more additional lists to cat. * @return The first list. */ struct dllnode *concatenate_lists(int, struct dllnode *, ...); /** * Delete a list and free all memory associated with that list. * @param first_n The first node in the list to be deleted. * @return 0 if successful, 1 if not */ int delete_list(struct dllnode *); /** * Get the last element in a doubly linked list. * @param first_n The first node in the list in question. * @return 0 if successful, 1 if not */ struct dllnode *get_last_node(struct dllnode *); /** * Get the next element in a doubly linked list. * @param n The element we are currently at. * @return The next element in the list. */ struct dllnode *get_next_element(struct dllnode *); /** * Get the previous element in a doubly linked list. * @param n The element we are currently at. * @return The previous element in the list. */ struct dllnode *get_prev_element(struct dllnode *); /** * Sort a list so that the data is arranged in ascending order. * (The first element is alphabetically before the second element, etc.) * @param first_n The first node in the list to be sorted. * @return 0 if successful, 1 if not */ struct dllnode *quicksort_list(struct dllnode *); /** * Create a new dllnode with the given string as data. * @param s The data for the new node. */ struct dllnode *make_new_node(); //+----------------------------------------------+ //| List information functions | //+----------------------------------------------+ /** * Get the list element at position. This method is O(n), so don't use it * if you can help it. * @param first_n A pointer to the first node in the list in question. * @param position The position of the node we wish to access. * @return A pointer to the node at position. */ struct dllnode *list_element_at(struct dllnode *, int); /** * Return an int which is the number of elements in a list. * @param first_n The first node in the list to be counted. * @return The number of elements in the list. */ int list_length(struct dllnode *); /** * Check to see if there is a next element in a doubly linked list. * @param n The element we are currently at. * @return 1 if there is a next element, 0 if not. */ int has_next_element(struct dllnode *); /** * Check to see if there is a previous element in a doubly linked list. * @param n The element we are currently at. * @return 1 if there is a previous element, 0 if not. */ int has_previous_element(struct dllnode *);