Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "DLinkedList.h"
  5.  
  6.  
  7. void AddWord(struct List *l, char *w)
  8. {
  9.     if (l->head == NULL)
  10.     {
  11.         printf("No head node, creating one\n");
  12.         struct ListNode *tmp = (struct ListNode *)malloc(sizeof(struct ListNode));
  13.         l->head = tmp;
  14.         l->tail = tmp;
  15.         tmp->previous = l->head;
  16.         tmp->next = l->tail;
  17.         tmp->data = (char *)malloc(strlen(w) + 1);
  18.         strcpy(tmp->data, w);
  19.         tmp->data[strlen(w)] = '\0';
  20.         return;
  21.        
  22.     }
  23.  
  24.     printf("Creating Node\n");
  25.     struct ListNode *tmp = l->tail;
  26.     tmp->next = (struct ListNode *)malloc(sizeof(struct ListNode));
  27.     tmp->next->previous = tmp;
  28.     l->tail = tmp->next;
  29.     tmp->next->next = l->tail;
  30.     tmp->next->data = (char *)malloc(strlen(w) + 1);
  31.     strcpy(tmp->next->data, w);
  32.     tmp->next->data[strlen(w)] = '\0';
  33.     return;
  34.  
  35. }
  36.  
  37. void PrintList(struct List *l)
  38. {
  39.     struct ListNode *tmp = l->head;
  40.     for(;;)
  41.     {
  42.         if (tmp == l->tail)
  43.         {
  44.             printf("%s\n", tmp->data);
  45.             break;
  46.         }
  47.         printf("%s\n", tmp->data);
  48.         tmp = tmp->next;
  49.        
  50.     }
  51. }
  52.  
  53. void DeleteList(struct List *l)
  54. {
  55.     struct ListNode *tmp = l->head;
  56.     for(;;)
  57.     {
  58.         if (tmp == l->tail)
  59.         {
  60.             free(tmp->data);
  61.             free(tmp);
  62.             return;
  63.         }
  64.         free(tmp->data);
  65.         tmp = tmp->next;
  66.         free(tmp->previous);
  67.     }
  68.     l->head = NULL;
  69.     l->tail = NULL;
  70.    
  71. }