fork download
  1. Make whatever changes are necessary so that the running time of every operation except icopy and idelete is O(1).
  2. You may assume that the following restriction is added to the interface for
  3.  
  4. iappend_destroy(il1,il2):
  5.  
  6. il1 and il2 must not be the same ilist.
  7.  
  8. You may not use recursion or arrays.
  9.  
  10.  
  11.  
  12.  
  13.  
  14. #include "ilist_destructive.h"
  15. #include <stdlib.h>
  16.  
  17.  
  18.  
  19. // The ilist ADT is a pointer to this secret struct
  20. struct ilist_ADT{
  21. struct ilist_ADT *rest;
  22. struct ilist_ADT *append;
  23. int first;
  24. int length;
  25. }
  26. ;
  27.  
  28.  
  29.  
  30. // observation: the names are not the best, i would call it destructiveICons
  31. // (and ->first should be called ->value)
  32. ilist icons_destroy(int in, ilist il) {
  33. if(iempty_huh(il)) {
  34. ilist temp = malloc(sizeof(struct ilist_ADT));
  35. temp->rest = iempty();
  36. temp->length = 1;
  37. temp->first = in;
  38. temp->append = temp;
  39. il = temp;
  40. return il;
  41. }
  42. else{
  43. ilist temp2 = malloc(sizeof(struct ilist_ADT));
  44. temp2->length = il->length;
  45. temp2->first = il->first;
  46. temp2->rest = il->rest;
  47. temp2->append = il->append;
  48. il->length = ilength(il) + 1;
  49. il->rest = temp2;
  50. il->first = in;
  51. il->append = temp2->append;
  52. return il;
  53. }
  54. }
  55.  
  56.  
  57. // ifirst returns the first element of il
  58. int ifirst(ilist il){
  59. return il->first;
  60. }
  61.  
  62.  
  63. ilist irest(ilist il){
  64. return il->rest;
  65. }
  66.  
  67. // irest returns il with first element removed
  68. ilist irest_destroy(ilist il){
  69. ilist temp = il->rest;
  70. if(iempty_huh(temp)) {
  71. free(il); return temp;
  72. }
  73.  
  74. il->first = temp->first;
  75. il->length = temp->length;
  76. il->rest = temp->rest;
  77. free(temp);
  78. return il;
  79. }
  80.  
  81.  
  82.  
  83.  
  84.  
  85. // ilist returns an empty ilist
  86. // we have to use a function because struct ilist_node is secret
  87. ilist iempty(){
  88. return NULL;
  89. }
  90.  
  91. // test for empty ilist
  92. int iempty_huh(ilist il){
  93. return il == NULL;
  94. }
  95.  
  96.  
  97. ilist icopy(ilist il){
  98. ilist temp = iempty();
  99. ilist temp2 = iempty();
  100. ilist a;
  101. while (!iempty_huh(il)){
  102. temp = icons_destroy(ifirst(il), temp);
  103. il = il->rest;
  104. }
  105. a = temp;
  106. while (!iempty_huh(temp)){
  107. temp2 = icons_destroy(ifirst(temp), temp2);
  108. temp = temp->rest;
  109. }
  110. idelete(a);
  111. return temp2;
  112. }
  113.  
  114.  
  115.  
  116. // computes the number of elements in il
  117. int ilength(ilist il){
  118. if(iempty_huh(il)){
  119. return 0;
  120. }
  121. else{
  122. return il->length;
  123. }
  124. }
  125.  
  126. ilist iappend_destroy(ilist il1, ilist il2){
  127. if (iempty_huh(il1)&&iempty_huh(il2))
  128. return il1;
  129.  
  130. if (il1 == il2)
  131. il2 = icopy(il2);
  132.  
  133. if(iempty_huh(il1))
  134. return il2;
  135.  
  136. ilist temp = il1;
  137. temp = temp->append;
  138. temp->length = temp->length + ilength(il2);
  139. temp->rest = il2;
  140. temp = temp->rest;
  141. return il1;
  142. }
  143.  
  144.  
  145. // free memory for entire ilist
  146. void idelete(ilist il){
  147. while (il != NULL) {
  148. ilist next = il->rest;
  149. free(il);
  150. il = next;
  151. }
  152. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:14:31: error: ilist_destructive.h: No such file or directory
prog.cpp:1: error: ‘Make’ does not name a type
In file included from prog.cpp:15:
/usr/include/stdlib.h:140: error: ‘size_t’ does not name a type
In file included from prog.cpp:15:
/usr/include/stdlib.h:337: error: ‘size_t’ has not been declared
/usr/include/stdlib.h:367: error: ‘size_t’ has not been declared
/usr/include/stdlib.h:471: error: ‘size_t’ was not declared in this scope
/usr/include/stdlib.h:471: error: expected ‘,’ or ‘;’ before ‘throw’
/usr/include/stdlib.h:473: error: ‘size_t’ was not declared in this scope
/usr/include/stdlib.h:473: error: ‘size_t’ was not declared in this scope
/usr/include/stdlib.h:473: error: initializer expression list treated as compound expression
/usr/include/stdlib.h:474: error: expected ‘,’ or ‘;’ before ‘throw’
/usr/include/stdlib.h:485: error: ‘size_t’ has not been declared
In file included from /usr/include/stdlib.h:497,
                 from prog.cpp:15:
/usr/include/alloca.h:33: error: ‘size_t’ was not declared in this scope
/usr/include/alloca.h:33: error: expected ‘,’ or ‘;’ before ‘throw’
In file included from prog.cpp:15:
/usr/include/stdlib.h:502: error: ‘size_t’ was not declared in this scope
/usr/include/stdlib.h:502: error: expected ‘,’ or ‘;’ before ‘throw’
/usr/include/stdlib.h:507: error: ‘size_t’ has not been declared
/usr/include/stdlib.h:507: error: ‘size_t’ has not been declared
/usr/include/stdlib.h:684: error: ‘size_t’ has not been declared
/usr/include/stdlib.h:684: error: ‘size_t’ has not been declared
/usr/include/stdlib.h:689: error: ‘size_t’ has not been declared
/usr/include/stdlib.h:689: error: ‘size_t’ has not been declared
/usr/include/stdlib.h:692: error: ‘size_t’ has not been declared
/usr/include/stdlib.h:692: error: ‘size_t’ has not been declared
/usr/include/stdlib.h:767: error: ‘size_t’ has not been declared
/usr/include/stdlib.h:770: error: ‘size_t’ has not been declared
/usr/include/stdlib.h:774: error: ‘size_t’ has not been declared
/usr/include/stdlib.h:778: error: ‘size_t’ has not been declared
/usr/include/stdlib.h:787: error: ‘size_t’ has not been declared
/usr/include/stdlib.h:791: error: ‘size_t’ has not been declared
/usr/include/stdlib.h:798: error: ‘size_t’ does not name a type
/usr/include/stdlib.h:801: error: ‘size_t’ does not name a type
/usr/include/stdlib.h:864: error: ‘size_t’ has not been declared
In file included from /usr/include/stdlib.h:882,
                 from prog.cpp:15:
/usr/include/bits/stdlib.h:26: error: ‘size_t’ has not been declared
/usr/include/bits/stdlib.h:30: error: ‘size_t’ has not been declared
/usr/include/bits/stdlib.h: In function ‘char* realpath(const char*, char*)’:
/usr/include/bits/stdlib.h:40: error: ‘size_t’ was not declared in this scope
/usr/include/bits/stdlib.h: At global scope:
/usr/include/bits/stdlib.h:53: error: ‘size_t’ has not been declared
/usr/include/bits/stdlib.h:54: error: ‘size_t’ has not been declared
/usr/include/bits/stdlib.h:55: error: ‘size_t’ has not been declared
/usr/include/bits/stdlib.h:58: error: ‘size_t’ has not been declared
/usr/include/bits/stdlib.h:58: error: ‘size_t’ has not been declared
/usr/include/bits/stdlib.h:65: error: ‘size_t’ has not been declared
/usr/include/bits/stdlib.h: In function ‘int ptsname_r(int, char*, int)’:
/usr/include/bits/stdlib.h:67: error: ‘size_t’ was not declared in this scope
/usr/include/bits/stdlib.h: At global scope:
/usr/include/bits/stdlib.h:78: error: ‘size_t’ has not been declared
/usr/include/bits/stdlib.h: In function ‘int wctomb(char*, wchar_t)’:
/usr/include/bits/stdlib.h:93: error: ‘size_t’ was not declared in this scope
/usr/include/bits/stdlib.h: At global scope:
/usr/include/bits/stdlib.h:99: error: ‘size_t’ does not name a type
/usr/include/bits/stdlib.h:102: error: ‘size_t’ does not name a type
/usr/include/bits/stdlib.h:106: error: ‘size_t’ does not name a type
/usr/include/bits/stdlib.h:113: error: ‘size_t’ does not name a type
/usr/include/bits/stdlib.h:131: error: ‘size_t’ does not name a type
/usr/include/bits/stdlib.h:134: error: ‘size_t’ does not name a type
/usr/include/bits/stdlib.h:138: error: ‘size_t’ does not name a type
/usr/include/bits/stdlib.h:144: error: ‘size_t’ does not name a type
prog.cpp:32: error: ‘ilist’ does not name a type
prog.cpp:58: error: ‘ilist’ was not declared in this scope
prog.cpp:58: error: expected ‘,’ or ‘;’ before ‘{’ token
prog.cpp:63: error: ‘ilist’ does not name a type
prog.cpp:68: error: ‘ilist’ does not name a type
prog.cpp:87: error: ‘ilist’ does not name a type
prog.cpp:92: error: ‘ilist’ was not declared in this scope
prog.cpp:92: error: expected ‘,’ or ‘;’ before ‘{’ token
prog.cpp:97: error: ‘ilist’ does not name a type
prog.cpp:117: error: ‘ilist’ was not declared in this scope
prog.cpp:117: error: expected ‘,’ or ‘;’ before ‘{’ token
stdout
Standard output is empty