728x90
// 배열 인덱스에 값 추가 방법
void add(int idx, int values)
{
for (int i=arrSize-2; i>=idx; i--)
{
arr[i+1]=arr[i];
}
arr[idx]=values;
}
// 배열에 인덱스 값 삭제
void remove (int idx)
{
for (int i=idx+1; i<arrSize; i++)
{
arr[i-1]=a[i];
}
arr[arrSize-1]=0;
}
// reverse, shift
class Arrays
{
private:
int* arr;
int arrSize;
public:
Arrays(int _arrSize)
{
this->arrSize = _arrSize;
this->arr = new int[arrSize];
for (int i = 0; i < arrSize; i++)
{
arr[i] = 0;
}
}
void set(int idx, int values)
{
arr[idx] = values;
}
void shift(int d)
{
for (int i = 0; i < d; i++)
{
int last = arr[arrSize - 1];
for (int i = arrSize - 2; i >= 0; i--)
{
arr[i + 1] = arr[i];
}
arr[0] = last;
}
}
void reverse(int first, int last)
{
int num = 0;
int temp;
for (int i = 0; i < (last - first + 1) / 2; i++)
{
temp = arr[first + num];
arr[first + num] = arr[last - num];
arr[last - num] = temp;
num = num + 1;
}
}
void print()
{
for (int i = 0; i < arrSize; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
};
728x90