-
Notifications
You must be signed in to change notification settings - Fork 0
ДЗ №6 #13
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
Open
shknoko
wants to merge
23
commits into
master
Choose a base branch
from
06_lists
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
ДЗ №6 #13
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
8a33699
Balance of brackets checker
shknoko 0018de6
substring count
shknoko dc4b0b5
zero elements count
shknoko c7ba069
removed logic from main
shknoko 59d25c1
Brackets checker
shknoko 1bc898f
Removed logic from main
shknoko 9e12898
changed return type
shknoko b34f58a
removed pointers in functions
shknoko 3f2979f
List implementation
shknoko 52ecf12
Added printList, extended indexOf and isEmpty
shknoko 616b4a4
Sorted list program
shknoko 336d68d
Rewrited with switch
shknoko 8b30626
Circular list implementation
shknoko 25e713a
Josephus problem
shknoko c7750f7
Fixed circularList.h
shknoko 9798927
Fixed condition
shknoko 9634190
Updated .gitignore
shknoko ea39643
Fixed codestyle
shknoko 1f0fff6
Refactored sortedList
shknoko 3deb7dc
Refactored josephusProblem
shknoko a8c118b
Removed redundant condition
shknoko bedc32a
Added newline character in printf
shknoko f268eaa
Added CMakeLists
shknoko 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| cmake_minimum_required(VERSION 3.10) | ||
| project(06_lists) | ||
|
|
||
| set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../bin) | ||
| set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib) | ||
| set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib) | ||
|
|
||
| add_library(List list.c) | ||
| add_library(circularList circularList.c) | ||
|
|
||
| add_executable(sortedList sortedList.c) | ||
| target_link_libraries(sortedList List) | ||
|
|
||
| add_executable(josephus josephusProblem.c) | ||
| target_link_libraries(josephus circularList) | ||
|
|
||
| add_compile_options(-Wall -Wextra -pedantic) |
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,204 @@ | ||
| #include "circularList.h" | ||
|
|
||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| typedef struct Node { | ||
| int data; | ||
| struct Node* next; | ||
| } Node; | ||
|
|
||
| typedef struct List { | ||
| Node* head; | ||
| } List; | ||
|
|
||
| List* newList() | ||
| { | ||
| List* newList = calloc(1, sizeof(List)); | ||
| return newList; | ||
| } | ||
|
|
||
| Node* createNode(int data) | ||
| { | ||
| Node* newNode = calloc(1, sizeof(Node)); | ||
|
|
||
| if (newNode == NULL) { | ||
| return NULL; | ||
| } | ||
|
|
||
| newNode->data = data; | ||
| newNode->next = NULL; | ||
|
|
||
| return newNode; | ||
| } | ||
|
|
||
| bool insert(List* list, int data, unsigned index) | ||
| { | ||
| if (list == NULL) { | ||
| return false; | ||
| } | ||
|
|
||
| Node* newNode = createNode(data); | ||
|
|
||
| if (newNode == NULL) { | ||
| return false; | ||
| } | ||
|
|
||
| if (list->head == NULL) { | ||
| if (index == 0) { | ||
| newNode->next = newNode; | ||
| list->head = newNode; | ||
| return true; | ||
| } | ||
| free(newNode); | ||
| return false; | ||
| } | ||
|
|
||
| if (index == 0) { | ||
| newNode->next = list->head; | ||
| Node* current = list->head; | ||
|
|
||
| while (current->next != list->head) { | ||
| current = current->next; | ||
| } | ||
|
|
||
| current->next = newNode; | ||
| list->head = newNode; | ||
| return true; | ||
| } | ||
|
|
||
| Node* before = list->head; | ||
|
|
||
| for (unsigned i = 1; i < index; i++) { | ||
| before = before->next; | ||
|
|
||
| if (before == list->head) { | ||
| free(newNode); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| Node* ahead = before->next; | ||
| before->next = newNode; | ||
| newNode->next = ahead; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool del(List* list, unsigned index) | ||
| { | ||
| if (list == NULL || list->head == NULL) { | ||
| return false; | ||
| } | ||
|
|
||
| Node* before = list->head; | ||
|
|
||
| if (index == 0) { | ||
| if (before->next == before) { | ||
| free(before); | ||
| list->head = NULL; | ||
| return true; | ||
| } | ||
|
|
||
| Node* current = before; | ||
|
|
||
| while (current->next != before) { | ||
| current = current->next; | ||
| } | ||
| current->next = before->next; | ||
| list->head = before->next; | ||
| free(before); | ||
| return true; | ||
| } | ||
|
|
||
| for (unsigned i = 1; i < index; i++) { | ||
| before = before->next; | ||
|
|
||
| if (before == list->head) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| Node* current = before->next; | ||
| if (current == NULL || current == list->head) { | ||
| return false; | ||
| } | ||
|
|
||
| before->next = current->next; | ||
| free(current); | ||
| return true; | ||
| } | ||
|
|
||
| int isEmpty(List* list) | ||
| { | ||
| if (list == NULL) { | ||
| return 2; | ||
| } | ||
|
|
||
| return list->head == NULL; | ||
| } | ||
|
|
||
| bool deleteList(List* list) | ||
| { | ||
| if (list == NULL) { | ||
| return true; | ||
| } | ||
|
|
||
| if (list->head != NULL) { | ||
| Node* current = list->head->next; | ||
| while (current != list->head) { | ||
| Node* next = current->next; | ||
| free(current); | ||
| current = next; | ||
| } | ||
| free(list->head); | ||
| } | ||
|
|
||
| free(list); | ||
| return true; | ||
| } | ||
|
|
||
| bool len(List* list, unsigned* res) | ||
| { | ||
| unsigned len = 0; | ||
|
|
||
| if (list == NULL || res == NULL) { | ||
| return false; | ||
| } | ||
|
|
||
| if (list->head == NULL) { | ||
| *res = 0; | ||
| return true; | ||
| } | ||
|
|
||
| Node* current = list->head; | ||
| len = 1; | ||
|
|
||
| while (current->next != list->head) { | ||
| len++; | ||
| current = current->next; | ||
| } | ||
|
|
||
| *res = len; | ||
| return true; | ||
| } | ||
|
|
||
| bool find(List* list, unsigned index, int* res) | ||
| { | ||
| if (list == NULL || list->head == NULL || res == NULL) { | ||
| return false; | ||
| } | ||
|
|
||
| Node* current = list->head; | ||
| for (unsigned i = 0; i < index; i++) { | ||
| current = current->next; | ||
|
|
||
| if (current == list->head) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| *res = current->data; | ||
| return true; | ||
| } |
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,14 @@ | ||
| #pragma once | ||
| #include <stdbool.h> | ||
|
|
||
| typedef struct Node Node; | ||
| typedef struct List List; | ||
|
|
||
| List* newList(); | ||
| Node* createNode(int data); | ||
| bool insert(List* list, int data, unsigned index); | ||
| bool del(List* list, unsigned index); | ||
| int isEmpty(List* list); | ||
| bool deleteList(List* list); | ||
| bool len(List* list, unsigned* res); | ||
| bool find(List* list, unsigned index, int* res); |
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,53 @@ | ||
| #include "circularList.h" | ||
| #include <stdio.h> | ||
|
|
||
| bool josephus(int n, int m, int* res) | ||
| { | ||
| if (n <= 0 || m <= 0) { | ||
| return false; | ||
| } | ||
|
|
||
| List* list = newList(); | ||
| int listLen = n; | ||
|
|
||
| for (int i = 1; i <= n; i++) { | ||
| if (!insert(list, i, i - 1)) { | ||
| deleteList(list); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| int currentIndex = (m - 1) % n; | ||
| while (listLen > 1) { | ||
| if (!del(list, currentIndex)) { | ||
| deleteList(list); | ||
| return false; | ||
| } | ||
| listLen--; | ||
| currentIndex = (currentIndex + m - 1) % listLen; | ||
| } | ||
|
|
||
| int last = -1; | ||
| if (!find(list, 0, &last)) { | ||
| deleteList(list); | ||
| return false; | ||
| } | ||
|
|
||
| deleteList(list); | ||
| *res = last; | ||
| return true; | ||
| } | ||
|
|
||
| int main(void) | ||
| { | ||
| int n = 0, m = 0; | ||
| scanf("%d%d", &n, &m); | ||
|
|
||
| int res = -1; | ||
|
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. зачем выставлять в |
||
| if (!josephus(n, m, &res)) { | ||
| return 1; | ||
| } | ||
|
|
||
| printf("%d\n", res); | ||
| return 0; | ||
| } | ||
Oops, something went wrong.
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.
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.
нет проверки (
!list)