diff --git a/src/cw6/zad12/slowa.cpp b/src/cw6/zad12/slowa.cpp new file mode 100644 index 0000000..f595f59 --- /dev/null +++ b/src/cw6/zad12/slowa.cpp @@ -0,0 +1,56 @@ +#define STOP_SYNTAX 'c' + +#include +#include + +using namespace std; + +int slowa(string s) +{ + queue q, k; + // fill q with default data + q.push('a'); + q.push(STOP_SYNTAX); + q.push('b'); + q.push(STOP_SYNTAX); + int count = 1; + bool wasB = false; + + for (int i = 0; s[i]; i++) + { + if (s[i] == 'b' && !wasB) + wasB = true; + + // do we encounter next fib letter + if (s[i] == q.front()) + { + k.push(s[i]); + q.pop(); + + // if it was not last letter of word + if (q.front() == STOP_SYNTAX) + { + ++count; + k.push(STOP_SYNTAX); + // copy finished word to make queue for next one + while (k.front() != STOP_SYNTAX) + { + q.push(k.front()); + k.push(k.front()); + k.pop(); + } + } + } + } + + // free q,k but i cannot do it + if (wasB) + return count; + return 0; +} + +int main() +{ + string slowo = "abccxabbbaaxccbbba" + 0; + cout << slowa(slowo) << endl; +} \ No newline at end of file diff --git a/src/cw6/zad3/zad3_ms.cpp b/src/cw6/zad3/zad3_ms.cpp new file mode 100644 index 0000000..ac599b8 --- /dev/null +++ b/src/cw6/zad3/zad3_ms.cpp @@ -0,0 +1,93 @@ +#include +#include + +using namespace std; + +bool is_ice(int layer) +{ + return layer < 0; +} + +void stack_layer(stack *layers, int layer, bool isIce) +{ + if (isIce) + { + layers->push(-abs(layer)); + } + else + { + if (!layers->empty()) + layers->push(abs(layer)); + } +} + +void put_on_stack(stack *layers, int layer) +{ + if (layers->empty()) + { + stack_layer(layers, layer, is_ice(layer)); + return; + } + + int sameType = 0, otherType = 0; + while (otherType < abs(layer) && !layers->empty()) + { + int topLayer = layers->top(); + if (is_ice(layer) != is_ice(topLayer)) + { + otherType += abs(topLayer); + } + else + { + sameType += topLayer; + } + layers->pop(); + } + + if (otherType < abs(layer)) + { + put_on_stack(layers, layer); + return; + } + + int remainingLayerOfOtherType = otherType - abs(layer); + stack_layer(layers, remainingLayerOfOtherType, !is_ice(layer)); + stack_layer(layers, sameType+layer, is_ice(layer)); +} + +int sadzawka(int size, int arr[]) +{ + stack layers; + for (int i = 0; i < size; i++) + { + put_on_stack(&layers, arr[i]); + } + + // copy results + int resultsCount = (int)layers.size(); + for (int i = 0; i < resultsCount; i++) + { + arr[i] = layers.top(); + layers.pop(); + } + return resultsCount; +} + +int main() +{ + int size; + cin >> size; + int *arr = (int *)malloc((size_t)size * sizeof(int)); + for (int i = 0; i < size; i++) + { + cin >> arr[i]; + } + + int results = sadzawka(size, arr); + for (int i = 0; i < results; i++) + { + cout << arr[i] << endl; + } + + free(arr); +} \ No newline at end of file diff --git a/src/cw6/zad5/wtc_ms.cpp b/src/cw6/zad5/wtc_ms.cpp new file mode 100644 index 0000000..6e910f3 --- /dev/null +++ b/src/cw6/zad5/wtc_ms.cpp @@ -0,0 +1,62 @@ +#define NOTHING_HERE -1 + +#include +#include + +using namespace std; + +/* +5 # +4 # # +3 #### +2 ###### +1 ####### +i 1234567 +k 1214111 +*/ + +/// @brief +/// @param arr array with towers [1..n] +/// @param size elements count n +/// @param results array [1..n] with k's, when arr[i] = max(arr[i-results[i]+1], ... arr[i]) +/// @return +int *katastrofa(int *arr, int size) +{ + stack indexes; + // if all towers are lower than current, when + indexes.push(NOTHING_HERE); + + int *results = (int *)malloc((size_t)(size + 1) * sizeof(int)); + for (int i = 1; i <= size; i++) + { + // remove all lower / same towers, because there are irrelevants + while (indexes.top() != NOTHING_HERE && arr[indexes.top()] <= arr[i]) + indexes.pop(); + // because indexes.top() is the index of first higher than arr[i] tower + // if arr[i] is the highest yet then + if (indexes.top() == NOTHING_HERE) + results[i] = i; + else + results[i] = i - indexes.top(); + // add current tower + indexes.push(i); + } + return results; +} + +int main() +{ + int n; + cin>>n; + int* arr = (int*)malloc((size_t)(n+1)*sizeof(int)); + for(int i = 1; i<=n; i++){ + cin>>arr[i]; + } + int* results = katastrofa(arr,n); + for(int i = 1; i<=n; i++){ + cout< +#include + +using namespace std; + +int best_area(stack *towers, int arr[], int currentTower, int i) +{ + int max = 0; + while (!towers->empty() && arr[towers->top()] > currentTower) + { + int width = (i-1) - towers->top() + 1; + int area = width * arr[towers->top()]; + if (area > max) + { + max = area; + } + towers->pop(); + } + return max; +} + +int prostokat(int n, int arr[]) +{ + if (n <= 0) + return 0; + stack towers; + towers.push(0); + int max = 0; + for (int i = 1; i < n; i++) + { + int currArea = best_area(&towers, arr, arr[i], i); + if (currArea > max) + max = currArea; + towers.push(i); + } + return max; +} + +int prostokat_brut(int n, int arr[]) +{ + int max = 0; + for (int i = 0; i < n; i++) + { + int currMin = arr[i]; + for (int j = i; j < n; j++) + { + if (currMin > arr[j]) + currMin = arr[j]; + + int currArea = (j-i+1) * currMin; + if(currArea > max) + max=currArea; + } + } + return max; +} + +int main() +{ + int n; + cin >> n; + int *arr = (int *)malloc((size_t)n * sizeof(int)); + for (int i = 0; i < n; i++) + { + cin >> arr[i]; + } + int bestArea = prostokat(n, arr); + cout << bestArea << endl; + + free(arr); +} \ No newline at end of file diff --git a/src/cw6/zad9/zad9_ms.cpp b/src/cw6/zad9/zad9_ms.cpp new file mode 100644 index 0000000..ae53f16 --- /dev/null +++ b/src/cw6/zad9/zad9_ms.cpp @@ -0,0 +1,94 @@ +#include +#include + +// let: results fit in arr +void inc(int n, bool arr[]) +{ + if (n <= 0) + return; + + int j; + if (arr[0]) + { + arr[0] = false; + arr[1] = true; + + // double 1 can be at arr[1] arr[2] + j = 2; + } + else{ + arr[0] = true; + // double 1 can be at arr[0] arr[1] + j = 1; + + } + + while (j < n && arr[j - 1] && arr[j]) + { + // if there are two 1 side by side, then represent them as 1 on arr[j+1] + arr[j + 1] = true; + arr[j - 1] = false; + arr[j] = false; + + j+=2; + } +} + +int fib(int n) +{ + if (n == 0) + return 1; + if (n == 2) + return 2; + return fib(n - 2) + fib(n - 1); +} + +// int to_dec_from_zeckendorf(int n, bool arr[]) +// { +// int decValue = 0; +// for (int i = 0; i < n; i++) +// { +// if (arr[i]) +// decValue += fib(i); +// } +// return decValue; +// } + +int scan_bool(bool *results) +{ + int input; + int response = scanf("%d", &input); + if (response) + { + *results = (bool)input; + } + return response; +} + +int main() +{ + int n; + if (!scanf("%d", &n)) + { + printf("wrong n"); + return -1; + } + + bool *arr = (bool *)malloc((size_t)n * sizeof(bool)); + for (int i = 0; i < n; i++) + { + if (!scan_bool(&arr[i])) + { + printf("wrong bool"); + i--; + } + } + + inc(n, arr); + for (int i = 0; i < n; i++) + { + printf("%d\n", (int)arr[i]); + } + + free(arr); +} \ No newline at end of file diff --git a/src/cw7/zad1/zad1_ms.cpp b/src/cw7/zad1/zad1_ms.cpp new file mode 100644 index 0000000..edf93bb --- /dev/null +++ b/src/cw7/zad1/zad1_ms.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + +using namespace std; + +// median (middle element in array, should there be two, return smaller) +int median(const vector s){ + vector sorted(s.size()); + copy(s.begin(), s.end(),sorted.begin()); + sort(sorted.begin(), sorted.end()); + + // median is middle element in sorted array or average of two middle ones + // for purpose of this task, we just need the first number from the middle ones + return sorted[(s.size() - 1) / 2]; +} + +// to find how many hits we need to do, we just need to find median +// O(nlogn) +int slupki(const vector s) +{ + int pivot = median(s); + int hitsCount = 0; + for(int pole : s){ + hitsCount+=abs(pivot-pole); + } + return hitsCount; +} + +int main() +{ + int n; + cin>>n; + vector poles; + for(int i = 0; i>pole; + poles.push_back(pole); + } + + int hammerHits = slupki(poles); + cout< +#include +#include + +using namespace std; + +// T(n) = O(nlogn) +// M(n) = O(n) +bool trojkat(const std::vector v) +{ + if (v.size() < 3) + return false; + vector sorted(v.size()); + copy(v.begin(), v.end(), sorted.begin()); + sort(sorted.begin(), sorted.end()); + + for (int i = 2; i < v.size(); i++) + { + if (v[i - 2] + v[i - 1] > v[i]) + return true; + } + return false; +} + +int main() +{ + int n; + cin >> n; + vector v; + for (int i = 0; i < n; i++) + { + int input; + cin >> input; + v.push_back(input); + } + + cout << trojkat(v) << endl; +} \ No newline at end of file diff --git a/src/cw7/zad4/zad4_ms.cpp b/src/cw7/zad4/zad4_ms.cpp new file mode 100644 index 0000000..042453c --- /dev/null +++ b/src/cw7/zad4/zad4_ms.cpp @@ -0,0 +1,142 @@ +#include +#include +#include + +using namespace std; + +// T(n) = O(nlogn) +// M(n) = O(n) +// https://math.stackexchange.com/questions/3974110/sorting-an-array-to-get-the-maximum-combined-sum-of-the-differences-between-ever +std::vector przekładaniec(const std::vector x) +{ + vector sorted(x.size()); + copy(x.begin(), x.end(), sorted.begin()); + sort(sorted.begin(), sorted.end()); + + vector results(x.size()); + if (x.size() % 2 == 0) + { + bool ascending = true; + int smallestI = (x.size() / 2) - 1, biggestI = x.size() / 2; + for (int i = 0; i < x.size() / 2; i++) + { + if (ascending) + { + results[i] = sorted[smallestI--]; + results[x.size() - 1 - i] = sorted[biggestI++]; + } + else + { + results[i] = sorted[biggestI++]; + results[x.size() - 1 - i] = sorted[smallestI--]; + } + ascending = !ascending; + } + } + else{ + bool shouldPutSmallest = true; + int smallestI = 0, biggestI = x.size() - 1 - 1; + int pivot = x.size()/2; + results[pivot] = sorted[x.size()-1]; + for (int i = 1; i <= pivot; i++) + { + if (shouldPutSmallest) + { + results[pivot-i] = sorted[smallestI++]; + results[pivot+i] = sorted[smallestI++]; + } + else + { + results[pivot-i] = sorted[biggestI--]; + results[pivot+i] = sorted[biggestI--]; + } + shouldPutSmallest = !shouldPutSmallest; + } + } + + return results; +} + +float sum(const std::vector x) +{ + float sum = 0; + for (int i = 1; i < x.size(); i++) + { + sum += abs(x[i - 1] - x[i]); + } + return sum; +} + +// T(n) = O(n!) +// M(n) = O(n) +std::vector przekładaniec_brut(const std::vector x) +{ + vector perm(x.size()); + copy(x.begin(), x.end(), perm.begin()); + sort(perm.begin(), perm.end()); + + float max = 0; + vector results(x.size()); + while (next_permutation(perm.begin(), perm.end())) + { + float currentSum = sum(perm); + if (currentSum > max) + { + max = currentSum; + copy(perm.begin(), perm.end(), results.begin()); + } + } + return results; +} + +vector randomVector() +{ + srand((unsigned)time(NULL)); + int length = 0; + while (length <= 3) + { + length = rand() % 10; + } + vector ret; + for (int i = 0; i < length; i++) + { + float nextNum = (rand() % 10000) / 100.0; + ret.push_back(nextNum); + } + return ret; +} + +int main() +{ + int n; + cin >> n; + vector x; + for (int i = 0; i < n; i++) + { + float input; + cin >> input; + x.push_back(input); + } + + // vector x = randomVector(); + + vector bestPerm = przekładaniec(x); + //int sumBest = sum(bestPerm); + for (float a : bestPerm) + { + cout << a << endl; + } + + // vector brutPerm = przekładaniec_brut(x); + // int sumBrut = sum(brutPerm); + // if (sumBest != sumBrut) + // { + // cout << "bad :(" << endl; + // for (float a : brutPerm) + // { + // cout << a << endl; + // } + // cout << endl + // << sumBest << " != " << sumBrut << endl; + // } +} \ No newline at end of file diff --git a/src/cw7/zad5/zad5_ms.cpp b/src/cw7/zad5/zad5_ms.cpp new file mode 100644 index 0000000..9010f70 --- /dev/null +++ b/src/cw7/zad5/zad5_ms.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +using namespace std; + +bool is_smallest_in_x(const std::vector x, int *currentPosinX, queue *sums) +{ + if (sums->empty()) + return true; + if (*currentPosinX >= x.size()) + return false; + return sums->front() > x[*currentPosinX]; +} + +float get_smallest(const std::vector x, int *currentPosinX, queue *sums) +{ + if (is_smallest_in_x(x, currentPosinX, sums)) + { + (*currentPosinX)++; + return x[*currentPosinX - 1]; + } + + float smallest = sums->front(); + sums->pop(); + return smallest; +} + +float zlewki(const std::vector x, int k) +{ + queue sums; + int sumCount = 0; + int currentPos = 0; + + while (sumCount++ < k) + { + float smallest = get_smallest(x, ¤tPos, &sums); + float secondSmallest = get_smallest(x, ¤tPos, &sums); + sums.push(smallest + secondSmallest); + } + + return get_smallest(x, ¤tPos, &sums); +} + +int main() +{ + int k; cin >> k; + int n; cin >> n; + vector x; + for (int i = 0; i < n; i++) + { + float input; + cin >> input; + x.push_back(input); + } + + cout << zlewki(x, k) << endl; +} \ No newline at end of file diff --git a/src/cw8/zad1/zad1_ms.cpp b/src/cw8/zad1/zad1_ms.cpp new file mode 100644 index 0000000..4e3a9c1 --- /dev/null +++ b/src/cw8/zad1/zad1_ms.cpp @@ -0,0 +1,207 @@ +#include + +using namespace std; + +template +struct SingleNode +{ + T value; + SingleNode *next; +}; + +template +struct OneWayList +{ + SingleNode *head; +}; + +template +struct DoubleNode +{ + T value; + DoubleNode *previous; + DoubleNode *next; +}; + +template +struct TwoWayList +{ + DoubleNode *head; + DoubleNode *tail; +}; + +template +OneWayList *create_new_one_way_list() +{ + OneWayList *newList = (OneWayList *)malloc(sizeof(OneWayList)); + newList->head = NULL; + return newList; +} + +template +SingleNode *create_single_node(T value) +{ + SingleNode *newNode = (SingleNode *)malloc(sizeof(SingleNode)); + newNode->value = value; + newNode->next = NULL; + return newNode; +} + +template +TwoWayList *create_new_two_way_list() +{ + TwoWayList *newList = (TwoWayList *)malloc(sizeof(TwoWayList)); + newList->tail = NULL; + newList->head = NULL; + return newList; +} + +template +DoubleNode *create_double_node(T value) +{ + DoubleNode *newNode = (DoubleNode *)malloc(sizeof(DoubleNode)); + newNode->value = value; + newNode->next = NULL; + newNode->previous = NULL; + return newNode; +} + +// create new one way list with duplicated values: +// 3 2 -> 3 3 2 2 +// free(old list) +template +OneWayList *duplicate_values(OneWayList *originalList) +{ + if (originalList->head == NULL) + return create_new_one_way_list(); + OneWayList *newList = create_new_one_way_list(); + SingleNode **lastNodeFromDuplicated = &(newList->head); + SingleNode **lastNodeFromOrigin = &(originalList->head); + while (*lastNodeFromOrigin != NULL) + { + *lastNodeFromDuplicated = create_single_node((*lastNodeFromOrigin)->value); + (*lastNodeFromDuplicated)->next = create_single_node((*lastNodeFromOrigin)->value); + + lastNodeFromDuplicated = &((*lastNodeFromDuplicated)->next->next); + + lastNodeFromOrigin = &((*lastNodeFromOrigin)->next); + } + free(originalList); + return newList; +} + +template +void push_back(OneWayList *originalList, T value) +{ + SingleNode *node = create_single_node(value); + + // find last elem's adress + SingleNode **lastNode = &(originalList->head); + while (*lastNode != NULL) + { + lastNode = &((*lastNode)->next); + } + // put adress of node in the .next of current last elem + (*lastNode) = node; +} + +template +void push_back(TwoWayList *originalList, T value) +{ + DoubleNode *node = create_double_node(value); + if (originalList->head == NULL) + { + originalList->head = node; + originalList->tail = node; + } + else + { + originalList->tail->next = node; + node->previous = originalList->tail; + originalList->tail = node; + } +} + +// create new two way list with duplicated values: +// 3 2 -> 3 3 2 2 +// free(old list) +template +TwoWayList *duplicate_values(TwoWayList *originalList) +{ + if (originalList->head == NULL) + return create_new_two_way_list(); + TwoWayList *newList = create_new_two_way_list(); + + DoubleNode **lastNodeFromOrigin = &(originalList->head); + while (*lastNodeFromOrigin != NULL) + { + push_back(newList, (*lastNodeFromOrigin)->value); + push_back(newList, (*lastNodeFromOrigin)->value); + lastNodeFromOrigin = &((*lastNodeFromOrigin)->next); + } + free(originalList); + return newList; +} + +template +void print_list(OneWayList *list) +{ + cout << "head adress" << endl; + cout << (list->head) << endl; + SingleNode *currentElem = list->head; + while (currentElem != NULL) + { + cout << currentElem << " | " << currentElem->value << " " << (currentElem->next) << endl; + currentElem = currentElem->next; + } +} + +template +void print_list(TwoWayList *list) +{ + cout << "head adress, tail adress" << endl; + cout << (list->head) << ", " << (list->tail) << endl; + DoubleNode *currentElem = list->head; + while (currentElem != NULL) + { + cout << currentElem << " | " << currentElem->previous << " " << currentElem->value << " " << currentElem->next << endl; + currentElem = currentElem->next; + } +} + +void one_way_list_test() +{ + OneWayList *list = create_new_one_way_list(); + push_back(list, 3.0); + push_back(list, 2.0); + + cout << "Normal:" << endl; + print_list(list); + cout << "Double:" << endl; + print_list(duplicate_values(list)); +} + +void two_way_list_test() +{ + TwoWayList *list = create_new_two_way_list(); + push_back(list, 3.0); + push_back(list, 2.0); + + cout << "Normal:" << endl; + print_list(list); + cout << "Double:" << endl; + print_list(duplicate_values(list)); +} + +int main() +{ + cout << "one way" << endl + << endl; + one_way_list_test(); + cout << endl + << "or another" << endl + << endl; + two_way_list_test(); + cout << endl + << "I'm gonna find ya" << endl; +} \ No newline at end of file diff --git a/src/cw8/zad3/zad3_ms.cpp b/src/cw8/zad3/zad3_ms.cpp new file mode 100644 index 0000000..98537a0 --- /dev/null +++ b/src/cw8/zad3/zad3_ms.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +template +struct SingleNode +{ + T value; + SingleNode *next; +}; + +template +struct OneWayList +{ + SingleNode *head; +}; + +template +OneWayList *create_new_one_way_list() +{ + OneWayList *newList = (OneWayList *)malloc(sizeof(OneWayList)); + newList->head = NULL; + return newList; +} + +template +SingleNode *create_single_node(T value) +{ + SingleNode *newNode = (SingleNode *)malloc(sizeof(SingleNode)); + newNode->value = value; + newNode->next = NULL; + return newNode; +} + +template +void print_list(OneWayList *list) +{ + cout << "head adress" << endl; + cout << (list->head) << endl; + SingleNode *currentElem = list->head; + while (currentElem != NULL) + { + cout << currentElem << " | " << currentElem->value << " " << (currentElem->next) << endl; + currentElem = currentElem->next; + } +} + +template +OneWayList *shuffle(OneWayList *listA, OneWayList *listB) +{ + SingleNode *nodeA = NULL, *nodeB = NULL; + if (listA != NULL) + { + nodeA = listA->head; + } + if (listB != NULL) + { + nodeB = listB->head; + } + bool shouldPutNodeA = true; + OneWayList* shuffledList = create_new_one_way_list(); + SingleNode **placeForNewNode = &(shuffledList->head); + while (nodeA != NULL && nodeB != NULL) + { + if (shouldPutNodeA) + { + *placeForNewNode = nodeA; + nodeA = nodeA->next; + } + else + { + *placeForNewNode = nodeB; + nodeB = nodeB->next; + } + placeForNewNode = &((*placeForNewNode)->next); + shouldPutNodeA = !shouldPutNodeA; + } + + if (nodeA == NULL) + nodeA = nodeB; + while (nodeA != NULL) + { + *placeForNewNode = nodeA; + nodeA = nodeA->next; + placeForNewNode = &((*placeForNewNode)->next); + } + return shuffledList; +} + +template +void push_back(OneWayList *originalList, T value) +{ + SingleNode *node = create_single_node(value); + + // find last elem's adress + SingleNode **lastNode = &(originalList->head); + while (*lastNode != NULL) + { + lastNode = &((*lastNode)->next); + } + // put adress of node in the .next of current last elem + (*lastNode) = node; +} + +int main() +{ + OneWayList *listA = create_new_one_way_list(); + push_back(listA, 1); + push_back(listA, 3); + push_back(listA, 5); + push_back(listA, 6); + + OneWayList *listB = create_new_one_way_list(); + push_back(listB, 2); + push_back(listB, 4); + + OneWayList* shuffledList = shuffle(listA,listB); + print_list(shuffledList); +} \ No newline at end of file diff --git a/src/cw8/zad5/zad5_ms.cpp b/src/cw8/zad5/zad5_ms.cpp new file mode 100644 index 0000000..bcff3ac --- /dev/null +++ b/src/cw8/zad5/zad5_ms.cpp @@ -0,0 +1,135 @@ +#include + +using namespace std; + +template +struct SingleNode +{ + T value; + SingleNode *next; +}; + +template +struct OneWayList +{ + SingleNode *head; +}; + +template +OneWayList *create_new_one_way_list() +{ + OneWayList *newList = (OneWayList *)malloc(sizeof(OneWayList)); + newList->head = NULL; + return newList; +} + +template +SingleNode *create_single_node(T value) +{ + SingleNode *newNode = (SingleNode *)malloc(sizeof(SingleNode)); + newNode->value = value; + newNode->next = NULL; + return newNode; +} + +template +void print_list(OneWayList *list) +{ + cout << "head adress" << endl; + cout << (list->head) << endl; + SingleNode *currentElem = list->head; + while (currentElem != NULL) + { + cout << currentElem << " | " << currentElem->value << " " << (currentElem->next) << endl; + currentElem = currentElem->next; + } +} + +template +void push_back(OneWayList *originalList, SingleNode* node) +{ + // find last elem's adress + SingleNode **lastNode = &(originalList->head); + while (*lastNode != NULL) + { + lastNode = &((*lastNode)->next); + } + // put adress of node in the .next of current last elem + (*lastNode) = node; +} + +template +void push_back(OneWayList *originalList, T value) +{ + push_back(originalList, create_single_node(value)); +} + +template +OneWayList *find_union(OneWayList *listA, OneWayList *listB) +{ + if (listA == NULL || listB == NULL) + return NULL; + int posA = 0, posB = 0; + SingleNode *jumperA = listA->head; + SingleNode *jumperB = listB->head; + while (jumperA != jumperB) + { + if (jumperA != NULL) + { + jumperA = jumperA->next; + posA++; + } + if (jumperB != NULL) + { + jumperB = jumperB->next; + posB++; + } + } + + if (posA != posB) + { + // both pointers are pointing to NULL + jumperA = listA->head; + jumperB = listB->head; + while(posA>posB){ + jumperA = jumperA->next; + posA--; + } + while(posB>posA){ + jumperB = jumperB->next; + posB--; + } + while(jumperA != jumperB){ + jumperA = jumperA->next; + jumperB = jumperB->next; + } + } + + OneWayList *commonList = create_new_one_way_list(); + commonList->head = jumperA; + return commonList; +} + +int main(){ + OneWayList* listA = create_new_one_way_list(); + push_back(listA, 1); + push_back(listA, 3); + push_back(listA, 5); + + OneWayList* listB = create_new_one_way_list(); + push_back(listB, 2); + push_back(listB, 4); + + OneWayList* common = create_new_one_way_list(); + push_back(common,6); + push_back(common,7); + push_back(common,8); + push_back(common,9); + + // connect + push_back(listA, common->head); + push_back(listB, common->head); + + OneWayList* foundCommon = find_union(listA, listB); + print_list(foundCommon); +} \ No newline at end of file diff --git a/tests/cw6/zad3/zad3_ms0.in b/tests/cw6/zad3/zad3_ms0.in new file mode 100644 index 0000000..109944d --- /dev/null +++ b/tests/cw6/zad3/zad3_ms0.in @@ -0,0 +1,10 @@ +9 +2 +-12 +8 +-4 +2 +4 +-4 +1 +-3 \ No newline at end of file diff --git a/tests/cw6/zad3/zad3_ms0.out b/tests/cw6/zad3/zad3_ms0.out new file mode 100644 index 0000000..218a135 --- /dev/null +++ b/tests/cw6/zad3/zad3_ms0.out @@ -0,0 +1,3 @@ +-6 +4 +-2 diff --git a/tests/cw6/zad5/cpp/wtc_ms0.in b/tests/cw6/zad5/cpp/wtc_ms0.in new file mode 100644 index 0000000..0cc4bc8 --- /dev/null +++ b/tests/cw6/zad5/cpp/wtc_ms0.in @@ -0,0 +1,2 @@ +7 +2 4 3 5 3 2 1 diff --git a/tests/cw6/zad5/cpp/wtc_ms0.out b/tests/cw6/zad5/cpp/wtc_ms0.out new file mode 100644 index 0000000..23bf0ff --- /dev/null +++ b/tests/cw6/zad5/cpp/wtc_ms0.out @@ -0,0 +1,7 @@ +1 +2 +1 +4 +1 +1 +1 diff --git a/tests/cw6/zad9/zad9_ms0.in b/tests/cw6/zad9/zad9_ms0.in new file mode 100644 index 0000000..b656914 --- /dev/null +++ b/tests/cw6/zad9/zad9_ms0.in @@ -0,0 +1,2 @@ +10 +1 0 1 0 0 1 0 0 0 1 diff --git a/tests/cw6/zad9/zad9_ms0.out b/tests/cw6/zad9/zad9_ms0.out new file mode 100644 index 0000000..6dbc280 --- /dev/null +++ b/tests/cw6/zad9/zad9_ms0.out @@ -0,0 +1,10 @@ +0 +0 +0 +1 +0 +1 +0 +0 +0 +1 diff --git a/tests/cw6/zad9/zad9_ms1.in b/tests/cw6/zad9/zad9_ms1.in new file mode 100644 index 0000000..fc3375a --- /dev/null +++ b/tests/cw6/zad9/zad9_ms1.in @@ -0,0 +1,2 @@ +10 +0 1 0 1 0 0 1 0 0 1 \ No newline at end of file diff --git a/tests/cw6/zad9/zad9_ms1.out b/tests/cw6/zad9/zad9_ms1.out new file mode 100644 index 0000000..673c424 --- /dev/null +++ b/tests/cw6/zad9/zad9_ms1.out @@ -0,0 +1,10 @@ +0 +0 +0 +0 +1 +0 +1 +0 +0 +1 diff --git a/tests/cw7/zad5/zad5_ms0.in b/tests/cw7/zad5/zad5_ms0.in new file mode 100644 index 0000000..b2913f6 --- /dev/null +++ b/tests/cw7/zad5/zad5_ms0.in @@ -0,0 +1,2 @@ +9 10 +1 2 3 4 5 6 7 8 9 10 diff --git a/tests/cw7/zad5/zad5_ms0.out b/tests/cw7/zad5/zad5_ms0.out new file mode 100644 index 0000000..c3f407c --- /dev/null +++ b/tests/cw7/zad5/zad5_ms0.out @@ -0,0 +1 @@ +55 diff --git a/tests/cw7/zad5/zad5_ms1.in b/tests/cw7/zad5/zad5_ms1.in new file mode 100644 index 0000000..1c6d9a5 --- /dev/null +++ b/tests/cw7/zad5/zad5_ms1.in @@ -0,0 +1,2 @@ +2 5 +3 4 5 6 21 \ No newline at end of file diff --git a/tests/cw7/zad5/zad5_ms1.out b/tests/cw7/zad5/zad5_ms1.out new file mode 100644 index 0000000..7ed6ff8 --- /dev/null +++ b/tests/cw7/zad5/zad5_ms1.out @@ -0,0 +1 @@ +5