update
This commit is contained in:
parent
2992f4f408
commit
4f46de8d00
3330 changed files with 394553 additions and 76939 deletions
133
.config/Code/User/History/5797a381/c9bR.cpp
Normal file
133
.config/Code/User/History/5797a381/c9bR.cpp
Normal file
|
@ -0,0 +1,133 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
class ResizableArray{
|
||||
int* arr;
|
||||
int size;
|
||||
int i;
|
||||
|
||||
void increaseCapacity(){
|
||||
int* newArr = new int[size*2];
|
||||
for(int j=0;j<size;j++){
|
||||
newArr[j] = arr[j];
|
||||
}
|
||||
delete arr;
|
||||
arr = newArr;
|
||||
size *= 2;
|
||||
}
|
||||
|
||||
void decreaseCapacity(){
|
||||
int* newArr = new int[size/2];
|
||||
for(int j=0;j<size;j++){
|
||||
newArr[j] = arr[j];
|
||||
}
|
||||
delete arr;
|
||||
arr = newArr;
|
||||
size /= 2;
|
||||
}
|
||||
|
||||
public:
|
||||
ResizableArray() : size(10), i(0){
|
||||
arr = new int[size];
|
||||
}
|
||||
|
||||
void add(int value){
|
||||
if(i >= size) increaseCapacity();
|
||||
arr[i] = value;
|
||||
i++;
|
||||
}
|
||||
|
||||
bool insert(int index, int value){
|
||||
if(index < 0 || index > size) return false;
|
||||
else{
|
||||
if(i >= size) increaseCapacity();
|
||||
for(int j=i;j>=index;j--){
|
||||
arr[j+1] = arr[j];
|
||||
}
|
||||
arr[index] = value;
|
||||
i++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool remove(int index){
|
||||
if(index < 0 || index > size) return false;
|
||||
else{
|
||||
for(int j=index;j<i-1;j++){
|
||||
arr[j] = arr[j+1];
|
||||
}
|
||||
i--;
|
||||
if(i < size/4) decreaseCapacity();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool updateElement(int index, int value){
|
||||
if(index < 0 || index > size) return false;
|
||||
arr[index] = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
int get(int index) const{
|
||||
if(index < 0 || index > size) return -1;
|
||||
return arr[index];
|
||||
}
|
||||
|
||||
int getSize() const{
|
||||
return i;
|
||||
}
|
||||
|
||||
int capacity() const{
|
||||
return size;
|
||||
}
|
||||
|
||||
void clear(){
|
||||
delete arr;
|
||||
arr = new int[1];
|
||||
i = 0;
|
||||
}
|
||||
|
||||
void reverse(){
|
||||
for(int j=0;j<=i/2;j++){
|
||||
int temp = arr[j];
|
||||
arr[j] = arr[i-j];
|
||||
arr[i-j] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
int find(int value) const{
|
||||
for(int j=0;j<i;j++){
|
||||
if(arr[j] == value) return j;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
ResizableArray arr;
|
||||
|
||||
arr.add(4);
|
||||
arr.add(3);
|
||||
arr.add(2);
|
||||
arr.add(5);
|
||||
arr.add(6);
|
||||
|
||||
|
||||
|
||||
for(int i=0;i<arr.getSize();i++){
|
||||
cout<<arr.get(i)<<endl;
|
||||
}
|
||||
arr.reverse();
|
||||
for(int i=0;i<arr.getSize();i++){
|
||||
cout<<arr.get(i)<<endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue