Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "list.h"
- #include <stdio.h>
- #include <stdbool.h>
- List *List_create() {
- List *list = (List *)malloc(sizeof(List));
- list->counter = 0;
- list->head = NULL;
- return list;
- }
- // You still need to add the remaining function definitions, as defined in the
- // tasks
- void List_print(List *list) {
- int i = 0;
- Node *end = list->head;
- while (i < list->counter) {
- printf("-> id: %i\n", end->item->id);
- end = end->next;
- i++;
- }
- }
- void List_append(List *list) {
- Node *node = (Node *)malloc(sizeof(Node));
- Item *item = (Item *)malloc(sizeof(Item));
- node->item = item;
- list->counter++;
- item->id = list->counter;
- if (list->head != NULL) {
- Node *end = list->head->prev;
- node->prev = end;
- node->next = list->head;
- end->next = node;
- list->head->prev = node;
- } else {
- node->next = node;
- node->prev = node;
- list->head = node;
- }
- }
- void List_insert(List *list) {
- Node *node = (Node *)malloc(sizeof(Node));
- Item *item = (Item *)malloc(sizeof(Item));
- node->item = item;
- list->counter++;
- item->id = list->counter;
- if (list->head != NULL) {
- Node *second = list->head;
- Node *end = list->head->prev;
- node->next = second;
- node->prev = second->prev;
- end->next = node;
- second->prev = node;
- list->head = node;
- } else {
- node->next = node;
- node->prev = node;
- list->head = node;
- }
- }
- Node *List_find(List *list, int id) {
- Node *current = list->head;
- if (list->head == NULL)
- return NULL;
- int i = 0;
- while (i < list->counter) {
- if (current->item->id == id) {
- return current;
- }
- current = current->next;
- i++;
- }
- return NULL;
- }
- void List_remove(List *list, Node *node) {
- if (node != NULL && list->head != NULL) {
- Node *prevNode = node->prev;
- Node *nextNode = node->next;
- prevNode->next = nextNode;
- nextNode->prev = prevNode;
- if (list->head == node)
- list->head = nextNode;
- free(node->item);
- free(node);
- list->counter--;
- if (list->counter == 0)
- list->head = NULL;
- }
- }
- void List_destroy(List *list) {
- if (list->head != NULL) {
- while (list->counter != 0) {
- Node *end = list->head->prev;
- List_remove(list, end);
- }
- }
- }
- void List_putfirst(List *list, Node *node) {
- if (node != NULL && list->counter > 1) {
- Node *prevNode = node->prev;
- Node *nextNode = node->next;
- prevNode->next = nextNode;
- nextNode->prev = prevNode;
- Node *head = list->head;
- Node *end = head->prev;
- node->next = head;
- node->prev = end;
- head->prev = node;
- end->next = node;
- list->head = node;
- }
- }
- void List_sort(List *list) {
- // If the linked list has at least 2 items
- if (list->counter > 1) {
- bool still_sorting = true;
- while (still_sorting) {
- still_sorting = false;
- // The "Compare Node" is the first node in the set of the two compared nodes
- Node *cmpNode = list->head;
- // As long as we haven't hit the end of the list yet
- while (cmpNode->next != list->head) {
- // If the cmpNode has a higher ID than the next node
- if (cmpNode->item->id > cmpNode->next->item->id) {
- // Then swap these two nodes
- Node *swapNode = cmpNode->next;
- Node *nextNode = swapNode->next;
- Node *prevNode = cmpNode->prev;
- swapNode->next = cmpNode;
- cmpNode->prev = swapNode;
- cmpNode->next = nextNode;
- swapNode->prev = prevNode;
- prevNode->next = swapNode;
- nextNode->prev = cmpNode;
- // And update the list head pointer if necessary
- if (list->head == cmpNode) {
- list->head = swapNode;
- }
- still_sorting = true;
- }
- // Now move the cmpNode one space to the right
- cmpNode = cmpNode->next;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment