-
Notifications
You must be signed in to change notification settings - Fork 0
Homework 6 - Task 2 - [Counting rhyme]
#12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f78d5ea
Added cyclic list (hw6 task 2)
ilya-krivtsov bb8d7a3
Refactored list (hw6 task 2)
ilya-krivtsov ee1b602
Homework 6 task 2
ilya-krivtsov 6470ad2
Added missing return statement
ilya-krivtsov 6f7007d
Merge branch 'build-system' into hw-6-task-2
ilya-krivtsov 7d2520a
Merge branch 'build-system' into hw-6-task-2
ilya-krivtsov 9a3f236
Merge branch 'hw-6' into hw-6-task-2
ilya-krivtsov fedd92f
Merge branch 'main' into hw-6-task-2
ilya-krivtsov 8645625
Merge branch 'main' into hw-6-task-2
ilya-krivtsov 7e5f2d6
Added stdbool.h includes (hw6 task 2)
ilya-krivtsov d226c39
Added stdlib.h include (hw6 task 2)
ilya-krivtsov f6adfe0
Merge branch 'hw-6' into hw-6-task-2
ilya-krivtsov 45e8ccf
Added task name to README (hw6 task 2)
ilya-krivtsov 42a4166
Fixed task name (hw6 task 2)
ilya-krivtsov 35ca9d5
Merge branch 'main' into hw-6-task-2
ilya-krivtsov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| project("${homeworkName}_task_2") | ||
|
|
||
| add_library(cyclicList cyclicList.c) | ||
|
|
||
| add_executable(${PROJECT_NAME} main.c) | ||
| target_link_libraries(${PROJECT_NAME} cyclicList) | ||
|
|
||
| add_executable(${PROJECT_NAME}_test test.c) | ||
| target_link_libraries(${PROJECT_NAME}_test cyclicList) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #include "cyclicList.h" | ||
|
|
||
| #include <stdbool.h> | ||
| #include <stdlib.h> | ||
|
|
||
| typedef struct ListElement { | ||
| struct ListElement *left; | ||
| struct ListElement *right; | ||
| int value; | ||
| } ListElement; | ||
|
|
||
| bool addElementAfter(ListElement *previous, ListElement **newElement, int value) { | ||
| ListElement *element = malloc(sizeof(ListElement)); | ||
| if (element == NULL) { | ||
| return false; | ||
| } | ||
|
|
||
| if (previous == NULL) { | ||
| element->left = element; | ||
| element->right = element; | ||
| } else { | ||
| ListElement *next = previous->right; | ||
| element->left = previous; | ||
| element->right = next; | ||
|
|
||
| next->left = element; | ||
| previous->right = element; | ||
| } | ||
|
|
||
| element->value = value; | ||
| *newElement = element; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| ListElement *getNext(ListElement *element) { | ||
| return element->right; | ||
| } | ||
|
|
||
| int getValue(ListElement *element) { | ||
| return element->value; | ||
| } | ||
|
|
||
| void removeElement(ListElement *element) { | ||
| ListElement *left = element->left; | ||
| ListElement *right = element->right; | ||
| left->right = right; | ||
| right->left = left; | ||
| free(element); | ||
| } | ||
|
|
||
| void removeAllElements(ListElement *element) { | ||
| ListElement *right = element->right; | ||
| do { | ||
| ListElement *next = right->right; | ||
| removeElement(right); | ||
| right = next; | ||
| } while (right != element); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #pragma once | ||
|
|
||
| #include <stdbool.h> | ||
|
|
||
| /// @brief Cyclic list element | ||
| typedef struct ListElement ListElement; | ||
|
|
||
| /// @brief Adds new element after specified element | ||
| /// @param previous Element that will be on the left relative to new element, | ||
| /// must be `NULL` when creating new list | ||
| /// @param newElement Pointer to store created `ListElement` to | ||
| /// @param value Value to store in new element | ||
| /// @return `true` if added successfully, `false` otherwise (allocaton failed) | ||
| bool addElementAfter(ListElement *previous, ListElement **newElement, int value); | ||
|
|
||
| /// @brief Gets element on the right of the specified element | ||
| /// @param element Element of cyclic list | ||
| /// @return Element on the right of the specified element | ||
| ListElement *getNext(ListElement *element); | ||
|
|
||
| /// @brief Gets value of the specified element | ||
| /// @param element Element of cyclic list | ||
| /// @return Value of specified element | ||
| int getValue(ListElement *element); | ||
|
|
||
| /// @brief Removes an element (and calls `free()` on it) | ||
| /// @param element Element to remove | ||
| void removeElement(ListElement *element); | ||
|
|
||
| /// @brief Removes all elements of cyclic list | ||
| /// @param element Any element of the list | ||
| void removeAllElements(ListElement *element); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
|
|
||
| #include "cyclicList.h" | ||
|
|
||
| bool getLastWarrior(int warriorsCount, int killersPerWarrior, int *result) { | ||
| ListElement *lastElement = NULL; | ||
| for (int i = 0; i < warriorsCount; ++i) { | ||
| ListElement *newElement = NULL; | ||
| if (!addElementAfter(lastElement, &newElement, i + 1)) { | ||
| return false; | ||
| } | ||
|
|
||
| lastElement = newElement; | ||
| } | ||
|
|
||
| // set lastElement to point to element | ||
| lastElement = getNext(lastElement); | ||
|
|
||
| while (getNext(lastElement) != lastElement) { | ||
| for (int i = 0; i < killersPerWarrior; ++i) { | ||
| lastElement = getNext(lastElement); | ||
| } | ||
| ListElement *next = getNext(lastElement); | ||
| removeElement(lastElement); | ||
| lastElement = next; | ||
| } | ||
|
|
||
| *result = getValue(lastElement); | ||
| removeAllElements(lastElement); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| int readValue(const char *prompt, const char *incorrectValueMessage) { | ||
| int value; | ||
| printf("%s", prompt); | ||
| while ((scanf("%d", &value) != 1) || value <= 0) { | ||
| while (getchar() != '\n') {} | ||
| printf("%s", incorrectValueMessage); | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| int main(void) { | ||
| int warriorsCount = readValue("warriors count: ", "incorrect value; warriors count should be greater than zero; try again: "); | ||
| int nthWarrior = readValue("n-th warrior to be killed: ", "incorrect value; n should be greater than zero; try again: "); | ||
|
|
||
| int result = 0; | ||
| if (!getLastWarrior(warriorsCount, nthWarrior - 1, &result)) { | ||
| printf("allocation error\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| printf("last warrior: %d\n", result); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #define CTEST_MAIN | ||
| #define CTEST_SEGFAULT | ||
| #include "../../ctest/ctest.h" | ||
|
|
||
| #include <stdlib.h> | ||
|
|
||
| #include "cyclicList.h" | ||
|
|
||
| int main(int argc, const char *argv[]) { | ||
| return ctest_main(argc, argv); | ||
| } | ||
|
|
||
| ListElement *add(ListElement *after, int value) { | ||
| ListElement *element; | ||
| ASSERT_TRUE(addElementAfter(after, &element, value)); | ||
| ASSERT_NOT_NULL(element); | ||
|
|
||
| return element; | ||
| } | ||
|
|
||
| CTEST(cyclicListTests, createTest) { | ||
| ListElement *element = add(NULL, 0); | ||
|
|
||
| ASSERT_TRUE(getNext(element) == element); | ||
| ASSERT_TRUE(getNext(getNext(element)) == element); | ||
|
|
||
| removeAllElements(element); | ||
| } | ||
|
|
||
| CTEST(cyclicListTests, addElementTest) { | ||
| ListElement *elementA = add(NULL, 0); | ||
| ListElement *elementB = add(elementA, 1); | ||
| ListElement *elementC = add(elementB, 2); | ||
| ListElement *elementD = add(elementC, 3); | ||
|
|
||
| ASSERT_TRUE(getNext(elementA) == elementB); | ||
| ASSERT_TRUE(getNext(elementB) == elementC); | ||
| ASSERT_TRUE(getNext(elementC) == elementD); | ||
| ASSERT_TRUE(getNext(elementD) == elementA); | ||
|
|
||
| ASSERT_EQUAL(getValue(elementA), 0); | ||
| ASSERT_EQUAL(getValue(elementB), 1); | ||
| ASSERT_EQUAL(getValue(elementC), 2); | ||
| ASSERT_EQUAL(getValue(elementD), 3); | ||
|
|
||
| removeAllElements(elementA); | ||
| } | ||
|
|
||
| CTEST(cyclicListTests, removeElementTest) { | ||
| ListElement *elementA = add(NULL, 0); | ||
| ListElement *elementB = add(elementA, 0); | ||
| ListElement *elementC = add(elementB, 0); | ||
| ListElement *elementD = add(elementC, 0); | ||
|
|
||
| removeElement(elementB); | ||
| ASSERT_TRUE(getNext(elementA) == elementC); | ||
|
|
||
| removeElement(elementC); | ||
| ASSERT_TRUE(getNext(elementA) == elementD); | ||
|
|
||
| removeElement(elementD); | ||
| ASSERT_TRUE(getNext(elementA) == elementA); | ||
|
|
||
| removeAllElements(elementA); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.