fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct {
  5. int *data;
  6. int length;
  7. int capacity;
  8. } array;
  9. void intialize_array(array* list,int initial_size){
  10.  
  11. list->data=(int*)malloc(initial_size*sizeof(int));
  12. list->length=0;
  13. list->capacity=initial_size;
  14.  
  15. }
  16. void resize(array* list){
  17. list->capacity*=2;
  18. list->data=(int*)realloc(list->data,list->capacity*sizeof(int));
  19. }
  20. void add_elements(array* list,int value){
  21. if(list->length==list->capacity){
  22. resize(list);
  23. }
  24. list->data[list->length++]=value;
  25.  
  26. }
  27. void bubbleshort(array* list){
  28. for(int i=0;i<list->length-1;i++){
  29. for(int j=0;j<list->length-i-1;j++){
  30. if(list->data[j]>list->data[j+1]){
  31. int temp=list->data[j];
  32. list->data[j]=list->data[j+1];
  33. list->data[j+1]=temp;
  34. }
  35. }
  36.  
  37. }
  38.  
  39. }
  40. void printArrayList(array* list) {
  41. printf("ArrayList contents: ");
  42. for (int i = 0; i < list->length; i++) {
  43. printf("%d ", list->data[i]);
  44. }
  45. printf("\n");
  46. }
  47.  
  48. int main (){
  49. array list;
  50. printf("capacity:");
  51. int size;
  52. scanf("%d",&size);
  53. int value;
  54. intialize_array(&list,size);
  55. printf("enter %d elements: ",size);
  56. for(int i=0;i<size;i++){
  57. scanf("%d",&value);
  58. add_elements(&list,value);
  59. }
  60. bubbleshort(&list);
  61. printArrayList(&list);
  62.  
  63. free(list.data);
  64.  
  65. }
  66.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
capacity:enter -784970923 elements: ArrayList contents: