Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "linkedlist.h"
  4.  
  5. int main(int argc, char **argv)
  6. {
  7.  
  8. void * oTemp;
  9. //With a double pointer the word itself (ie 'pHead') can be thought of as a pointer itself
  10. //to get the data you just need pHead
  11. struct element ** pHead;
  12. struct element ** pTail;
  13. struct element * h;
  14. struct element * t;
  15.  
  16. int cycle;
  17. int removeData;
  18. int loadData [20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
  19. int loadData2 [20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
  20.  
  21. t = NULL;
  22. h = NULL;
  23. pHead = &h;
  24. pTail = &t;
  25.  
  26. for( cycle = 0; cycle<20; cycle++){
  27.  
  28. oTemp = &loadData[cycle];
  29. addFirst(oTemp, pHead, pTail);
  30.  
  31. h = *pHead;
  32. }
  33.  
  34.  
  35. for( removeData = 0; removeData <20; removeData++){
  36. removeFirst(pHead, pTail);
  37. }
  38.  
  39. t = NULL;
  40. h = NULL;
  41. pHead = &h;
  42. pTail = &t;
  43.  
  44.  
  45. for( cycle = 0; cycle<20; cycle++){
  46. oTemp = &loadData2[cycle];
  47. addLast(oTemp, pHead, pTail);
  48. t = *pTail;
  49. printf("Value in pTail: %d\n", *(int *)t->pData);
  50. }
  51.  
  52.  
  53. for( removeData = 0; removeData <20; removeData++){
  54. removeFirst(pHead, pTail);
  55. }
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. #include "linkedlist.h"
  67. #include <stdio.h>
  68. #include <stdlib.h>
  69.  
  70. /*Functions from LinkedList.h*/
  71.  
  72. void addLast(void * oTemp, struct element ** pHead, struct element ** pTail){
  73. /*
  74. *New element is created and data is allocated. Then set as head.
  75. */
  76. struct element * new2Last= (struct element*)malloc(sizeof(struct element));
  77. struct element *headLast;
  78.  
  79. //By passing in **pHead we are passing in a POINTER to a POINTER
  80. //So now to manipulate this pointer we need to have access to it
  81. //need to test if there is data in the head
  82. //valgrind for testing memory leaks
  83.  
  84. if(*pTail == NULL){
  85. new2Last->pData = oTemp;
  86. new2Last->pNext = NULL;
  87. *pTail = new2Last;
  88. *pHead = new2Last;
  89. }
  90. else{
  91. headLast = *pTail;
  92. new2Last->pData = oTemp;
  93. headLast->pNext = new2Last;
  94. new2Last->pNext = NULL;
  95.  
  96. *pTail = new2Last;
  97. }
  98.  
  99.  
  100. }
  101.  
  102.  
  103. void addFirst(void * oTemp, struct element ** pHead, struct element ** pTail){
  104.  
  105. /*
  106. *New element is created and data is allocated. Then set as head.
  107. */
  108. struct element * new2 = (struct element*)malloc(sizeof(struct element));
  109. struct element *head;
  110.  
  111. //By passing in **pHead we are passing in a POINTER to a POINTER
  112. //So now to manipulate this pointer we need to have access to it
  113.  
  114. //need to test if there is data in the head
  115. //valgrind for testing memory leaks
  116.  
  117. if(*pHead == NULL){
  118. new2->pData = oTemp;
  119. new2->pNext = NULL;
  120. *pHead = new2;
  121. *pTail = new2;
  122. }
  123. else{
  124. head = *pHead;
  125. new2->pData = oTemp;
  126. new2->pNext = head;
  127. *pHead = new2;
  128. }
  129.  
  130.  
  131. }
  132.  
  133. void * removeFirst(struct element ** pHead, struct element ** pTail){
  134.  
  135. /*We need to return a void pointer that has the data of the element we are removing
  136. *To do this we need to store this data in a void pointer, so we can return it
  137. *For correct memeory allocation we also need to free the entire element
  138. *So first we must save the data in the element the head is pointing to and then free the element
  139. */
  140. void *remove;
  141. struct element *head;
  142. head = *pHead;
  143. remove = head->pData;
  144. *pHead = head -> pNext;
  145. free(head);
  146. return remove;
  147.  
  148.  
  149. //Save the element after the head
  150. //free the head
  151. //make the pointer to the head eqaul to the saved node
  152. }
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169. /**************************************************************************************************************************
  170. DO NOT CHANGE THE CONTENTS OF THIS FILE FOR YOUR COURSEWORK. ONLY WORK WITH THE OFFICIAL VERSION
  171. **************************************************************************************************************************/
  172.  
  173. #include <stdio.h>
  174.  
  175. struct element
  176. {
  177. void * pData;
  178. struct element * pNext;
  179. };
  180.  
  181. void addLast(void * oTemp, struct element ** pHead, struct element ** pTail);
  182. void addFirst(void * oTemp, struct element ** pHead, struct element ** pTail);
  183. void * removeFirst(struct element ** pHead, struct element ** pTail);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement