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 Control2/List/List.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}") = "List", "List\List.vcxproj", "{721388A3-2CFA-44FE-915A-0A846DB709F4}"
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
{721388A3-2CFA-44FE-915A-0A846DB709F4}.Debug|x64.ActiveCfg = Debug|x64
{721388A3-2CFA-44FE-915A-0A846DB709F4}.Debug|x64.Build.0 = Debug|x64
{721388A3-2CFA-44FE-915A-0A846DB709F4}.Debug|x86.ActiveCfg = Debug|Win32
{721388A3-2CFA-44FE-915A-0A846DB709F4}.Debug|x86.Build.0 = Debug|Win32
{721388A3-2CFA-44FE-915A-0A846DB709F4}.Release|x64.ActiveCfg = Release|x64
{721388A3-2CFA-44FE-915A-0A846DB709F4}.Release|x64.Build.0 = Release|x64
{721388A3-2CFA-44FE-915A-0A846DB709F4}.Release|x86.ActiveCfg = Release|Win32
{721388A3-2CFA-44FE-915A-0A846DB709F4}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E12AB49D-6742-4F31-87F2-2C61C86207CE}
EndGlobalSection
EndGlobal
225 changes: 225 additions & 0 deletions Control2/List/List/List.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
#define _CRT_SECURE_NO_WARNINGS
#include "List.h"
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>

// Structure containing pointers to the beginning and end of the list
typedef struct List
{
int size;
struct ListElement* head;
struct ListElement* tail;
} List;

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

// Structure containing a pointer to a ListElement
typedef struct Position
{
ListElement* position;
} Position;

List* createList(Error* error)
{
List* list = calloc(1, sizeof(List));
if (list == NULL)
{
*error = INSUFFICIENT_MEMORY;
}
return list;
}

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

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

void removeFirstElement(List* list, Error* error)
{
if (*error != NOT_ERROR)
{
return;
}
if (list->head == NULL)
{
*error = EMPTY_LIST;
return;
}
if (list->head == list->tail)
{
list->size = 0;
free(list->head->value);
free(list->head);
list->head = NULL;
list->tail = NULL;
return;
}
ListElement* element = list->head;
list->head = list->head->next;
list->size--;
free(element->value);
free(element);

}
Comment on lines +81 to +83

Choose a reason for hiding this comment

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

Suggested change
free(element);
}
free(element);
}


Position* first(List* list, Error* error)
{
if (*error != NOT_ERROR)
{
return NULL;
}
Position* positionFirst = malloc(sizeof(Position));
if (positionFirst == NULL)
{
*error = EMPTY_LIST;
return NULL;
}
positionFirst->position = list->head;
return positionFirst;
}

Position* last(List* list, Error* error)
{
if (*error != NOT_ERROR)
{
return NULL;
}
Position* positionLast = malloc(sizeof(Position));
if (positionLast == NULL)
{
*error = EMPTY_LIST;
return NULL;
}
positionLast->position = list->tail;
return positionLast;
}

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

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

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

char* getHeadValue(List* list)
{
if (list->head == NULL)
{
return NULL;
}
return list->head->value;
}

void add(List* list, const char* value, Error* error)
{
if (*error != NOT_ERROR)
{
return;
}
char* valueCopy = calloc(strlen(value) + 1, sizeof(char));
if (valueCopy == NULL)
{
*error = INSUFFICIENT_MEMORY;
return;
}
strcpy(valueCopy, value);
ListElement* newElement = calloc(1, sizeof(ListElement));
if (newElement == NULL)
{
free(valueCopy);
*error = INSUFFICIENT_MEMORY;
return;
}
newElement->value = valueCopy;
if (list->head == NULL)
{
list->size = 1;
list->head = newElement;
list->tail = newElement;
return;
}
list->size++;
list->tail->next = newElement;
list->tail = list->tail->next;
}

char* getValue(Position* position)
{
return position->position->value;
}

bool isEmpty(List* list)
{
return list->head == NULL;
}

bool isOneElement(List* list)
{
return list->head->next == NULL;
}

void print(List* list)
{
ListElement* element = list->head;
while (element != NULL)
{
printf("%s\n", element->value);
element = element->next;
}
}

const char* decodingError(Error error)
{
if (error == EMPTY_LIST)
{
return "Error in the program";
}
if (error == INSUFFICIENT_MEMORY)
{
return "Memory not allocated";
}
return NULL;
}

bool inList(List* list, const char* value)
{
ListElement* element = list->head;
while (element != NULL)
{
if (strcmp(value, element->value) == 0)
{
return true;
}
element = element->next;
}
return false;
}
62 changes: 62 additions & 0 deletions Control2/List/List/List.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once
#include <stdbool.h>

// Structure that represents list
typedef struct List List;

// This is a structure describing the position of an item in the list.
typedef struct Position Position;

// Enum type for working with errors
typedef enum Error
{
NOT_ERROR,
EMPTY_LIST,
INSUFFICIENT_MEMORY,
ELEMENT_REPEATS
} Error;

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

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

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

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

// function to find a position to the first element
Position* first(List* list, Error* error);

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

// Function for checking an item for being at the end of the list
bool isLastElement(Position* position);

// Function for printing a list
void print(List* list);

// Function for deleting the first element
void removeFirstElement(List* list, Error* error);

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

// Function for checking the emptiness of the list
bool isEmpty(List* list);

// Function that returns a value for the first element
char* getHeadValue(List* list);

// Function that returns a value of element
char* getValue(Position* position);

// Function for error decoding
const char* decodingError(Error error);

// Function for determining the location of an item in the list
bool inList(List* list, const char* value);
Loading