Consider a dynamic table with the following properties:
- Elements are stored in a dynamic array
- Capacity is the size of the dynamic array
- Size is defined is the number of elements stored in the array
Insert elements into this dynamic table. Double capacity if size is equal to capacity before push_back()
Don't use malloc or calloc functions.
Input: (n, elements)
9
6 7 8 12 4 10 11 1 15
Output:
capacity = 1; size = 1; elements = 6
capacity = 2; size = 2; elements = 6 7
capacity = 4; size = 3; elements = 6 7 8
capacity = 4; size = 4; elements = 6 7 8 12
capacity = 8; size = 5; elements = 6 7 8 12 4
capacity = 8; size = 6; elements = 6 7 8 12 4 10
capacity = 8; size = 7; elements = 6 7 8 12 4 10 11
capacity = 8; size = 8; elements = 6 7 8 12 4 10 11 1
capacity = 16; size = 9; elements = 6 7 8 12 4 10 11 1 15
#include <stdio.h>
int size=0;
int capacity=0;
int * double_capacity(int *a) {
int l=0;
if(capacity==0) capacity++;
capacity=capacity*2;
int b[capacity];
for(l;l<size;l++){
//printf("%d : %d \n",l,a[l]);
b[l]=a[l];
}
return b;
}
int * push_back(int *a,int j) {
if(size==capacity)
a=double_capacity(a);
a[size]=j;
size++;
int k=0;
printf("capacity = %d; size = %d; elements = ",capacity,size);
for(k=0;k<size;k++) {
printf(" %d",*(a+k));
}
printf("\n");
return a;
}
main() {
int *a;
int n,i,j,k,l;
scanf("%d",&n);
int temp[n];
for(i=0; i<n; i++) {
scanf("%d",&temp[i]);
}
for(i=0; i<n; i++) {
a=push_back(a,temp[i]);
}
}
it is showing compile error like
\temp.c: In function ‘double_capacity’:
temp.c:16:2: warning: function returns address of local variable [-Wreturn-local-addr]
return b;
^
even though i run this
when i gave input
3 3 2 1
output
capacity = 1; size = 1; elements = 3
capacity = 2; size = 2; elements = 3 2
capacity = 4; size = 3; elements = 3 2 1
when i gave input
5 5 4 3 2 1
output
capacity = 1; size = 1; elements = 5
capacity = 2; size = 2; elements = 5 4
capacity = 4; size = 3; elements = 5 4 3
capacity = 4; size = 4; elements = 0 0 -2128976676 2
capacity = 8; size = 5; elements = 0 0 -2128976676 32524 1