diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..bda3ac9 --- /dev/null +++ b/.clang-format @@ -0,0 +1,243 @@ +Language: Cpp +# BasedOnStyle: WebKit +AccessModifierOffset: -4 +AlignAfterOpenBracket: DontAlign +AlignArrayOfStructures: None +AlignConsecutiveAssignments: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: true +AlignConsecutiveBitFields: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveDeclarations: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveMacros: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveShortCaseStatements: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCaseColons: false +AlignEscapedNewlines: Right +AlignOperands: DontAlign +AlignTrailingComments: + Kind: Never + OverEmptyLines: 0 +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowBreakBeforeNoexceptSpecifier: Never +AllowShortBlocksOnASingleLine: Empty +AllowShortCaseLabelsOnASingleLine: false +AllowShortCompoundRequirementOnASingleLine: true +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: true +BitFieldColonSpacing: Both +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterExternBlock: false + AfterFunction: true + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakAdjacentStringLiterals: true +BreakAfterAttributes: Leave +BreakAfterJavaFieldAnnotations: false +BreakArrays: true +BreakBeforeBinaryOperators: All +BreakBeforeConceptDeclarations: Always +BreakBeforeBraces: WebKit +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeComma +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +ColumnLimit: 0 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: false +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: false +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseBlocks: false +IndentCaseLabels: false +IndentExternBlock: AfterExternBlock +IndentGotoLabels: true +IndentPPDirectives: None +IndentRequiresClause: true +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertBraces: false +InsertNewlineAtEOF: false +InsertTrailingCommas: None +IntegerLiteralSeparator: + Binary: 0 + BinaryMinDigits: 0 + Decimal: 0 + DecimalMinDigits: 0 + Hex: 0 + HexMinDigits: 0 +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +KeepEmptyLinesAtEOF: false +LambdaBodyIndentation: Signature +LineEnding: DeriveLF +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: Inner +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 4 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: true +ObjCSpaceBeforeProtocolList: true +PackConstructorInitializers: BinPack +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakScopeResolution: 500 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyIndentedWhitespace: 0 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Left +PPIndentWidth: -1 +QualifierAlignment: Leave +ReferenceAlignment: Pointer +ReflowComments: true +RemoveBracesLLVM: false +RemoveParentheses: Leave +RemoveSemicolon: false +RequiresClausePosition: OwnLine +RequiresExpressionIndentation: OuterScope +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SkipMacroDefinitionBody: false +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: LexicographicNumeric +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: true +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeJsonColon: false +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDefinitionName: false + AfterFunctionDeclarationName: false + AfterIfMacros: true + AfterOverloadedOperator: false + AfterPlacementOperator: true + AfterRequiresInClause: false + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: true +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInContainerLiterals: true +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParens: Never +SpacesInParensOptions: + InCStyleCasts: false + InConditionalStatements: false + InEmptyParentheses: false + Other: false +SpacesInSquareBrackets: false +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseTab: Never +VerilogBreakBetweenInstancePorts: true +WhitespaceSensitiveMacros: + - BOOST_PP_STRINGIZE + - CF_SWIFT_NAME + - NS_SWIFT_NAME + - PP_STRINGIZE + - STRINGIZE \ No newline at end of file diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..355e687 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: github-actions + directory: / + schedule: + interval: "weekly" \ No newline at end of file diff --git a/.github/workflows/build-and-lint.yml b/.github/workflows/build-and-lint.yml new file mode 100644 index 0000000..31983d4 --- /dev/null +++ b/.github/workflows/build-and-lint.yml @@ -0,0 +1,14 @@ +name: Build and lint + +on: + - push + - pull_request + +jobs: + build-and-lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + - name: Check format + run: | + find . -path ./build -prune -o -type f -name '*.[c|h]' -print | xargs clang-format-18 --style=file --dry-run -Werror \ No newline at end of file diff --git a/counting/CMakeLists.txt b/counting/CMakeLists.txt new file mode 100644 index 0000000..ef457e6 --- /dev/null +++ b/counting/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.25) +project(count C) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) + +# Создаём библиотеку из файла в поддиректории +add_library(cyclicList cyclicList.c) + +# Создаём исполняемый файл из файла в другой поддиректории +add_executable(main main.c) + +# Линкуем библиотеку +target_link_libraries(main PRIVATE cyclicList) \ No newline at end of file diff --git a/counting/cyclicList.c b/counting/cyclicList.c new file mode 100644 index 0000000..8c844a9 --- /dev/null +++ b/counting/cyclicList.c @@ -0,0 +1,154 @@ +#include +#include +#include + +// узел списка +typedef struct ListNode { + // значение + int value; + // ссылка на следующий узел + struct ListNode* next; +} ListNode; + +typedef struct List { + ListNode* head; + ListNode* tail; + // длина + int size; +} List; + +// создание нового списка +List* newList() +{ + List* list = (List*)malloc(sizeof(List)); + list->head = NULL; + list->tail = NULL; + list->size = 0; + return list; +} + +// Проверка на пустоту +bool isEmpty(List* list) +{ + return list->head == NULL || list == NULL; +} + +// получение размера списка +int getSize(List* list) +{ + return list->size; +} + +// вставка элемента +void insert(List* list, int value) +{ + ListNode* newNode = (ListNode*)malloc(sizeof(ListNode)); + newNode->value = value; + + if (isEmpty(list)) { + // первый элемент - указываем на себя самого + newNode->next = newNode; + list->head = newNode; + list->tail = newNode; + } else { + // Новый элемент - указывает на голову + newNode->next = list->head; + // Старый хвост указывает на новый элемент + list->tail->next = newNode; + // Обновляем хвост + list->tail = newNode; + } + + list->size++; +} + +// удаление элемента по значению +bool deleteNode(List* list, int value) +{ + // если список пустой + if (isEmpty(list)) { + return false; + } + + ListNode* current = list->head; + ListNode* prev = list->tail; + + int i = 0; + while (i < list->size) { + if (current->value == value) { + // если удаляем единственный элемент + if (list->size == 1) { + free(current); + list->head = NULL; + list->tail = NULL; + } else { + // удаляем элемент + prev->next = current->next; + + // если удаляем голову + if (current == list->head) { + list->head = current->next; + } + + // если удаляем хвост + if (current == list->tail) { + list->tail = prev; + } + + free(current); + } + + list->size--; + return true; + } + + prev = current; + current = current->next; + i++; + } + + return false; +} + +// вывод списка +void printList(List* list) +{ + if (isEmpty(list)) { + printf("Список пуст\n"); + return; + } + + ListNode* current = list->head; + printf("Список: "); + + int i = 0; + while (i < list->size) { + printf("%d ", current->value); + current = current->next; + i++; + } + + printf("\n"); +} + +// удаление всего списка +void deleteList(List* list) +{ + if (isEmpty(list)) { + free(list); + return; + } + + ListNode* current = list->head; + + int i = 0; + while (i < list->size) { + ListNode* temp = current; + current = current->next; + free(temp); + i++; + } + + list->size = 0; + free(list); +} \ No newline at end of file diff --git a/counting/cyclicList.h b/counting/cyclicList.h new file mode 100644 index 0000000..816b106 --- /dev/null +++ b/counting/cyclicList.h @@ -0,0 +1,23 @@ +#pragma once +#include + +// узел списка +typedef struct ListNode { + int value; + struct ListNode* next; +} ListNode; + +typedef struct List { + ListNode* head; + ListNode* tail; + // длина + int size; +} List; + +List* newList(); +bool isEmpty(List* list); +int getSize(List* list); +void insert(List* list, int value); +bool deleteNode(List* list, int value); +void printList(List* list); +void deleteList(List* list); diff --git a/counting/main.c b/counting/main.c new file mode 100644 index 0000000..6e19594 --- /dev/null +++ b/counting/main.c @@ -0,0 +1,45 @@ +#include "cyclicList.h" +#include +#include +#include + +int main(int argc, char** argv) +{ + // Количество воинов + int n = 0; + // какого война будут убивать + int m = 0; + + while (n <= 0 || m <= 0) { + printf("Введите количество войнов: "); + scanf("%d", &n); + printf("Введите, какого по счету война будут убивать: "); + scanf("%d", &m); + } + + List* warriors = newList(); + for (int ind = 1; ind < n + 1; ind++) { + insert(warriors, ind); + } + + ListNode* current = warriors->head; + int count = 1; + while (getSize(warriors) > 1) { + // Если дошли до m-го воина + if (count == m) { + ListNode* toRemove = current; + current = current->next; + deleteNode(warriors, toRemove->value); + // Сбрасываем счетчик + count = 1; + } else { + // Переходим к следующему воину + current = current->next; + count++; + } + } + printf("Последний выживший был под номером: "); + printList(warriors); + deleteList(warriors); + return 0; +} \ No newline at end of file