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
31 changes: 31 additions & 0 deletions CyclicList/CyclicList.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31410.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CyclicList", "CyclicList\CyclicList.vcxproj", "{3E224E7A-155B-41F9-8988-80A502835B6E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3E224E7A-155B-41F9-8988-80A502835B6E}.Debug|x64.ActiveCfg = Debug|x64
{3E224E7A-155B-41F9-8988-80A502835B6E}.Debug|x64.Build.0 = Debug|x64
{3E224E7A-155B-41F9-8988-80A502835B6E}.Debug|x86.ActiveCfg = Debug|Win32
{3E224E7A-155B-41F9-8988-80A502835B6E}.Debug|x86.Build.0 = Debug|Win32
{3E224E7A-155B-41F9-8988-80A502835B6E}.Release|x64.ActiveCfg = Release|x64
{3E224E7A-155B-41F9-8988-80A502835B6E}.Release|x64.Build.0 = Release|x64
{3E224E7A-155B-41F9-8988-80A502835B6E}.Release|x86.ActiveCfg = Release|Win32
{3E224E7A-155B-41F9-8988-80A502835B6E}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {11CCDD59-DDF5-48B1-85A1-8BE50AF8E546}
EndGlobalSection
EndGlobal
55 changes: 55 additions & 0 deletions CyclicList/CyclicList/CircleOfMurders.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "CircleOfMurders.h"
#include "CyclicList.h"

int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* error)
{
if (numberOfWarriors == 0)
{
*error = 2;
return 0;
}
if (frequencyOfMurders == 0)
{
*error = 2;
return 0;
}
List* newList = createList();
for (int i = 1; i <= numberOfWarriors; i++)
{
add(newList, i, error);
if (*error == 3)
{
deleteList(newList);
return 0;
}
}
Position* firstPosition = first(newList, error);
if (*error == 3)
{
deleteList(newList);
return 0;
}
int counter = 1;
while (numberOfElements(newList) != 1)
{
if (counter % frequencyOfMurders == 0)
{
removeElement(newList, firstPosition, error);
if (*error == 1)
{
freePosition(firstPosition);
deleteList(newList);
return 0;
}
}
else
{
next(firstPosition);
}
counter++;
}
const int answer = returnFirstElementValue(newList);
freePosition(firstPosition);
deleteList(newList);
return answer;
}
4 changes: 4 additions & 0 deletions CyclicList/CyclicList/CircleOfMurders.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

// Function for finding the position of the surviving warrior
int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* error);
167 changes: 167 additions & 0 deletions CyclicList/CyclicList/CyclicList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
#include "CyclicList.h"
#include <stdio.h>
#include <stdlib.h>

// Structure containing pointers to the "first" and "last" list item
typedef struct List
{
int size;
struct ListElement* head;
struct ListElement* tail;
} List;

// Structure containing pointers to the next and previous list item and a value variable for list items
typedef struct ListElement
{
int value;
struct ListElement* next;
struct ListElement* previous;
} ListElement;

// A structure that stores a pointer to a list item
typedef struct Position
{
ListElement* position;
} Position;

List* createList()
{
return calloc(1, sizeof(List));
}

void deleteList(List* list)
{
ListElement* position = list->head;
while (list->head != list->tail)
{
list->head = list->head->next;
free(position);
position = list->head;
}

This comment was marked as resolved.

list->size = 0;

Choose a reason for hiding this comment

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

Это не нужно. Мы двумя строками ниже удаляем весь list. Всё равно как покрасить дом перед тем, как его снести.

free(position);
free(list);
}

Position* first(List* list, int* error)
{
*error = 0;
Position* positionFirst = malloc(sizeof(Position));
if (positionFirst == NULL)
{
*error = 3;
return NULL;
}
positionFirst->position = list->head;
return positionFirst;
}

Position* next(Position* position)
{
position->position = position->position->next;
return position;
}

Position* previous(Position* position)
{
position->position = position->position->previous;
return position;
}

void attach(ListElement* element)
{
element->next->previous = element;
element->previous->next = element;
}

void add(List* list, int value, int* error)
{
ListElement* newElement = calloc(1, sizeof(ListElement));
if (newElement == NULL)
{
*error = 3;
return;
}
newElement->value = value;
if (list->head == NULL)
{
list->size = 1;
list->head = newElement;
list->tail = newElement;
list->tail->next = list->head;
list->head->previous = list->tail;
return;
}
list->size++;
newElement->next = list->head;

This comment was marked as resolved.

newElement->previous = list->tail;
attach(newElement);
list->tail = list->tail->next;
}

int returnFirstElementValue(List* list)
{
return list->head->value;
}

void removeElement(List* list, Position* position, int* error)
{
if (list->head == NULL || position == NULL)
{
*error = 1;
return;
}
Position* newPosition = position;
if (position->position == list->head)
{
if (list->tail == list->head)
{
free(list->head);
free(position);
list->tail = NULL;
list->head = NULL;
list->size = 0;
return;
}
ListElement* element = list->head;
list->head = list->head->next;
list->head->previous = list->tail;
attach(list->head);
position->position = list->head;
free(element);
list->size--;
return;
}
if (position->position == list->tail)
{
ListElement* element = list->tail;
list->tail = list->tail->previous;
list->tail->next = list->head;
attach(list->tail);

Choose a reason for hiding this comment

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

Кажется, можно было бы вынести более общую функцию, типа "удалить между", и использовать её тут в трёх местах.

position->position = list->head;
free(element);
list->size--;
return;
}
ListElement* element = position->position;
position->position = position->position->next;
position->position->previous = position->position->previous->previous;
free(element);
list->size--;
attach(position->position);
}

This comment was marked as resolved.


int get(Position* position)
{
return position->position->value;
}

int numberOfElements(List* list)
{
return list->size;
}

void freePosition(Position* position)
{
free(position);
}

This comment was marked as resolved.

41 changes: 41 additions & 0 deletions CyclicList/CyclicList/CyclicList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

// Structure that represents cyclic list
typedef struct List List;

// Structure for implementing a cyclic list

Choose a reason for hiding this comment

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

Неправда :) Откомментил в предыдущей задаче аналогичную проблему

typedef struct Position Position;

// Function for creating a list
List* createList();

// Function for deleting a list
void deleteList(List* list);

// Function for adding an item to a list
void add(List* list, int value, int* error);

// Function for finding a pointer to the first element
Position* first(List* list, int* error);

// Function to remove an item from the list
void removeElement(List* list, Position* position, int* error);

// Function for finding a pointer to the next element
Position* next(Position* position);

// Function for finding a pointer to the previous element
Position* previous(Position* position);

// Function that returns the value of the first element
int returnFirstElementValue(List* list);

// Function that returns the value of an element
int get(Position* position);

// Function for finding the number of items in the list
int numberOfElements(List* list);

// Function for freeing up memory
void freePosition(Position* position);

Loading