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
5 changes: 0 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ dkms.conf

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Local History for Visual Studio Code
.history/
Expand Down
17 changes: 17 additions & 0 deletions 06_lists/src/CMakeLists.txt
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)
204 changes: 204 additions & 0 deletions 06_lists/src/circularList.c
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;
}
14 changes: 14 additions & 0 deletions 06_lists/src/circularList.h
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);
53 changes: 53 additions & 0 deletions 06_lists/src/josephusProblem.c
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();

Choose a reason for hiding this comment

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

нет проверки (!list)

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;

Choose a reason for hiding this comment

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

зачем выставлять в -1, если josephus()=true перезапишет res, а josephus()=false не дойдет до printf где используется res?

if (!josephus(n, m, &res)) {
return 1;
}

printf("%d\n", res);
return 0;
}
Loading