diff --git a/ex2.cpp b/ex2.cpp index 4fcb033..9cc8068 100644 --- a/ex2.cpp +++ b/ex2.cpp @@ -28,112 +28,139 @@ class Vec swap(i,rand()%10); } - int get(int a) - { - return v[a]; - } + int get(int a); - void put(int a,int value) - { - v[a]=value; - } + void put(int a,int value); - void swap(int a, int b) - { - int temp = v[a]; - v[a] = v[b]; - v[b] = temp; - } + void swap(int a, int b); - void expand() - { - LEN=LEN*2; - int *p=new int[LEN]; + void expand(); - for(int i=0;i<=USED;i++) - p[i]=v[i]; - - delete[] v; - v=p; - } + void shrink(); - void shrink() - { - LEN=LEN>>1; - int *p=new int[LEN]; + int insert(int locate,int value); - for(int i=0;i<=USED;i++) - p[i]=v[i]; - - delete[] v; - v=p; - } + int del(int locate,int value); - int insert(int locate,int value) - { - if(locate<0||locate>USED) - return 1; + int find(int value); - USED++; - if(USED>=LEN) - expand(); + void printall(); - for(int i=USED;i>locate;i--) - v[i]=v[i-1]; - - v[locate]=value; + int getlen(); - return 0; - } - - int del(int locate,int value) - { - - if(locate<0||((USED-value)<0)||((locate+value-1)>USED)) - return 1; - - USED=USED-value; - - for(int i=locate;i<=USED;i++) - v[i]=v[i+value]; - - if( ( (double)USED/(double)LEN )<=0.3 ) - shrink(); - - return 0; - } - - int find(int value) - { - int i = 0; - for(i;i<=USED;i++) - { - if(v[i]==value) - { - return i; - } - } - - return -1; - } - - void printall() - { - for(int i=0;i>1; + int *p=new int[LEN]; + + for(int i=0;i<=USED;i++) + p[i]=v[i]; + + delete[] v; + v=p; +} + +int Vec::insert(int locate,int value) +{ + if(locate<0||locate>USED) + return 1; + + USED++; + + if(USED>=LEN) + expand(); + + for(int i=USED;i>locate;i--) + v[i]=v[i-1]; + + v[locate]=value; + + return 0; +} + +int Vec::del(int locate,int value) +{ + + if(locate<0||((USED-value)<0)||((locate+value-1)>USED)) + return 1; + + USED=USED-value; + + for(int i=locate;i<=USED;i++) + v[i]=v[i+value]; + + if( ( (double)USED/(double)LEN )<=0.3 ) + shrink(); + + return 0; +} + +int Vec::find(int value) +{ + int i = 0; + for(i;i<=USED;i++) + { + if(v[i]==value) + { + return i; + } + } + + return -1; +} + +int Vec::getlen() +{ + return LEN; +} + +int Vec::getused() +{ + return USED; +} + +void Vec::printall() +{ + for(int i=0;i