-
Notifications
You must be signed in to change notification settings - Fork 0
Number 2 #30
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
base: main
Are you sure you want to change the base?
Number 2 #30
Changes from 6 commits
e700253
06ddbf7
e6d1b29
f3e1512
5392d0c
400deae
abcb64b
6f2d365
debdaff
bfade1e
0cb42a9
d5d468f
aec798e
3f832a2
882c086
3890aab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 16 | ||
| VisualStudioVersion = 16.0.31410.357 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CyclicList", "CyclicList\CyclicList.vcxproj", "{3E224E7A-155B-41F9-8988-80A502835B6E}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|x64 = Debug|x64 | ||
| Debug|x86 = Debug|x86 | ||
| Release|x64 = Release|x64 | ||
| Release|x86 = Release|x86 | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {3E224E7A-155B-41F9-8988-80A502835B6E}.Debug|x64.ActiveCfg = Debug|x64 | ||
| {3E224E7A-155B-41F9-8988-80A502835B6E}.Debug|x64.Build.0 = Debug|x64 | ||
| {3E224E7A-155B-41F9-8988-80A502835B6E}.Debug|x86.ActiveCfg = Debug|Win32 | ||
| {3E224E7A-155B-41F9-8988-80A502835B6E}.Debug|x86.Build.0 = Debug|Win32 | ||
| {3E224E7A-155B-41F9-8988-80A502835B6E}.Release|x64.ActiveCfg = Release|x64 | ||
| {3E224E7A-155B-41F9-8988-80A502835B6E}.Release|x64.Build.0 = Release|x64 | ||
| {3E224E7A-155B-41F9-8988-80A502835B6E}.Release|x86.ActiveCfg = Release|Win32 | ||
| {3E224E7A-155B-41F9-8988-80A502835B6E}.Release|x86.Build.0 = Release|Win32 | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {11CCDD59-DDF5-48B1-85A1-8BE50AF8E546} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| #include "CircleOfMurders.h" | ||
| #include "CyclicList.h" | ||
|
|
||
| int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* error) | ||
| { | ||
| if (numberOfWarriors == 0) | ||
| { | ||
| *error = 2; | ||
| return 0; | ||
| } | ||
| if (frequencyOfMurders == 0) | ||
| { | ||
| *error = 2; | ||
| return 0; | ||
| } | ||
| List* newList = createList(); | ||
| int errorCode = 0; | ||
| for (int i = 1; i <= numberOfWarriors; i++) | ||
| { | ||
| add(newList, i, &errorCode); | ||
| if (errorCode == 3) | ||
| { | ||
| deleteList(newList); | ||
| *error = 3; | ||
| return 0; | ||
| } | ||
| } | ||
| Position* firstPosition = first(newList, &errorCode); | ||
| if (errorCode == 3) | ||
| { | ||
| deleteList(newList); | ||
| *error = 3; | ||
| return 0; | ||
| } | ||
| int counter = 1; | ||
| while (newList->head != newList->tail) | ||
| { | ||
| if (counter % frequencyOfMurders == 0) | ||
| { | ||
| removeElement(newList, firstPosition, &errorCode); | ||
| if (errorCode == 1) | ||
| { | ||
| free(firstPosition); | ||
| deleteList(newList); | ||
| *error = 1; | ||
| return 0; | ||
| } | ||
| previous(firstPosition); | ||
| } | ||
| next(firstPosition); | ||
| counter++; | ||
| } | ||
| const int answer = newList->head->value; | ||
| free(firstPosition); | ||
| deleteList(newList); | ||
| return answer; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #pragma once | ||
|
|
||
| // Function for finding the position of the surviving warrior | ||
| int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* error); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| #include "CyclicList.h" | ||
| #include <stdio.h> | ||
|
|
||
| List* createList() | ||
| { | ||
| return calloc(1, sizeof(List)); | ||
| } | ||
|
|
||
| void deleteList(List* list) | ||
| { | ||
| ListElement* position = list->head; | ||
| while (list->head != list->tail) | ||
| { | ||
| list->head = list->head->next; | ||
| free(position); | ||
| position = list->head; | ||
| } | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
| free(position); | ||
| free(list); | ||
| } | ||
|
|
||
| Position* first(List* list, int* error) | ||
| { | ||
| *error = 0; | ||
| Position* positionFirst = malloc(sizeof(Position)); | ||
| if (positionFirst == NULL) | ||
| { | ||
| *error = 3; | ||
| return NULL; | ||
| } | ||
| positionFirst->position = list->head; | ||
| return positionFirst; | ||
| } | ||
|
|
||
| Position* next(Position* position) | ||
| { | ||
| position->position = position->position->next; | ||
| return position; | ||
| } | ||
|
|
||
| Position* previous(Position* position) | ||
| { | ||
| position->position = position->position->previous; | ||
| return position; | ||
| } | ||
|
|
||
| void add(List* list, int value, int* error) | ||
| { | ||
| int errorCode = 0; | ||
| ListElement* newElement = calloc(1, sizeof(ListElement)); | ||
| if (newElement == NULL) | ||
| { | ||
| *error = 3; | ||
| return; | ||
| } | ||
| newElement->value = value; | ||
| if (list->head == NULL) | ||
| { | ||
| list->head = newElement; | ||
| list->tail = newElement; | ||
| list->tail->next = list->head; | ||
| list->tail->previous = list->head; | ||
| list->head->next = list->tail; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это излишне. list->head и list->tail указывают на один и тот же newElement, так что эта строчка повторяет то, что уже сделано двумя строками выше. |
||
| return; | ||
| } | ||
| newElement->previous = list->tail; | ||
| list->tail->next = newElement; | ||
| newElement->next = list->head; | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
| list->tail = list->tail->next; | ||
| } | ||
|
|
||
| void removeElement(List* list, Position* position, int* error) | ||
| { | ||
| if (list->head == NULL || list->tail == NULL) | ||
| { | ||
| *error = 1; | ||
| return; | ||
| } | ||
| if (position->position == list->head) | ||
| { | ||
| if (list->tail == list->head) | ||
| { | ||
| list->tail = NULL; | ||
| list->head = NULL; | ||
| return; | ||
| } | ||
| list->tail->next = list->head->next; | ||
| list->head->next->previous = list->tail; | ||
| list->head = list->head->next; | ||
| return; | ||
| } | ||
| if (position->position == list->tail) | ||
| { | ||
| list->tail->previous->next = list->head; | ||
| list->head->previous = list->tail->previous; | ||
| list->tail = list->tail->previous; | ||
| return; | ||
| } | ||
| position->position->previous->next = position->position->next; | ||
| position->position->next->previous = position->position->previous; | ||
| position->position->previous = position->position; | ||
| position->position = position->position->next; | ||
| } | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #pragma once | ||
| #include <stdbool.h> | ||
| #include <malloc.h> | ||
|
|
||
| // Structure containing pointers to the "first" and "last" list item | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не-а. Structure that represents cyclic list, скорее. Что она содержит --- это детали реализации, которые могут меняться в процессе рефакторинга или оптимизации программы. Почитайте где-нибудь про абстрактные типы данных (например, Ахо и др., "Структуры данных и алгоритмы", эта часть курса в целом по ней), мне кажется, у Вас с принципом абстракции проблемы, а это самый важный принцип в программировании вообще. |
||
| typedef struct List | ||
| { | ||
| struct ListElement* head; | ||
| struct ListElement* tail; | ||
| } List; | ||
|
|
||
| // Structure containing pointers to the next and previous list item and a value variable for list items | ||
| typedef struct ListElement | ||
| { | ||
| int value; | ||
| struct ListElement* next; | ||
| struct ListElement* previous; | ||
| } ListElement; | ||
|
|
||
| // Structure containing a pointer to the position of a list item | ||
| typedef struct Position | ||
| { | ||
| ListElement* position; | ||
| } Position; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут тоже, надо "упаковать" это всё в АТД "Циклический список", который бы следил за своими инвариантами, не раскрывал своего внутреннего устройства и был переиспользуемым. Конечно, отвалятся тесты, но они и не должны тестировать внутреннее устройство, они должны тестировать наблюдаемое поведение (иначе при изменении чего-то в реализации придётся и тесты переписывать). |
||
|
|
||
| // Function for creating a list | ||
| List* createList(); | ||
|
|
||
| // Function for deleting a list | ||
| void deleteList(List* list); | ||
|
|
||
| // Function for adding an item to a list | ||
| void add(List* list, int value, int* error); | ||
|
|
||
| // Function for the first position | ||
| Position* first(List* list, int* error); | ||
|
|
||
| // Function to remove an item from the list | ||
| void removeElement(List* list, Position* position, int* error); | ||
|
|
||
| // Function for moving to the next position | ||
| Position* next(Position* position); | ||
|
|
||
| // Function for moving to the previous position | ||
| Position* previous(Position* position); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Стоит тут функцию для доступа к значению в голове сделать. Незачем лезть в личную жизнь списка.