Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include "list.h"
  2.  
  3. void CreateList(list* list)
  4. {
  5. list->head=NULL;
  6. }
  7. list_element* CreateElement(char* solution)
  8. {
  9. list_element* element=(list_element*)malloc(sizeof(list_element));
  10. strcpy(element->solution, solution);
  11. element->next=NULL;
  12. return element;
  13. }
  14. void AddElement(list* list, char* solution)
  15. {
  16. list_element* element=CreateElement(solution);
  17. list_element* current;
  18. if(list->head==NULL)
  19. {
  20. list->head=element;
  21. }
  22. else
  23. {
  24. current=list->head;
  25. while(current->next!=NULL)
  26. current=current->next;
  27. current->next=element;
  28. }
  29. }
  30. void RemoveElement(list* list, list_element* element)
  31. {
  32. list_element* current;
  33.  
  34. if(list->head==NULL)
  35. {
  36. printf("List is empty\n");
  37. exit(1);
  38. }
  39. if(strcmp(list->head->solution, element->solution)==0)
  40. {
  41. list->head=list->head->next;
  42. free(element);
  43. }
  44. else{
  45. current=list->head;
  46. while(strcmp(current->solution, element->solution)!=0)
  47. current=current->next;
  48. current->next=element->next;
  49. free(element);
  50. }
  51.  
  52. }
  53. void DestroyList(list* list)
  54. {
  55. list_element* current=list->head;
  56. list_element* next;
  57. while(current!=NULL)
  58. {
  59. next=current->next;
  60. free(current);
  61. current=next;
  62. }
  63. list->head=NULL;
  64. }
  65.  
  66. void PrintList(list* list)
  67. {
  68. if(list->head==NULL)
  69. {
  70. printf("Lista je prazna");
  71. exit(1);
  72. }
  73. list_element* current=list->head;
  74. printf("Stampanje resenja:\n");
  75. while(current!=NULL)
  76. {
  77. printf("%s\t", current->solution);
  78. current=current->next;
  79. }
  80. printf("\n");
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement