fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct
  4. {
  5. int* data;
  6. int length;
  7. int size;
  8.  
  9. } arraylist;
  10. void initialize_arraylist(arraylist* list,int n)
  11. {
  12. list->data=(int*)malloc(n*sizeof(int));
  13. list->length=0;
  14. list->size=n;
  15. }
  16. void resize_arraylist(arraylist* list)
  17. {
  18. list->size*=2;
  19. list->data=(int*)realloc(list->data,list->size*sizeof(int));
  20. }
  21. void add_elements(arraylist* list,int value)
  22. {
  23. if(list->length>=list->size)
  24. {
  25. resize_arraylist(list);
  26. }
  27. list->data[list->length++]=value;
  28. }
  29. void print_arraylist(arraylist *list)
  30. {
  31. for(int i=0; i<list->length; i++)
  32. {
  33.  
  34. printf("%d ",list->data[i]);
  35. }
  36. }
  37. void insert_first(arraylist* list,int value)
  38. {
  39. if(list->length>=list->size)
  40. {
  41. resize_arraylist(list);
  42.  
  43. }
  44. list->length++;
  45. for(int i=list->length-1; i>=0; i--)
  46. {
  47. list->data[i+1]=list->data[i];
  48.  
  49. }
  50. list->data[0]=value;
  51. }
  52.  
  53.  
  54. void insert_at_last(arraylist* list,int value)
  55. {
  56. if(list->length>=list->size)
  57. {
  58. resize_arraylist(list);
  59.  
  60. }
  61. list->length++;
  62. list->data[list->length-1]=value;
  63. }
  64.  
  65. void delete_position(arraylist* list,int position )
  66. {
  67.  
  68. for(int j=position-1; j<list->length; j++)
  69. {
  70. list->data[j]=list->data[j+1];
  71.  
  72.  
  73. }
  74. list->length--;
  75. }
  76.  
  77.  
  78. void insert_position(arraylist *list,int position,int value)
  79. {
  80.  
  81. if(list->length>=list->size)
  82. {
  83. resize_arraylist(list);
  84.  
  85. }
  86. for(int j=list->length-1; j>=position-1; j--)
  87. {
  88.  
  89. list->data[j+1]=list->data[j];
  90.  
  91.  
  92. }
  93. list->data[position-1]=value;
  94. list->length++;
  95. }
  96. void delete_matching_values(arraylist* list,int value)
  97. {
  98. int k=0;
  99. for(int i=0; i<list->length; i++)
  100. {
  101.  
  102.  
  103. if(list->data[i]==value)
  104. {
  105.  
  106. for(int j=i; j<list->length; j++)
  107. {
  108. list->data[j]=list->data[j+1];
  109. }
  110. k++;
  111.  
  112.  
  113. }
  114.  
  115. }
  116. list->length =list->length-k;
  117. }
  118. void replace_matching_values(arraylist* list,int value,int k)
  119. {
  120.  
  121. for(int i=0; i<list->length; i++)
  122. {
  123.  
  124.  
  125. if(list->data[i]==value)
  126. {
  127. list->data[i]=k;
  128. }
  129.  
  130. }
  131.  
  132. }
  133.  
  134. int main()
  135. {
  136. arraylist list_main;
  137. initialize_arraylist(&list_main,3);
  138.  
  139. printf("\nEnter elements number: ");
  140. int x;
  141. scanf("%d",&x);
  142. for(int i=0; i<x; i++)
  143. {
  144. int y;
  145. scanf("%d",&y);
  146. add_elements(&list_main,y);
  147. }
  148. print_arraylist(&list_main);
  149. int choice,Value,position;
  150.  
  151. do
  152. {
  153. printf("\nEnter your choice :\n");
  154. printf("1.insert at first\n");
  155. printf("2.insert at last\n");
  156. printf("3.see current elements\n");
  157. printf("4.delete elements from position\n");
  158. printf("5.insert at a particular position\n");
  159. printf("6.delete matching values\n");
  160. printf("7.replace matching values\n");
  161. printf("8.exit\n");
  162. scanf("%d",&choice);
  163. if(choice>=9||choice<=0)
  164. {
  165. printf("\nis this a joke!!\nBye");
  166. break;
  167. }
  168. else{
  169. switch(choice)
  170. {
  171. case 1:
  172. printf("\nenter value :");
  173. scanf("%d",&Value);
  174. insert_first(&list_main,Value);
  175. print_arraylist(&list_main);
  176. break;
  177. case 2:
  178. printf("\nenter value :");
  179. scanf("%d",&Value);
  180. insert_at_last(&list_main,Value);
  181. print_arraylist(&list_main);
  182. break;
  183. case 3:
  184. print_arraylist(&list_main);
  185. break;
  186. case 4:
  187. printf("What position should be deleted?:");
  188. scanf("%d",&position);
  189. delete_position(&list_main,position);
  190. print_arraylist(&list_main);
  191. break;
  192.  
  193. case 5:
  194. printf("Enter the postion and value :");
  195. scanf("%d %d",&position,&Value);
  196. insert_position(&list_main,position,Value);
  197. print_arraylist(&list_main);
  198. break;
  199. case 6:
  200. printf("Enter the value you want to delete :");
  201. scanf("%d",&Value);
  202. delete_matching_values(&list_main,Value);
  203. print_arraylist(&list_main);
  204. break;
  205. case 7:
  206. printf("Enter the value you want to repalce :");
  207. scanf("%d",&Value);
  208. printf("Enter the value you want to repalce with:");
  209. scanf("%d",&position);
  210. replace_matching_values(&list_main,Value,position);
  211. print_arraylist(&list_main);
  212. break;
  213.  
  214. }}
  215. }
  216. while(choice!=8);
  217. }
  218.  
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
Enter elements number: 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 32765 
Enter your choice :
1.insert at first
2.insert at last
3.see current elements
4.delete elements from position
5.insert at a particular position
6.delete matching values
7.replace matching values
8.exit

is this a joke!!
Bye