arraylist_demo.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. #include "arraylist.h"
  2. void initializeDemo() {
  3. printf("\n\t\t =============== START DEMO - initialize() =============== \n\n\n");
  4. printf("Initialising List a with initialize() \n\n");
  5. List *a = initialize();
  6. printf("\t a = %s \n", toString(a));
  7. printf("\t Number of elements = %d \n", len(a));
  8. printf("\t Capacity of a = %d \n\n", cap(a));
  9. printf("Appending 5, 9 \n\n");
  10. append(a, 5);
  11. append(a, 9);
  12. printf("\t a = %s \n", toString(a));
  13. printf("\t Number of elements = %d \n", len(a));
  14. printf("\t Capacity of a = %d \n", cap(a));
  15. printf("\n\n\t\t =============== END DEMO - initialize() =============== \n\n\n");
  16. }
  17. void initializeWithCapacityDemo() {
  18. printf("\n\t\t =============== START DEMO - initializeWithCapacity() =============== \n\n\n");
  19. printf("Initialising List a with initializeWithCapacity(0) \n\n");
  20. List *a = initializeWithCapacity(0);
  21. printf("\t a = %s \n", toString(a));
  22. printf("\t Number of elements = %d \n", len(a));
  23. printf("\t Capacity of a = %d \n\n", cap(a));
  24. printf("Appending 5, 4, 15, 9, 19 \n\n");
  25. append(a, 5);
  26. append(a, 4);
  27. append(a, 15);
  28. append(a, 9);
  29. append(a, 19);
  30. printf("\t a = %s \n", toString(a));
  31. printf("\t Number of elements = %d \n", len(a));
  32. printf("\t Capacity of a = %d \n", cap(a));
  33. printf("\n\n\t\t =============== END DEMO - initializeWithCapacity() =============== \n\n\n");
  34. }
  35. void initializeWithArrayDemo() {
  36. printf("\n\t\t =============== START DEMO - initializeWithArray() =============== \n\n\n");
  37. printf("Initialising int arr[] with numbers from 1 to 18 \n\n");
  38. int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
  39. printf("Initialising List a with arr[] using initializeWithArray(arr, 18) \n\n");
  40. List *a = initializeWithArray(arr, 18);
  41. printf("\t a = %s \n", toString(a));
  42. printf("\t Number of elements = %d \n", len(a));
  43. printf("\t Capacity of a = %d \n", cap(a));
  44. printf("\n\n\t\t =============== END DEMO - initializeWithArray() =============== \n\n\n");
  45. }
  46. void valuesDemo() {
  47. printf("\n\t\t =============== START DEMO - values() =============== \n\n\n");
  48. printf("Initialising List a with values(5, 1, 2, 5, 9, 17) \n\n");
  49. List *a = values(5, 1, 2, 5, 9, 17);
  50. printf("\t a = %s \n", toString(a));
  51. printf("\t Number of elements = %d \n", len(a));
  52. printf("\t Capacity of a = %d \n", cap(a));
  53. printf("\n\n\t\t =============== END DEMO - values() =============== \n\n\n");
  54. }
  55. void rangeDemo() {
  56. printf("\n\t\t =============== START DEMO - range() =============== \n\n\n");
  57. printf("Initialising List a with range(0, 11, 2) \n\n");
  58. List *a = range(0, 11, 2);
  59. printf("\t a = %s \n", toString(a));
  60. printf("\t Number of elements = %d \n", len(a));
  61. printf("\t Capacity of a = %d \n\n", cap(a));
  62. printf("Initialising List b with range(10, -1, -2) \n\n");
  63. List *b = range(10, -1, -2);
  64. printf("\t b = %s \n", toString(b));
  65. printf("\t Number of elements = %d \n", len(b));
  66. printf("\t Capacity of a = %d \n\n", cap(b));
  67. printf("toString( range(5, 51, 5) ) = %s \n", toString( range(5, 51, 5) ));
  68. printf("\n\n\t\t =============== END DEMO - range() =============== \n\n\n");
  69. }
  70. void sliceDemo() {
  71. printf("\n\t\t =============== START DEMO - slice() =============== \n\n\n");
  72. printf("Initialising List a with range(10, 21, 1) \n\n");
  73. List *a = range(10, 21, 1);
  74. printf("\t a = %s \n", toString(a));
  75. printf("\t Number of elements = %d \n", len(a));
  76. printf("\t Capacity of a = %d \n\n", cap(a));
  77. printf("toString( slice(a, 2, 6, 1) ) = %s \n\n", toString( slice(a, 2, 6, 1) ));
  78. printf("toString( slice(a, 10, -1, -2) ) = %s \n\n", toString( slice(a, 10, -1, -2) ));
  79. printf("\n\n\t\t =============== END DEMO - slice() =============== \n\n\n");
  80. }
  81. void clearDemo() {
  82. printf("\n\t\t =============== START DEMO - clear() =============== \n\n\n");
  83. printf("Initialising List a with range(0, 11, 1) \n\n");
  84. List *a = range(0, 11, 1);
  85. printf("\t a = %s \n", toString(a));
  86. printf("\t Number of elements = %d \n", len(a));
  87. printf("\t Capacity of a = %d \n\n", cap(a));
  88. printf("Removing all elements from List a using clear(a) \n\n");
  89. clear(a);
  90. printf("\t a = %s \n", toString(a));
  91. printf("\t Number of elements = %d \n", len(a));
  92. printf("\t Capacity of a = %d \n\n", cap(a));
  93. printf("Appending 5 \n\n");
  94. append(a, 5);
  95. printf("\t a = %s \n", toString(a));
  96. printf("\t Number of elements = %d \n", len(a));
  97. printf("\t Capacity of a = %d \n", cap(a));
  98. printf("\n\n\t\t =============== END DEMO - clear() =============== \n\n\n");
  99. }
  100. void ensureCapacityDemo() {
  101. printf("\n\t\t =============== START DEMO - ensureCapacity() =============== \n\n\n");
  102. printf("Initialising List a with range(0, 11, 2) \n\n");
  103. List *a = range(0, 11, 2);
  104. printf("\t a = %s \n", toString(a));
  105. printf("\t Number of elements = %d \n", len(a));
  106. printf("\t Capacity of a = %d \n\n", cap(a));
  107. printf("Ensuring a minimum capacity of 2 using ensureCapacity(a, 2) \n\n");
  108. ensureCapacity(a, 2);
  109. printf("\t Capacity of a = %d \n\n", cap(a));
  110. printf("Ensuring a minimum capacity of 6 using ensureCapacity(a, 6) \n\n");
  111. ensureCapacity(a, 6);
  112. printf("\t Capacity of a = %d \n\n", cap(a));
  113. printf("Ensuring a minimum capacity of 7 using ensureCapacity(a, 7) \n\n");
  114. ensureCapacity(a, 7);
  115. printf("\t Capacity of a = %d \n\n", cap(a));
  116. printf("Ensuring a minimum capacity of 20 using ensureCapacity(a, 20) \n\n");
  117. ensureCapacity(a, 20);
  118. printf("\t Capacity of a = %d \n", cap(a));
  119. printf("\n\n\t\t =============== END DEMO - ensureCapacity() =============== \n\n\n");
  120. }
  121. void trimToSizeDemo() {
  122. printf("\n\t\t =============== START DEMO - trimToSize() =============== \n\n\n");
  123. printf("Initialising List a with initialize() \n\n");
  124. List *a = initialize();
  125. printf("\t a = %s \n", toString(a));
  126. printf("\t Number of elements = %d \n", len(a));
  127. printf("\t Capacity of a = %d \n\n", cap(a));
  128. printf("Initialising int arr[] with numbers from 1 to 6 \n\n");
  129. int arr[] = {1, 2, 3, 4, 5, 6};
  130. printf("Extending List a with arr[] using extendWithArray(a, arr, 6) \n\n");
  131. extendWithArray(a, arr, 6);
  132. printf("\t a = %s \n", toString(a));
  133. printf("\t Number of elements = %d \n", len(a));
  134. printf("\t Capacity of a = %d \n\n", cap(a));
  135. printf("Trimming Unused Space in List a using trimToSize(a) \n\n");
  136. trimToSize(a);
  137. printf("\t a = %s \n", toString(a));
  138. printf("\t Number of elements = %d \n", len(a));
  139. printf("\t Capacity of a = %d \n", cap(a));
  140. printf("\n\n\t\t =============== END DEMO - trimToSize() =============== \n\n\n");
  141. }
  142. void fillDemo() {
  143. printf("\n\t\t =============== START DEMO - fill() =============== \n\n\n");
  144. printf("Initialising List a with initialize() \n\n");
  145. List *a = initialize();
  146. printf("\t a = %s \n", toString(a));
  147. printf("\t Number of elements = %d \n", len(a));
  148. printf("\t Capacity of a = %d \n\n", cap(a));
  149. printf("Filling List a with 20 occurences of 5 using fill(a, 5, 20) \n\n");
  150. fill(a, 5, 20);
  151. printf("\t a = %s \n", toString(a));
  152. printf("\t Number of elements = %d \n", len(a));
  153. printf("\t Capacity of a = %d \n\n", cap(a));
  154. printf("Filling List a with 7 occurences of 19 using fill(a, 19, 7) \n\n");
  155. fill(a, 19, 7);
  156. printf("\t a = %s \n", toString(a));
  157. printf("\t Number of elements = %d \n", len(a));
  158. printf("\t Capacity of a = %d \n", cap(a));
  159. printf("\n\n\t\t =============== END DEMO - fill() =============== \n\n\n");
  160. }
  161. void appendDemo() {
  162. printf("\n\t\t =============== START DEMO - append() =============== \n\n\n");
  163. printf("Initialising List a with initializeWithCapacity(0) \n\n");
  164. List *a = initializeWithCapacity(0);
  165. printf("\t a = %s \n", toString(a));
  166. printf("\t Number of elements = %d \n", len(a));
  167. printf("\t Capacity of a = %d \n\n", cap(a));
  168. printf("Appending 5, 4, 15, 9, 19 using append(a, n) \n\n");
  169. append(a, 5);
  170. append(a, 4);
  171. append(a, 15);
  172. append(a, 9);
  173. append(a, 19);
  174. printf("\t a = %s \n", toString(a));
  175. printf("\t Number of elements = %d \n", len(a));
  176. printf("\t Capacity of a = %d \n", cap(a));
  177. printf("\n\n\t\t =============== END DEMO - append() =============== \n\n\n");
  178. }
  179. void extendWithArrayDemo() {
  180. printf("\n\t\t =============== START DEMO - extendWithArray() =============== \n\n\n");
  181. printf("Initialising List a with range(0, 11, 2) \n\n");
  182. List *a = range(0, 11, 2);
  183. printf("\t a = %s \n", toString(a));
  184. printf("\t Number of elements = %d \n", len(a));
  185. printf("\t Capacity of a = %d \n\n", cap(a));
  186. printf("Initialising int arr[] with numbers from 1 to 6 \n\n");
  187. int arr[] = {1, 2, 3, 4, 5, 6};
  188. printf("Extending List a with arr[] using extendWithArray(a, arr, 6) \n\n");
  189. extendWithArray(a, arr, 6);
  190. printf("\t a = %s \n", toString(a));
  191. printf("\t Number of elements = %d \n", len(a));
  192. printf("\t Capacity of a = %d \n", cap(a));
  193. printf("\n\n\t\t =============== END DEMO - extendWithArray() =============== \n\n\n");
  194. }
  195. void extendDemo() {
  196. printf("\n\t\t =============== START DEMO - extend() =============== \n\n\n");
  197. printf("Initialising List a with range(0, 11, 2) \n\n");
  198. List *a = range(0, 11, 2);
  199. printf("\t a = %s \n", toString(a));
  200. printf("\t Number of elements = %d \n", len(a));
  201. printf("\t Capacity of a = %d \n\n", cap(a));
  202. printf("Initialising List b with range(1, 11, 2) \n\n");
  203. List *b = range(1, 11, 2);
  204. printf("\t b = %s \n", toString(b));
  205. printf("\t Number of elements = %d \n", len(b));
  206. printf("\t Capacity of b = %d \n\n", cap(b));
  207. printf("Extending List a with List b using extend(a, b) \n\n");
  208. extend(a, b);
  209. printf("\t a = %s \n", toString(a));
  210. printf("\t Number of elements = %d \n", len(a));
  211. printf("\t Capacity of a = %d \n\n", cap(a));
  212. printf("Extending List a using extend(a, range(90, 96, 2)) \n\n");
  213. extend(a, range(90, 96, 2));
  214. printf("\t a = %s \n", toString(a));
  215. printf("\t Number of elements = %d \n", len(a));
  216. printf("\t Capacity of a = %d \n", cap(a));
  217. printf("\n\n\t\t =============== END DEMO - extend() =============== \n\n\n");
  218. }
  219. void insertDemo() {
  220. printf("\n\t\t =============== START DEMO - insert() =============== \n\n\n");
  221. printf("Initialising List a with range(0, 10, 1) \n\n");
  222. List *a = range(0, 10, 1);
  223. printf("\t a = %s \n", toString(a));
  224. printf("\t Number of elements = %d \n", len(a));
  225. printf("\t Capacity of a = %d \n\n", cap(a));
  226. printf("Inserting 99 at 0th index using insert(a, 0, 99) \n\n");
  227. insert(a, 0, 99);
  228. printf("\t a = %s \n", toString(a));
  229. printf("\t Number of elements = %d \n", len(a));
  230. printf("\t Capacity of a = %d \n\n", cap(a));
  231. printf("Inserting 88 at the end using insert(a, len(a), 88) \n\n");
  232. insert(a, len(a), 88);
  233. printf("\t a = %s \n", toString(a));
  234. printf("\t Number of elements = %d \n", len(a));
  235. printf("\t Capacity of a = %d \n\n", cap(a));
  236. printf("Inserting 55 at 5th index using insert(a, 5, 55) \n\n");
  237. insert(a, 5, 55);
  238. printf("\t a = %s \n", toString(a));
  239. printf("\t Number of elements = %d \n", len(a));
  240. printf("\t Capacity of a = %d \n\n", cap(a));
  241. printf("Inserting 22 at the last index using insert(a, -1, 22) \n\n");
  242. insert(a, -1, 22);
  243. printf("\t a = %s \n", toString(a));
  244. printf("\t Number of elements = %d \n", len(a));
  245. printf("\t Capacity of a = %d \n", cap(a));
  246. printf("\n\n\t\t =============== END DEMO - insert() =============== \n\n\n");
  247. }
  248. void copyDemo() {
  249. printf("\n\t\t =============== START DEMO - copy() =============== \n\n\n");
  250. printf("Initialising List a with range(0, 10, 2) \n\n");
  251. List *a = range(0, 10, 2);
  252. printf("\t a = %s \n", toString(a));
  253. printf("\t Number of elements = %d \n", len(a));
  254. printf("\t Capacity of a = %d \n\n", cap(a));
  255. printf("Initialising List b with copy(a) \n\n");
  256. List *b = copy(a);
  257. printf("\t b = %s \n", toString(b));
  258. printf("\t Number of elements = %d \n", len(b));
  259. printf("\t Capacity of b = %d \n", cap(b));
  260. printf("\n\n\t\t =============== END DEMO - copy() =============== \n\n\n");
  261. }
  262. void indexOfDemo() {
  263. printf("\n\t\t =============== START DEMO - indexOf() =============== \n\n\n");
  264. printf("Initialising List a with range(0, 11, 2) \n\n");
  265. List *a = range(0, 11, 2);
  266. printf("\t a = %s \n", toString(a));
  267. printf("\t Number of elements = %d \n", len(a));
  268. printf("\t Capacity of a = %d \n\n", cap(a));
  269. printf("Searching for the first occurrence of numbers from 0 to 10 using indexOf(a, i) \n\n");
  270. for(int i = 0; i < 11; i++) {
  271. int index = indexOf(a, i);
  272. if(index < 0)
  273. printf("\t %d not found in a \n", i);
  274. else
  275. printf("\t %d found at index %d \n", i, index);
  276. }
  277. printf("\n\n\t\t =============== END DEMO - indexOf() =============== \n\n\n");
  278. }
  279. void lastIndexOfDemo() {
  280. printf("\n\t\t =============== START DEMO - lastIndexOf() =============== \n\n\n");
  281. printf("Initialising int arr[] with {0, 1, 2, 2, 3, 4, 5, 6, 2, 7, 8, 9} \n\n");
  282. int arr[] = {0, 1, 2, 2, 3, 4, 5, 6, 2, 7, 8, 9};
  283. printf("Initialising List a with arr[] using initializeWithArray(arr, 12) \n\n");
  284. List *a = initializeWithArray(arr, 12);
  285. printf("\t a = %s \n", toString(a));
  286. printf("\t Number of elements = %d \n", len(a));
  287. printf("\t Capacity of a = %d \n\n", cap(a));
  288. printf("Searching for the last occurrence of 2, 10 using lastIndexOf(a, i) \n\n");
  289. printf("\t Last index of 2 = %d \n", lastIndexOf(a, 2));
  290. printf("\t Last index of 10 = %d \n", lastIndexOf(a, 10));
  291. printf("\n\n\t\t =============== END DEMO - lastIndexOf() =============== \n\n\n");
  292. }
  293. void binarySearchDemo() {
  294. printf("\n\t\t =============== START DEMO - binarySearch() =============== \n\n\n");
  295. printf("Initialising List a with range(0, 11, 2) \n\n");
  296. List *a = range(0, 11, 2);
  297. printf("\t a = %s \n", toString(a));
  298. printf("\t Number of elements = %d \n", len(a));
  299. printf("\t Capacity of a = %d \n\n", cap(a));
  300. printf("Searching for the index of numbers from 0 to 10 using binarySearch(a, i) \n\n");
  301. for(int i = 0; i < 11; i++) {
  302. int index = binarySearch(a, i);
  303. if(index < 0)
  304. printf("\t %d not found in a \n", i);
  305. else
  306. printf("\t %d found at index %d \n", i, index);
  307. }
  308. printf("\n\n\t\t =============== END DEMO - binarySearch() =============== \n\n\n");
  309. }
  310. void containsDemo() {
  311. printf("\n\t\t =============== START DEMO - contains() =============== \n\n\n");
  312. printf("Initialising List a with range(0, 11, 2) \n\n");
  313. List *a = range(0, 11, 2);
  314. printf("\t a = %s \n", toString(a));
  315. printf("\t Number of elements = %d \n", len(a));
  316. printf("\t Capacity of a = %d \n\n", cap(a));
  317. printf("Checking for the presence of numbers from 0 to 10 using contains(a, i) \n\n");
  318. for(int i = 0; i < 11; i++) {
  319. if(contains(a, i))
  320. printf("\t a contains %d \n", i);
  321. else
  322. printf("\t a doesn't contain %d \n", i);
  323. }
  324. printf("\n\n\t\t =============== END DEMO - contains() =============== \n\n\n");
  325. }
  326. void isEmptyDemo() {
  327. printf("\n\t\t =============== START DEMO - isEmpty() =============== \n\n\n");
  328. printf("Initialising List a with range(0, 11, 1) \n\n");
  329. List *a = range(0, 11, 1);
  330. printf("\t a = %s \n", toString(a));
  331. printf("\t Number of elements = %d \n", len(a));
  332. printf("\t Capacity of a = %d \n\n", cap(a));
  333. printf("Checking if a is empty using isEmpty(a) \n\n");
  334. if(isEmpty(a))
  335. printf("\t a is empty \n\n");
  336. else
  337. printf("\t a is not empty \n\n");
  338. printf("Removing all elements from List a using clear(a) \n\n");
  339. clear(a);
  340. printf("\t a = %s \n", toString(a));
  341. printf("\t Number of elements = %d \n", len(a));
  342. printf("\t Capacity of a = %d \n\n", cap(a));
  343. printf("Checking if a is empty using isEmpty(a) \n\n");
  344. if(isEmpty(a))
  345. printf("\t a is empty \n");
  346. else
  347. printf("\t a is not empty \n");
  348. printf("\n\n\t\t =============== END DEMO - isEmpty() =============== \n\n\n");
  349. }
  350. void isEqualDemo() {
  351. printf("\n\t\t =============== START DEMO - isEqual() =============== \n\n\n");
  352. printf("Initialising List a with range(0, 10, 2) \n\n");
  353. List *a = range(0, 10, 2);
  354. printf("\t a = %s \n", toString(a));
  355. printf("\t Number of elements = %d \n", len(a));
  356. printf("\t Capacity of a = %d \n\n", cap(a));
  357. printf("Initialising List b with copy(a) \n\n");
  358. List *b = copy(a);
  359. printf("\t b = %s \n", toString(b));
  360. printf("\t Number of elements = %d \n", len(b));
  361. printf("\t Capacity of b = %d \n\n", cap(b));
  362. printf("Checking if List a is equal to List b using isEqual(a, b) \n\n");
  363. if(isEqual(a, b))
  364. printf("\t a is equal to b \n\n");
  365. else
  366. printf("\t a is not equal to b \n\n");
  367. printf("Appending 0 to List b using append(b, 0) \n\n");
  368. append(b, 0);
  369. printf("\t b = %s \n", toString(b));
  370. printf("\t Number of elements = %d \n", len(b));
  371. printf("\t Capacity of b = %d \n\n", cap(b));
  372. printf("Checking if List a is equal to List b using isEqual(a, b) \n\n");
  373. if(isEqual(a, b))
  374. printf("\t a is equal to b \n\n");
  375. else
  376. printf("\t a is not equal to b \n\n");
  377. printf("Appending 0 to List a using append(a, 0) \n\n");
  378. append(a, 0);
  379. printf("\t a = %s \n", toString(a));
  380. printf("\t Number of elements = %d \n", len(a));
  381. printf("\t Capacity of a = %d \n\n", cap(a));
  382. printf("Checking if List a is equal to List b using isEqual(a, b) \n\n");
  383. if(isEqual(a, b))
  384. printf("\t a is equal to b \n");
  385. else
  386. printf("\t a is not equal to b \n");
  387. printf("\n\n\t\t =============== END DEMO - isEqual() =============== \n\n\n");
  388. }
  389. void popDemo() {
  390. printf("\n\t\t =============== START DEMO - pop() =============== \n\n\n");
  391. printf("Initialising List a with range(0, 11, 1) \n\n");
  392. List *a = range(0, 11, 1);
  393. printf("\t a = %s \n", toString(a));
  394. printf("\t Number of elements = %d \n", len(a));
  395. printf("\t Capacity of a = %d \n\n", cap(a));
  396. printf("Removing first element of a using pop(a, 0) \n\n");
  397. pop(a, 0);
  398. printf("\t a = %s \n", toString(a));
  399. printf("\t Number of elements = %d \n", len(a));
  400. printf("\t Capacity of a = %d \n\n", cap(a));
  401. printf("Removing last element of a using pop(a, -1) \n\n");
  402. pop(a, -1);
  403. printf("\t a = %s \n", toString(a));
  404. printf("\t Number of elements = %d \n", len(a));
  405. printf("\t Capacity of a = %d \n\n", cap(a));
  406. printf("Trying to remove value at index 10 from a using pop(a, 10) \n\n");
  407. if(pop(a, 10))
  408. printf("\t a = %s \n\n", toString(a));
  409. else
  410. printf("\t index %d not found in a \n\n", 10);
  411. printf("Trying to remove value at index 2 from a using pop(a, 2) \n\n");
  412. if(pop(a, 2))
  413. printf("\t a = %s \n\n", toString(a));
  414. else
  415. printf("\t index %d not found in a \n\n", 10);
  416. printf("Removing value at 2nd last index using pop(a, -2) \n\n");
  417. pop(a, -2);
  418. printf("\t a = %s \n", toString(a));
  419. printf("\t Number of elements = %d \n", len(a));
  420. printf("\t Capacity of a = %d \n", cap(a));
  421. printf("\n\n\t\t =============== END DEMO - pop() =============== \n\n\n");
  422. }
  423. void deleteDemo() {
  424. printf("\n\t\t =============== START DEMO - delete() =============== \n\n\n");
  425. printf("Initialising List a with range(0, 11, 2) \n\n");
  426. List *a = range(0, 11, 2);
  427. printf("\t a = %s \n", toString(a));
  428. printf("\t Number of elements = %d \n", len(a));
  429. printf("\t Capacity of a = %d \n\n", cap(a));
  430. printf("Trying to delete numbers from 0 to 10 using delete(a, i) \n\n");
  431. for(int i = 0; i < 11; i++) {
  432. if(delete(a, i))
  433. printf("\t %d deleted from a \n", i);
  434. else
  435. printf("\t a doesn't contain %d \n", i);
  436. }
  437. printf("\n");
  438. printf("\t a = %s \n", toString(a));
  439. printf("\t Number of elements = %d \n", len(a));
  440. printf("\t Capacity of a = %d \n", cap(a));
  441. printf("\n\n\t\t =============== END DEMO - delete() =============== \n\n\n");
  442. }
  443. void getDemo() {
  444. printf("\n\t\t =============== START DEMO - get() =============== \n\n\n");
  445. printf("Initialising List a with range(0, 11, 2) \n\n");
  446. List *a = range(0, 11, 2);
  447. printf("\t a = %s \n", toString(a));
  448. printf("\t Number of elements = %d \n", len(a));
  449. printf("\t Capacity of a = %d \n\n", cap(a));
  450. printf("Trying to print the values at indexes 0 to 10 using get(a, i) \n\n");
  451. for(int i = 0; i < 11; i++) {
  452. printf("\t Value at index %d = %d \n", i, get(a, i));
  453. }
  454. printf("\n\n\t\t =============== END DEMO - get() =============== \n\n\n");
  455. }
  456. void setDemo() {
  457. printf("\n\t\t =============== START DEMO - set() =============== \n\n\n");
  458. printf("Initialising List a with range(0, 11, 2) \n\n");
  459. List *a = range(0, 11, 2);
  460. printf("\t a = %s \n", toString(a));
  461. printf("\t Number of elements = %d \n", len(a));
  462. printf("\t Capacity of a = %d \n\n", cap(a));
  463. printf("Trying to double the value of all elements using set(a, i, get(a, i) * 2) \n\n");
  464. for(int i = 0; i < 11; i++) {
  465. if(set(a, i, get(a, i) * 2))
  466. printf("\t Doubling value at index %d successful \n", i);
  467. else
  468. printf("\t Index %d not found \n", i);
  469. }
  470. printf("\n");
  471. printf("a after doubling \n\n");
  472. printf("\t a = %s \n", toString(a));
  473. printf("\t Number of elements = %d \n", len(a));
  474. printf("\t Capacity of a = %d \n", cap(a));
  475. printf("\n\n\t\t =============== END DEMO - set() =============== \n\n\n");
  476. }
  477. void reverseDemo() {
  478. printf("\n\t\t =============== START DEMO - reverse() =============== \n\n\n");
  479. printf("Initialising List a with range(0, 11, 1) \n\n");
  480. List *a = range(0, 11, 1);
  481. printf("\t a = %s \n", toString(a));
  482. printf("\t Number of elements = %d \n", len(a));
  483. printf("\t Capacity of a = %d \n\n", cap(a));
  484. printf("Reversing the order of elements using reverse(a) \n\n");
  485. reverse(a);
  486. printf("\t a = %s \n", toString(a));
  487. printf("\t Number of elements = %d \n", len(a));
  488. printf("\t Capacity of a = %d \n", cap(a));
  489. printf("\n\n\t\t =============== END DEMO - reverse() =============== \n\n\n");
  490. }
  491. void replaceDemo() {
  492. printf("\n\t\t =============== START DEMO - replace() =============== \n\n\n");
  493. printf("Initialising int arr[] with {0, 1, 2, 3, 4, 5, 6, 1, 2, 2, 3, 4, 2, 6, 2} \n\n");
  494. int arr[] = {0, 1, 2, 3, 4, 5, 6, 1, 2, 2, 3, 4, 2, 6, 2};
  495. printf("Initialising List a with arr[] using initializeWithArray(arr, 15) \n\n");
  496. List *a = initializeWithArray(arr, 15);
  497. printf("\t a = %s \n", toString(a));
  498. printf("\t Number of elements = %d \n", len(a));
  499. printf("\t Capacity of a = %d \n\n", cap(a));
  500. printf("Replacing all occurrences of 2 with 99 using replace(a, 2, 99) \n\n");
  501. replace(a, 2, 99);
  502. printf("\t a = %s \n", toString(a));
  503. printf("\t Number of elements = %d \n", len(a));
  504. printf("\t Capacity of a = %d \n", cap(a));
  505. printf("\n\n\t\t =============== END DEMO - replace() =============== \n\n\n");
  506. }
  507. void sortDemo() {
  508. printf("\n\t\t =============== START DEMO - sort() =============== \n\n\n");
  509. printf("Initialising List a with range(0, 11, 2) \n\n");
  510. List *a = range(0, 11, 2);
  511. printf("\t a = %s \n", toString(a));
  512. printf("\t Number of elements = %d \n", len(a));
  513. printf("\t Capacity of a = %d \n\n", cap(a));
  514. printf("Extending List a using extend(a, range(1, 11, 2)) \n\n");
  515. extend(a, range(1, 11, 2));
  516. printf("\t a = %s \n", toString(a));
  517. printf("\t Number of elements = %d \n", len(a));
  518. printf("\t Capacity of a = %d \n\n", cap(a));
  519. printf("Sorting the elements of a in increasing order using sort(a) \n\n");
  520. sort(a);
  521. printf("\t a = %s \n", toString(a));
  522. printf("\t Number of elements = %d \n", len(a));
  523. printf("\t Capacity of a = %d \n", cap(a));
  524. printf("\n\n\t\t =============== END DEMO - sort() =============== \n\n\n");
  525. }
  526. void sortReverseDemo() {
  527. printf("\n\t\t =============== START DEMO - sortReverse() =============== \n\n\n");
  528. printf("Initialising List a with range(0, 11, 2) \n\n");
  529. List *a = range(0, 11, 2);
  530. printf("\t a = %s \n", toString(a));
  531. printf("\t Number of elements = %d \n", len(a));
  532. printf("\t Capacity of a = %d \n\n", cap(a));
  533. printf("Extending List a using extend(a, range(1, 11, 2)) \n\n");
  534. extend(a, range(1, 11, 2));
  535. printf("\t a = %s \n", toString(a));
  536. printf("\t Number of elements = %d \n", len(a));
  537. printf("\t Capacity of a = %d \n\n", cap(a));
  538. printf("Sorting the elements of a in increasing order using sortReverse(a) \n\n");
  539. sortReverse(a);
  540. printf("\t a = %s \n", toString(a));
  541. printf("\t Number of elements = %d \n", len(a));
  542. printf("\t Capacity of a = %d \n", cap(a));
  543. printf("\n\n\t\t =============== END DEMO - sortReverse() =============== \n\n\n");
  544. }
  545. void countDemo() {
  546. printf("\n\t\t =============== START DEMO - count() =============== \n\n\n");
  547. printf("Initialising int arr[] with {1, 2, 2, 3, 3, 3, 5, 5, 5, 5} \n\n");
  548. int arr[] = {1, 2, 2, 3, 3, 3, 5, 5, 5, 5};
  549. printf("Initialising List a with arr[] using initializeWithArray(arr, 10) \n\n");
  550. List *a = initializeWithArray(arr, 10);
  551. printf("\t a = %s \n", toString(a));
  552. printf("\t Number of elements = %d \n", len(a));
  553. printf("\t Capacity of a = %d \n\n", cap(a));
  554. printf("Counting the frequency of numbers from 0 to 5 using count(a, i) \n\n");
  555. for(int i = 0; i <= 5; i++)
  556. printf("\t %d is present %d times \n", i, count(a, i));
  557. printf("\n\n\t\t =============== END DEMO - count() =============== \n\n\n");
  558. }
  559. void sumDemo() {
  560. printf("\n\t\t =============== START DEMO - sum() =============== \n\n\n");
  561. printf("Initialising List a with range(0, 11, 2) \n\n");
  562. List *a = range(0, 11, 2);
  563. printf("\t a = %s \n", toString(a));
  564. printf("\t Number of elements = %d \n", len(a));
  565. printf("\t Capacity of a = %d \n\n", cap(a));
  566. printf("Calculating sum of all elements of a using sum(a) \n\n");
  567. printf("\t Sum(a) = %d \n", sum(a));
  568. printf("\n\n\t\t =============== END DEMO - sum() =============== \n\n\n");
  569. }
  570. void lenDemo() {
  571. printf("\n\t\t =============== START DEMO - len() =============== \n\n\n");
  572. printf("Initialising List a with range(0, 11, 2) \n\n");
  573. List *a = range(0, 11, 2);
  574. printf("\t a = %s \n", toString(a));
  575. printf("\t Number of elements = %d \n", len(a));
  576. printf("\t Capacity of a = %d \n\n", cap(a));
  577. printf("Appending 5, 9 \n\n");
  578. append(a, 5);
  579. append(a, 9);
  580. printf("\t a = %s \n", toString(a));
  581. printf("\t Capacity of a = %d \n\n", cap(a));
  582. printf("Finding new length of a using len(a) \n\n");
  583. printf("\t Len(a) = %d \n", len(a));
  584. printf("\n\n\t\t =============== END DEMO - len() =============== \n\n\n");
  585. }
  586. void capDemo() {
  587. printf("\n\t\t =============== START DEMO - cap() =============== \n\n\n");
  588. printf("Initialising List a with range(0, 11, 2) \n\n");
  589. List *a = range(0, 11, 2);
  590. printf("\t a = %s \n", toString(a));
  591. printf("\t Number of elements = %d \n", len(a));
  592. printf("\t Capacity of a = %d \n\n", cap(a));
  593. printf("Appending 5 \n\n");
  594. append(a, 5);
  595. printf("\t a = %s \n", toString(a));
  596. printf("\t Number of elements = %d \n\n", len(a));
  597. printf("Finding new capacity of a using cap(a) \n\n");
  598. printf("\t Cap(a) = %d \n", cap(a));
  599. printf("\n\n\t\t =============== END DEMO - cap() =============== \n\n\n");
  600. }
  601. void toArrayDemo() {
  602. printf("\n\t\t =============== START DEMO - toArray() =============== \n\n\n");
  603. printf("Initialising List a with range(0, 11, 2) \n\n");
  604. List *a = range(0, 11, 2);
  605. printf("\t a = %s \n", toString(a));
  606. printf("\t Number of elements = %d \n", len(a));
  607. printf("\t Capacity of a = %d \n\n", cap(a));
  608. printf("Initialising int arr[] with elements of List a using toArray(a) \n\n");
  609. int *arr = toArray(a);
  610. int arrLength = len(a);
  611. printf("\t arr[] = ");
  612. for(int i = 0; i < arrLength; i++)
  613. printf("%d ", arr[i]);
  614. printf("\n");
  615. printf("\n\n\t\t =============== END DEMO - toArray() =============== \n\n\n");
  616. }
  617. void toStringDemo() {
  618. printf("\n\t\t =============== START DEMO - toString() =============== \n\n\n");
  619. printf("Initialising List a with range(1000000000, 1000000009, 1) \n\n");
  620. List *a = range(1000000000, 1000000009, 1);
  621. printf("\t toString(a) = %s \n", toString(a));
  622. printf("\t Number of elements = %d \n", len(a));
  623. printf("\t Capacity of a = %d \n\n", cap(a));
  624. printf("Appending 1111111111 \n\n");
  625. append(a, 1111111111);
  626. printf("\t toString(a) = %s \n", toString(a));
  627. printf("\t Number of elements = %d \n", len(a));
  628. printf("\t Capacity of a = %d \n", cap(a));
  629. printf("\n\n\t\t =============== END DEMO - toString() =============== \n\n\n");
  630. }
  631. void displayDemo() {
  632. printf("\n\t\t =============== START DEMO - display() =============== \n\n\n");
  633. printf("Initialising List a with range(0, 11, 2) \n\n");
  634. List *a = range(0, 11, 2);
  635. printf("Displaying the elements of a using display(a) \n\n");
  636. printf("\t ");
  637. display(a);
  638. printf("\n");
  639. printf("Appending 5 \n\n");
  640. append(a, 5);
  641. printf("Displaying the elements of a using display(a) \n\n");
  642. printf("\t ");
  643. display(a);
  644. printf("\n\n\t\t =============== END DEMO - display() =============== \n\n\n");
  645. }
  646. int main(void) {
  647. initializeDemo();
  648. initializeWithCapacityDemo();
  649. initializeWithArrayDemo();
  650. valuesDemo();
  651. rangeDemo();
  652. sliceDemo();
  653. clearDemo();
  654. ensureCapacityDemo();
  655. trimToSizeDemo();
  656. fillDemo();
  657. appendDemo();
  658. extendWithArrayDemo();
  659. extendDemo();
  660. insertDemo();
  661. copyDemo();
  662. indexOfDemo();
  663. lastIndexOfDemo();
  664. binarySearchDemo();
  665. containsDemo();
  666. isEmptyDemo();
  667. isEqualDemo();
  668. popDemo();
  669. deleteDemo();
  670. getDemo();
  671. setDemo();
  672. reverseDemo();
  673. replaceDemo();
  674. sortDemo();
  675. sortReverseDemo();
  676. countDemo();
  677. sumDemo();
  678. lenDemo();
  679. capDemo();
  680. toArrayDemo();
  681. toStringDemo();
  682. displayDemo();
  683. return 0;
  684. }