Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/schitalochka/compilation.md
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
66 changes: 66 additions & 0 deletions src/schitalochka/cyclic_list.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);
}
Comment on lines +40 to +43
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Оптимистично. А вдруг инициализировали и решили удалить? Давайте всё-таки честно попробуем удалить элементы.


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);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
}
19 changes: 19 additions & 0 deletions src/schitalochka/cyclic_list.h
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Наверное, чуть красивее было бы сделать так:

Suggested change
int deleteElement(List* list, int index);
bool deleteElement(List* list, int index, int* out);

Если вернулось true, то удалили последний элемент и записали в out, в ином случае out не трогать.

И в целом, по большому счёту, 0 допустим только потому что Вы воинов нумеруете от 1, а не от 0.

17 changes: 17 additions & 0 deletions src/schitalochka/main.c
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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): ");
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)) { }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while?

printf("Number %d is alive! \n", humanNumber);

deleteList(people);
return 0;
}