/** * linked-queue.c * An implementation of stacks using linked lists. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include "collection.h" #include // For malloc #include // For strdup // +-------+---------------------------------------------------------- // | Types | // +-------+ struct node { char *data; struct node *next; }; struct collection { struct node *head; struct node *tail; int size; }; // +-----------+------------------------------------------------------ // | Functions | // +-----------+ struct collection * new_collection (int recommended_capacity) { struct collection *result; result = malloc (sizeof (struct collection)); if (result == NULL) return NULL; result->head = NULL; result->tail = NULL; result->size = 0; return result; } // new_collection struct collection * collection_copy (struct collection *stuff) { struct collection *result = malloc (sizeof (struct collection)); if (result == NULL) return NULL; struct node *temp = stuff->head; while (temp != NULL) { collection_put (result, temp->data); temp = temp->next; } return result; } // collection_copy void collection_free (struct collection *stuff) { } // collection_free int collection_put (struct collection *stuff, char *str) { // Add to the end of the list // Create a new node struct node *newbie = malloc (sizeof (struct node)); newbie->data = strdup (str); newbie->next = NULL; if (stuff->tail == NULL) { stuff->head = newbie; stuff->tail = newbie; } else { stuff->tail->next = newbie; stuff->tail = newbie; } ++(stuff->size); return 1; } // collection_put char * collection_get (struct collection *stuff) { if (stuff->head == NULL) return NULL; else { // Get the data from stuff->head char *str = stuff->head->data; // Save a pointer to the stuff->head struct node *dom = stuff->head; // Change stuff->head to stuff->head->next stuff->head = stuff->head->next; // Decrement Size --stuff->size; // Get rid of the node free (dom); // Special case if (stuff->head == NULL) stuff->tail = NULL; // And we're done return str; } // else } // collection_get char * collection_peek (struct collection *stuff) { if (stuff->head == NULL) return NULL; else return stuff->head->data; } // collection_peek int collection_size (struct collection *stuff) { return stuff->size; } // collection_size int collection_is_empty (struct collection *stuff) { return stuff->head == NULL; } // collection_is_empty int collection_is_full (struct collection *stuff) { return 0; } // collection_is_full