Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. /* This file is to demonstrate the use of the functions
  2. * This has to be done N ascedning integers to the start and end of the list
  3. * The structure defined in the header file contains a void pointer rather than a pointer to specific data type
  4. * This means our implementation should allow you to add any data type with appropriate casting of the data types.
  5. * Then once added the program should remove the elements one by one from the beginning of the list
  6. * AND display the data value for each element on the screen whilst removing it (first for FIFO, then for LIFO)
  7. */
  8.  
  9. #include "linkedlist.c"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. int main (int argc, char **argv)
  14. {
  15.  
  16. void * oTemp;
  17. //With a double pointer the word itself (ie 'pHead') can be thought of as a pointer itself
  18. //to get the data you just need pHead
  19. struct element ** pHead = malloc(sizeof(struct element));
  20. // struct element * h;
  21.  
  22. struct element ** pTail = malloc(sizeof(struct element));
  23. //struct element * t;
  24.  
  25. //pHead = &h;
  26. //pTail = &t;
  27.  
  28.  
  29. int i= 2;
  30.  
  31. oTemp = &i;
  32.  
  33.  
  34. addFirst(oTemp,pHead,pTail);
  35. addFirst(oTemp,pHead,pTail);
  36. addLast(oTemp,pHead,pTail);
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. }
  46.  
  47.  
  48. /*Operating Systems and Concurrency Lab 1*/
  49.  
  50. /*If on school PC's go on PUTTY and connect to bam.cs.nott.ac.uk *
  51. *This file is for the implementation of the function prototypes from linkedlist.h
  52. *The code needs to implement functions to:
  53. * - Add elements at the beginning of the list (addFirst)
  54. * - Add elements at the end of the list (addLast)
  55. * - Remove the first element from the list (removeFirst)
  56. *
  57. *
  58. *In order to use the structure the header file needs to be included*/
  59. #include "linkedlist.h"
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62.  
  63. /*Functions from LinkedList.h*/
  64.  
  65. void addLast(void * oTemp, struct element ** pHead, struct element ** pTail){
  66.  
  67. /*struct element * new1 = malloc(sizeof(struct element));
  68.  
  69. if(pTail)
  70. {
  71. new1 = oTemp;
  72. pTail-> = *new1;
  73.  
  74.  
  75.  
  76. printf("LL.C val 2\n");
  77. printf("Value in new1: %d\n",*(int *) new1);
  78. //pHead->pNext = NULL;
  79. }*/
  80. }
  81.  
  82.  
  83. void addFirst(void * oTemp, struct element ** pHead, struct element ** pTail){
  84.  
  85. /*
  86. *New element is created and data is allocated. Then set as head.
  87. */
  88. printf("Seg Fault test 1\n");
  89. struct element * new2 = malloc(sizeof(struct element));
  90. struct element *head;
  91.  
  92. //By passing in **pHead we are passing in a POINTER to a POINTER
  93. //So now to manipulate this pointer we need to have access to it
  94. printf("Seg Fault test 2\n");
  95.  
  96. //need to test if there is data in the head
  97. //valgrind for testing memory leaks
  98.  
  99. if(*pHead == NULL){
  100. printf("Seg Fault test 3\n");
  101. new2->pData = oTemp;
  102. printf("Seg Fault test 3.1\n");
  103. new2->pNext = NULL;
  104. printf("Seg Fault test 3.2\n");
  105. *pHead = new2;
  106. printf("Seg Fault test 3.3\n");
  107. }
  108. else{
  109. printf("Seg Fault test 4\n");
  110.  
  111. head = *pHead;
  112. new2->pData = oTemp;
  113. new2->pNext = head;
  114. *pHead = new2;
  115. printf("Seg Fault test 4.1\n");
  116. }
  117.  
  118.  
  119. }
  120.  
  121. void * removeFirst(struct element ** pHead, struct element ** pTail){
  122.  
  123. }
  124.  
  125. /*Summarising notes for when we come back
  126. *Finish modifying first year append_linked_list code
  127. *Fully find out what double pointer (** pHead and ** pTail) exactly means
  128. *Figuring out HOW data is assigned
  129. *Work on the task1.c file after Linkedlist.c is fully implemented
  130. *So when we come back we should fully implement addFirst, addLast, * removeFirst
  131. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement