Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.87 KB | None | 0 0
  1. header:
  2. #ifndef GADT_H
  3. #define GADT_H
  4. #include <stdio.h>
  5. //typedef declaration
  6.  
  7. typedef void* ELM;
  8. typedef void* SLN;
  9. typedef void* HEAD;
  10. typedef enum { success, outOfMem, badArgs, failure} RESULT;
  11.  
  12. HEAD SLCreate(ELM head_val, ELM(*create_elm)(), void(*cpy_elm)(ELM, ELM),
  13. int(*cmp_elm)( ELM, ELM), void(*free_elm)(ELM),
  14. void(*print_elm)( ELM), ELM(*add_elm_to_elm)(ELM, ELM));
  15. void SLDestroy(HEAD head);
  16. RESULT SLAddListElement(HEAD* head, ELM node);
  17. RESULT SLRemoveElement(HEAD* head, ELM node);
  18. SLN SLFindElement(HEAD head, ELM node);
  19. void SLAddToElement(HEAD* head, ELM toEl, ELM addEl);
  20. void SLPrintElements(HEAD head);
  21. void SLSize(HEAD head);
  22. int SLSizeReturn(HEAD head);
  23. #endif
  24.  
  25. c file:
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <malloc.h>
  29. #include "gadt.h"
  30. typedef struct Node{
  31. ELM data;
  32. struct Node* next;
  33. //Functions associated with the struct.
  34. ELM(*create_elm)();
  35. void(*cpy_elm)(ELM, ELM);
  36. int(*cmp_elm)( ELM, ELM);
  37. void(*free_elm)(ELM);
  38. void(*print_elm)( ELM);
  39. ELM(*add_elm_to_elm)(ELM, ELM);
  40. }Node;
  41. /************************************************************************
  42. * function name: SLCreate
  43. * The input: a pointer to a value(void*), pointers to functions
  44. that are associated with the struct.
  45. * The output: void*
  46. * the operation: creates the a pointer to a new linked list
  47. and initializes its functions.
  48. *************************************************************************/
  49. extern HEAD SLCreate(ELM head_val,
  50. ELM(*create_elm)(),
  51. void(*cpy_elm)(ELM, ELM),
  52. int(*cmp_elm)( ELM, ELM),
  53. void(*free_elm)(ELM),void(*print_elm)( ELM),
  54. ELM(*add_elm_to_elm)(ELM, ELM)){
  55. HEAD head;
  56. Node* pToFirstNode=(Node*)malloc(sizeof(Node));
  57. if (pToFirstNode!=NULL){ //if the allocation hasn't failed
  58. pToFirstNode->data=create_elm();
  59. cpy_elm(pToFirstNode->data, head_val);
  60. pToFirstNode->next=NULL;
  61. //initializing functions assosiated with Head.
  62. pToFirstNode->cpy_elm=cpy_elm;
  63. pToFirstNode->create_elm=create_elm;
  64. pToFirstNode->cmp_elm=cmp_elm;
  65. pToFirstNode->free_elm=free_elm;
  66. pToFirstNode->print_elm=print_elm;
  67. pToFirstNode->add_elm_to_elm=add_elm_to_elm;
  68. head=pToFirstNode; //so both of them point ot the same place
  69. return head;
  70. }
  71. return NULL;
  72. }
  73. /************************************************************************
  74. * function name: SLAddListElement
  75. * The input: the head of the list and the node to be created
  76. * The output: integer-RESULT (outOfMem, Succes)
  77. * the operation:goes to the end of the linked list, allocates memory
  78. at the end and copies node to it.
  79. *************************************************************************/
  80. extern RESULT SLAddListElement(HEAD* head, ELM node){
  81. Node* currNode;
  82. Node* iteratorNode=(Node*)(*head);
  83. Node* tempNode;
  84. currNode=(Node*)malloc(sizeof(Node));
  85. if(currNode==NULL){
  86. return outOfMem;
  87. }
  88. //inserting functions into currNode
  89. currNode->add_elm_to_elm=iteratorNode->add_elm_to_elm;
  90. currNode->cpy_elm=iteratorNode->cpy_elm;
  91. currNode->create_elm=iteratorNode->create_elm;
  92. currNode->free_elm=iteratorNode->free_elm;
  93. currNode->print_elm=iteratorNode->print_elm;
  94. currNode->cmp_elm=iteratorNode->cmp_elm;
  95. //inserting data into currNode
  96. currNode->data=currNode->create_elm();
  97. currNode->cpy_elm(currNode->data,node);//calls the function from head.
  98. currNode->next=NULL;
  99. //get the correct space to enter the node
  100. //exception if the list has only one item
  101. if(iteratorNode->next==NULL){
  102. if(iteratorNode->cmp_elm(iteratorNode->data,currNode->data)<0){
  103. currNode->next=iteratorNode;
  104. (*head)=currNode;
  105. return success;
  106. }
  107. else{
  108. iteratorNode->next=currNode;
  109. return success;
  110. }
  111. }
  112. //if something needs to be inserted in the first spot
  113. if(iteratorNode->cmp_elm(iteratorNode->data,currNode->data)<0){
  114. currNode->next=iteratorNode;
  115. (*head)=currNode;
  116. return success;
  117. }
  118. //else
  119. while(iteratorNode->next!=NULL &&
  120. iteratorNode->cmp_elm(iteratorNode->next->data,currNode->data)>0)
  121. {
  122. iteratorNode=iteratorNode->next;
  123. }
  124. tempNode=iteratorNode->next;
  125. iteratorNode->next=currNode;
  126. currNode->next=tempNode;
  127. return success;
  128. }
  129. /************************************************************************
  130. * function name: SLDestroy
  131. * The input: void* head
  132. * The output: none
  133. * the operation:removes every element from the list(destroys it)
  134. *************************************************************************/
  135. extern void SLDestroy(HEAD head){
  136. Node* currNode=(Node*)head;
  137. Node* subsequentNode=(Node*)head; //next node after previous
  138. //need two pointers because after i free the first one
  139. //i dont have access to the next node.
  140. while(currNode!=NULL){
  141. subsequentNode=subsequentNode->next;
  142. currNode->free_elm(currNode->data);
  143. free(currNode);
  144. currNode=subsequentNode;
  145. }
  146. }
  147. /************************************************************************
  148. * function name: SLPrintElements
  149. * The input: void*-the head of the list
  150. * The output: none
  151. * the operation:print the list's elements
  152. *************************************************************************/
  153. extern void SLPrintElements(HEAD head){
  154. Node* nodePointer=(Node*)head;
  155. int i=0, counter=1; //iterators
  156. //counter represents the amount of nodes printed allready.
  157. nodePointer->print_elm(nodePointer->data);
  158. while (nodePointer->next!=NULL){
  159. nodePointer=nodePointer->next;
  160. printf("\n");
  161. while(i<counter){
  162. printf("\t");
  163. i++;
  164. }
  165. counter++;
  166. i=0;
  167. nodePointer->print_elm(nodePointer->data);
  168. }
  169. printf("\n");
  170. }
  171. /************************************************************************
  172. * function name: SLSize
  173. * The input: void*-the head of the list
  174. * The output: integer-RESULT (outOfMem, Succes)
  175. * the operation:prints the amount of nodes in the list
  176. *************************************************************************/
  177. extern void SLSize(HEAD head){
  178. printf("%d\n",SLSizeReturn(head));
  179. }
  180. /************************************************************************
  181. * function name: SLSizeReturn
  182. * The input: the head of the list
  183. * The output: integer
  184. * the operation:returns the amount of nodes in the list
  185. *************************************************************************/
  186. extern int SLSizeReturn(HEAD head){
  187. int counter=0;
  188. Node* nodeCounter=(Node*)head;
  189. while(nodeCounter!=NULL){
  190. nodeCounter=nodeCounter->next;
  191. counter++;
  192. }
  193. return counter;
  194. }
  195. /************************************************************************
  196. * function name: SLFindElement
  197. * The input: two void* (head and element)
  198. * The output: void*
  199. * the operation:looks for the element in the list and returns its address.
  200. *************************************************************************/
  201. extern SLN SLFindElement(HEAD head, ELM node){
  202. Node* currNode=(Node*)head;
  203. void* result=NULL;
  204. while(currNode!=NULL){
  205. if(currNode->cmp_elm(currNode->data,node)==0){
  206. result=currNode;
  207. }
  208. currNode=currNode->next;
  209. }
  210. return result;
  211. }
  212. /************************************************************************
  213. * function name: SLRemoveElement
  214. * The input: a pointer to the head of the list and a node
  215. * The output: integer-resault.
  216. * the operation:removes the node from the list
  217. returns success.
  218. *************************************************************************/
  219. extern RESULT SLRemoveElement(HEAD* head, ELM node){
  220. Node* prevNode=(Node*)(*head);
  221. Node* toBeRemovedNode=(Node*)(SLFindElement((*head),node));
  222. Node* temp;
  223. temp=NULL;
  224. if(toBeRemovedNode==NULL){
  225. return failure;
  226. }
  227. while(prevNode->next!=toBeRemovedNode){
  228. prevNode=prevNode->next;
  229. }//prevNode is the node before toBeRemoved
  230. toBeRemovedNode->free_elm(toBeRemovedNode->data);
  231. temp=toBeRemovedNode->next;
  232. free(prevNode->next);
  233. prevNode->next=temp;
  234. return success;
  235. }
  236. /************************************************************************
  237. * function name: SLAddToElement
  238. * The input: pointer to the head of the list, two elements
  239. * The output: none
  240. * the operation:searches for the node with the first element
  241. removes it, adds the second element to it and returns it in the list
  242. *************************************************************************/
  243. extern void SLAddToElement(HEAD* head, ELM toEl, ELM addEl){
  244. Node* funcNode = (Node*)(*head);
  245. Node* tbRemovedNode = (Node*)SLFindElement(*head,toEl);
  246. //if we add stuff to the first element.
  247. if(funcNode==tbRemovedNode){
  248. (*head)=((Node*)(*head))->next;
  249. tbRemovedNode->data = tbRemovedNode->add_elm_to_elm
  250. (tbRemovedNode->data,addEl);
  251. SLAddListElement(head, tbRemovedNode->data);
  252. return;
  253. }
  254. while(funcNode->next != tbRemovedNode)
  255. funcNode=funcNode->next;
  256. funcNode->next = tbRemovedNode->next;
  257. tbRemovedNode->next = NULL;
  258. tbRemovedNode->data = tbRemovedNode->add_elm_to_elm
  259. (tbRemovedNode->data,addEl);
  260. SLAddListElement(head, tbRemovedNode->data);
  261. /*tbRemovedNode->free_elm(tbRemovedNode->data);
  262. free(tbRemovedNode);*/
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement