#include #include #include "list.h" List* create_list() //creates and allocates memory for an empty list. { List* L = (List)malloc(sizeof(List)); L->head = NULL; L->tail = NULL; return(L); } Node create_node(int key) //creates a node with a key and allocates memory for it. { Node* N = (Node)malloc(sizeof(Node)); N->key = key; N->next = NULL; N->prev = NULL; return(N); } int isEmpty(List L) { return L->head == NULL; //If there is no head, the list is empty and it will either return 1 or 0 } void insert(List* L, Node* N) //Inserts a node into a list. { if (L->head != NULL) //If there is no head set the node N to head { L->head->prev = N; } if (L->tail == NULL) //And if there is no tail set N to tail { L->tail = N; } N->next = L->head; N->prev = NULL; L->head = N; } Node* search(const List* L, int key) //Searching for a node using its key. { Node* temp_pointer = L->head; while (temp_pointer != NULL) { if (temp_pointer->key == key) //checking if the nodes key matches the searched key. { break; } temp_pointer = temp_pointer->next; } return(temp_pointer); } Node* node_delete(List* L, Node* N) //removes a node from the list and returns it { if(N->prev != NULL) { N->prev->next = N->next; //makes the previous node point to the next from where it's currently at. } else { L->head = N->next; //if the previous is NULL set the one it's currently at to head } if (N->next != NULL) { N->next->prev = N->prev; //the next node will now point to the previous one from where it's currently at. } if (L->tail == N) { L->tail = N->prev; //keeping the tail up to date } return(N); } Node* maximum(List* L) //Searching for the node with the highest "key" value by comparing every key to the previous one, saving the largest { Node* temp_pointer = L->head,max = L->head; while (temp_pointer!= NULL) { if (temp_pointer->key > max->key) //if the temp key is larger than the previous max set max to that temp node { max = temp_pointer; } temp_pointer = temp_pointer->next; } return(max); } Node minimum(List* L) //Same as maximum except searching for the minimum { Node* temp_pointer = L->head, *min = L->head; while (temp_pointer != NULL) { if (temp_pointer->key < min->key) { min = temp_pointer; } temp_pointer = temp_pointer->next; } return(min); }