-
Notifications
You must be signed in to change notification settings - Fork 0
ДЗ № 6.2 Считалочка, Шестаков Николай #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
base: main
Are you sure you want to change the base?
Changes from all commits
46f0859
93f7ae4
940a4bc
dadea8f
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,3 @@ | ||
| ## Сборка: | ||
| gcc -Wall -Wextra -pedantic -O2 -c cyclic_list.c\ | ||
| gcc -Wall -Wextra -pedantic -O2 cyclic_list.o main.c |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| #include "cyclic_list.h" | ||
|
|
||
| #include <stdlib.h> | ||
|
|
||
| typedef struct ListElement { | ||
| int value; | ||
| struct ListElement* next; | ||
| struct ListElement* prev; | ||
| } ListElement; | ||
|
|
||
| struct List { | ||
| ListElement* zeroIndexElement; | ||
| int size; | ||
| }; | ||
|
|
||
| List* createList(int n) | ||
| { | ||
| if (n <= 0) { | ||
| return NULL; | ||
| } | ||
|
|
||
| ListElement* oldElement = malloc(sizeof(*oldElement)); | ||
| oldElement->value = 1; | ||
| List* list = malloc(sizeof(*list)); | ||
| list->zeroIndexElement = oldElement; | ||
| list->size = n; | ||
| for (int i = 2; i <= n; ++i) { | ||
| ListElement* newElement = malloc(sizeof(*newElement)); | ||
| newElement->value = i; | ||
| newElement->prev = oldElement; | ||
| oldElement->next = newElement; | ||
| oldElement = newElement; | ||
| } | ||
| oldElement->next = list->zeroIndexElement; | ||
| list->zeroIndexElement->prev = oldElement; | ||
|
|
||
| return list; | ||
| } | ||
|
|
||
| void deleteList(List* list) | ||
| { | ||
| free(list); | ||
| } | ||
|
|
||
| int deleteElement(List* list, int index) | ||
| { | ||
| if (list->zeroIndexElement->next == list->zeroIndexElement) { | ||
| int value = list->zeroIndexElement->value; | ||
| free(list->zeroIndexElement); | ||
| return value; | ||
| } | ||
|
|
||
| index = index < 0 ? (index % list->size + list->size) : (index % list->size); | ||
|
Collaborator
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. Прикольно, но вообще, наверное, логично сделать индексы только положительными |
||
|
|
||
| ListElement* currentElement = list->zeroIndexElement; | ||
| for (int i = 0; i < index; ++i) { | ||
| currentElement = currentElement->next; | ||
| } | ||
| currentElement->prev->next = currentElement->next; | ||
| currentElement->next->prev = currentElement->prev; | ||
| list->zeroIndexElement = currentElement->next; | ||
| list->size--; | ||
| free(currentElement); | ||
|
|
||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,19 @@ | ||||||
| #pragma once | ||||||
|
|
||||||
| #include <stdbool.h> | ||||||
|
|
||||||
| typedef struct List List; | ||||||
|
|
||||||
| // Creates cyclic list with n elements numerated from 1 to n. | ||||||
| // If n <= 0 returns NULL, else returns pointer on the list. | ||||||
| // Takes n - count of elements. | ||||||
| List* createList(int n); | ||||||
|
|
||||||
| // Deletes the list. The list is assumed to be empty. | ||||||
| // Takes pointer on the list; | ||||||
| void deleteList(List* list); | ||||||
|
|
||||||
| // Deletes element with taken index. Changes index starting pointer on the next element after deleted. | ||||||
| // Takes pointer on the list and index of the element. | ||||||
| // Returns 0. If the element is the last returns element. | ||||||
| int deleteElement(List* list, int index); | ||||||
|
Collaborator
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. Наверное, чуть красивее было бы сделать так:
Suggested change
Если вернулось И в целом, по большому счёту, 0 допустим только потому что Вы воинов нумеруете от 1, а не от 0. |
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #include "cyclic_list.h" | ||
| #include <stdio.h> | ||
|
|
||
| int main() | ||
| { | ||
| int peopleCount, everyM; | ||
|
Collaborator
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. А инициализировать?.. |
||
| printf("Enter people count and after how many people we kill human (Каждый k+1-ый будет убит) (separated by space): "); | ||
|
Collaborator
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. Забавная смесь :) |
||
| scanf("%d %d", &peopleCount, &everyM); | ||
|
|
||
| List* people = createList(peopleCount); | ||
| int humanNumber = 0; | ||
| for (; !humanNumber; humanNumber = deleteElement(people, everyM)) { } | ||
|
Collaborator
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.
|
||
| printf("Number %d is alive! \n", humanNumber); | ||
|
|
||
| deleteList(people); | ||
| return 0; | ||
| } | ||
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.
Оптимистично. А вдруг инициализировали и решили удалить? Давайте всё-таки честно попробуем удалить элементы.