LAB1
LAB1
1. print 0,1,2,.. to n
void printArray(int n)
{
if(n > 0)
{printArray(n-1);
cout << ',' << ' ' << n;}
else if ( n == 0)
cout << n;
}
return 0;
5.findGCD
int findGCD(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if(a == b)
return a;
if(a > b)
return findGCD(a-b, b);
else return findGCD(a, b-a);
}
7.
void printHailstone(int number)
{
if(number == 1)
cout << 1;
else if(number%2 == 0)
{ cout << number << ' ';
printHailstone(number/2);}
else
{
cout << number << ' ';
printHailstone(number*3+1);
}
}
8.
int myArrayToInt(char *str, int n)
{
int num = 0;
if(n == 1)
{
num += (str[0]-48);
return num;
}
else
{
num += (str[0]-48)*pow(10,n-1);
return num + myArrayToInt(++str,n-1);
}
}
9. find LCM (have to find GCD then find LCM by multiply A*B/GCD)
int findGCD(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if(a == b)
return a;
if(a > b)
return findGCD(a-b, b);
else return findGCD(a, b-a);
}
int LCM(int a, int b)
{
int GCD = findGCD(a,b);
return a*b/GCD;
}
ARRAY_LIST
template<class T>
void ArrayList<T>::ensureCapacity(int cap){
/*
if cap > capacity:
new_capacity = capacity * 1.5;
create new array with new_capacity
else: do nothing
*/
if(cap > capacity)
{
int newCapacity = capacity*3/2;
int *newData = new int[newCapacity];
for(int i = 0; i < count; i++)
{
newData[i] = this->data[i];
}
this->capacity = newCapacity;
delete [] this->data;
this->data = newData;
}
}
template<class T>
void ArrayList<T>::add(int index, T e) {
/*
Insert an element into the array at given index.
if index is invalid:
throw std::out_of_range("the input index is out of range!");
*/
if(index > count||index < 0)
{
cout << "the input index is out of range!"<<endl;
}
else{
this->ensureCapacity(this->count+1);
for(int i = this->count-1; i>=index; i--)
{
this->data[i+1] = this->data[i];
}
this->data[index] = e;
this->count++;}
}
template<class T>
int ArrayList<T>::size() {
/* Return the length (size) of the array */
return count;
}
VECTOR
vector<int> updateArrayPerRange(vector<int>& nums, vector<vector<int>>& operations)
{
// STUDENT ANSWER
int size = operations.size();
for(int i = 0; i < size;i++)
{
int L,R,X;
L = operations[i][0];
R = operations[i][1];
X = operations[i][2];
for(int j = L; j <= R;j++)
{
nums[j] += X;
}
}
return nums;
}
int equalSumIndex(vector<int>& nums) {
// STUDENT ANSWER
int size = nums.size();
for(int i = 0; i < size;i++)
{
int left = 0;
int right = 0;
for(int n = i-1;n>=0;n--)
{
left += nums[n];
}
for(int m = i+1; m < size;m++)
{
right += nums[m];
}
if(left == right)
{
return i;
}
}
return -1;
}
int buyCar(int* nums, int length, int k) {
sort(nums, nums + length);
int count = 0;
int sum = k;
while(nums[count] <= sum)
{
sum -= nums[count];
count++;
}
return count;
}
template<class T>
void SLinkedList<T>::add(int index, const T& e) {
/* Insert an element into the list at given index. */
Node* pNew = new Node();
pNew->data = e;
pNew->next = NULL;
if(count == 0)
{
this->head = this->tail = pNew;
count++;
}
else
{
if(index == 0)
{
pNew->next = this->head;
this->head = pNew;
count+=1;
}
else if (index >= count)
{
this->tail->next = pNew;
this->tail = pNew;
count+=1;
}
else
{
Node* pre = this->head;
for(int i = 1; i < index; i++)
{
pre = pre->next;
}
pNew->next = pre->next;
pre->next = pNew;
}
}
}
template<class T>
int SLinkedList<T>::size() {
/* Return the length (size) of list */
return this->count;
}
template<class T>
T SLinkedList<T>::get(int index) {
/* Give the data of the element at given index in the list. */
Node* ptr = this->head;
for(int i = 0; i<index;i++)
{
ptr = ptr->next;
}
return ptr->data;
}
template <class T>
void SLinkedList<T>::set(int index, const T& e) {
/* Assign new value for element at given index in the list */
Node* ptr = this->head;
for(int i = 0; i<index;i++)
{
ptr = ptr->next;
}
ptr->data = e;
}
template<class T>
bool SLinkedList<T>::empty() {
/* Check if the list is empty or not. */
return (this->head == NULL && this->tail == NULL);
template<class T>
int SLinkedList<T>::indexOf(const T& item) {
/* Return the first index wheter item appears in list, otherwise return -1 */
Node* ptr = this->head;
int i = 0;
while(ptr != NULL)
{
if(ptr->data == item)
{
return i;
}
else
{
i+=1;
ptr = ptr->next;
}
}
return -1;
}
template<class T>
bool SLinkedList<T>::contains(const T& item) {
/* Check if item appears in the list */
Node* ptr = this->head;
while(ptr != NULL)
{
if(ptr->data == item)
{
return 1;
}
else
ptr = ptr->next;
}
return 0;
}
template <class T>
T SLinkedList<T>::removeAt(int index)
{
/* Remove element at index and return removed value */
T removed;
if(index == 0)
{
if(count == 1)
{
Node* ptr = this->head;
removed = ptr->data;
free(this->head);
this->head = NULL;
this->tail = NULL;
ptr = NULL;
count -=1;
return removed;
}
else
{
Node* ptr = this->head;
Node* tmp = this->head->next;
removed = ptr->data;
free(this->head);
this->head = tmp;
ptr = NULL;
count -=1;
return removed;
}
}
else if (index == count - 1)
{
Node* ptr = this->tail;
Node* tmp = this->head;
for(int i = 0; i < index -1 ;i++)
{
tmp = tmp->next;
}
removed = ptr->data;
free(this->tail);
tmp->next = NULL;
this->tail = tmp;
ptr = NULL;
count-=1;
return removed;
}
else
{
Node* ptr = this->head;
Node* pre = this->head;
for(int i = 0; i< index;i++)
{
ptr = ptr->next;
}
for(int i = 0; i<index-1;i++)
{
pre = pre->next;
}
removed = ptr->data;
pre->next = ptr->next;
free(ptr);
ptr = NULL;
count-=1;
return removed;
}
}
template<class T>
void SLinkedList<T>::clear(){
/* Remove all elements in list */
Node* tmp = this->head;
while(tmp != NULL)
{
tmp = this->head->next;
free(this->head);
this->head = tmp;
}
}