Advertisement
Guest User

DLL

a guest
Nov 20th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. typedef struct dl_list_t dl_list_t;
  2. struct dl_list_t
  3. {
  4. char *str; //yes, this is just a string since you're porting to java
  5.  
  6. dl_list_t *next, *prev;
  7. };
  8.  
  9. dl_list_t *dl_list_create()
  10. {
  11. dl_list_t *tmp = malloc(sizeof(dl_list_t));
  12. if(tmp == NULL)
  13. return NULL;
  14.  
  15. memset(tmp, 0, sizeof(dl_list_t));
  16. return tmp;
  17. }
  18.  
  19. dl_list_t *dl_list_free(dl_list_t *head)
  20. {
  21. while(head->prev != NULL)
  22. head=head->prev;
  23. while(head != NULL)
  24. {
  25. dl_list_t *tmp = head;
  26. head=head->next;
  27. free(tmp->str);
  28. free(tmp);
  29.  
  30. }
  31. return head;
  32. }
  33.  
  34. dl_list_t *dl_list_insert(dl_list_t *dl_list, char *stri)
  35. {
  36. if(dl_list == NULL)
  37. return NULL;
  38. dl_list_t *tmp = dl_list;
  39.  
  40. if(tmp->next != NULL)
  41. tmp=tmp->next;
  42.  
  43. if(tmp->str == NULL)
  44. {
  45. tmp->str = malloc(strlen(cats)+1);
  46. strcpy(tmp->str, cats);
  47. }
  48. else if(strcmp(tmp->str, cats) != 0)
  49. {
  50. tmp->next = dl_list_create();
  51. if(tmp->next == NULL)
  52. return NULL;
  53. tmp->next->prev = tmp;
  54. tmp = tmp->next;
  55. tmp->str = malloc(strlen(cats)+1);
  56. strcpy(tmp->str, cats);
  57. }
  58.  
  59. return tmp;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement