Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d10292d
Writing all functions and tests
MinyazevR Nov 1, 2021
0b08d69
fixed memory bugs
MinyazevR Nov 2, 2021
00f3204
started changing functions
MinyazevR Nov 7, 2021
c2e4c84
adding features
MinyazevR Nov 7, 2021
39595a9
change
MinyazevR Nov 7, 2021
cf166c3
changing tests
MinyazevR Nov 7, 2021
898009c
fixed some memory leaks
MinyazevR Nov 9, 2021
ec14372
changing the project structure
MinyazevR Nov 10, 2021
70da8f8
merge sorting is implemented in a separate project
MinyazevR Nov 10, 2021
a92c736
fixed function for compare head
MinyazevR Nov 10, 2021
7d2d6e2
fixed function for compare list
MinyazevR Nov 10, 2021
2f1bae8
the system of working with errors has been changed
MinyazevR Nov 15, 2021
0efbf49
improved error handling
MinyazevR Nov 26, 2021
efe9deb
moving files
MinyazevR Nov 26, 2021
f3b2155
put a space
MinyazevR Nov 26, 2021
e7ffca8
changed error handling for some list function
MinyazevR Nov 27, 2021
0a7f160
added a function that determines whether an item is in the list
MinyazevR Nov 27, 2021
95cf48b
Revert "added a function that determines whether an item is in the list"
MinyazevR Nov 27, 2021
ff784e0
changed the function for deleting an element
MinyazevR Dec 5, 2021
152c6aa
changed the function for deleting an element
MinyazevR Dec 5, 2021
b2733c6
adding functions
MinyazevR Dec 5, 2021
6407802
changing tests
MinyazevR Dec 5, 2021
a9c8916
changing the function to remove an item from the list
MinyazevR Dec 8, 2021
8685dee
removed unnecessary functions
MinyazevR Dec 8, 2021
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 SinglyLinkedList/NewList/NewList.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}") = "NewList", "NewList\NewList.vcxproj", "{8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}"
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
{8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x64.ActiveCfg = Debug|x64
{8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x64.Build.0 = Debug|x64
{8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x86.ActiveCfg = Debug|Win32
{8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x86.Build.0 = Debug|Win32
{8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x64.ActiveCfg = Release|x64
{8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x64.Build.0 = Release|x64
{8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x86.ActiveCfg = Release|Win32
{8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3AA29672-FC57-4049-A441-2EC868F66725}
EndGlobalSection
EndGlobal
262 changes: 262 additions & 0 deletions SinglyLinkedList/NewList/NewList/List.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
#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* firstValue;
char* secondValue;
struct ListElement* next;
} ListElement;

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

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

void deleteList(List* list)
{
ListElement* position = list->head;
while (position != NULL)
{
list->head = list->head->next;
free(position->secondValue);
free(position->firstValue);
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->secondValue);
free(list->head->firstValue);
free(list->head);
list->head = NULL;
list->tail = NULL;
return;
}
ListElement* element = list->head;
list->head = list->head->next;
list->size--;
free(element->secondValue);
free(element->firstValue);
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* getHeadFirstValue(List* list)
{
if (list->head == NULL)
{
return NULL;
}
return list->head->firstValue;
}

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

char* getFirstValue(Position* position)
{
if (position->position == NULL)
{
return NULL;
}
return position->position->firstValue;
}

char* getSecondValue(Position* position)
{
if (position->position == NULL)
{
return NULL;
}
return position->position->secondValue;
}

void add(List* list, const char* firstValue, const char* secondValue, Error* error)
{
if (*error != NOT_ERROR)
{
return;
}
char* firstValueCopy = calloc(strlen(firstValue) + 1, sizeof(char));
if (firstValueCopy == NULL)
{
*error = INSUFFICIENT_MEMORY;
return;
}
strcpy(firstValueCopy, firstValue);
char* secondValueCopy = calloc(strlen(secondValue) + 1, sizeof(char));
if (secondValueCopy == NULL)
{
free(firstValueCopy);
*error = 3;
return;
}
strcpy(secondValueCopy, secondValue);
ListElement* newElement = calloc(1, sizeof(ListElement));
if (newElement == NULL)
{
free(firstValueCopy);
free(secondValueCopy);
*error = INSUFFICIENT_MEMORY;
return;
}
newElement->firstValue = firstValueCopy;
newElement->secondValue = secondValueCopy;
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;
}

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 ", element->firstValue);
printf("%s\n", element->secondValue);
element = element->next;
}
}

bool compareList(List* firstList, List* secondList)
{
ListElement* firstElement = firstList->head;
ListElement* secondElement = secondList->head;
if (numberOfElements(firstList) != numberOfElements(secondList))
{
return false;
}
while (firstElement != NULL)
{
if (strcmp(firstElement->firstValue, secondElement->firstValue) != 0 || strcmp(firstElement->secondValue, secondElement->secondValue) != 0)
{
return false;
}
firstElement = firstElement->next;
secondElement = secondElement->next;
}
return true;
}

const char* decodingError(Error error)
{
if (error == EMPTY_LIST)
{
return "Error in the program";
}
if (error == INSUFFICIENT_MEMORY)
{
return "Memory not allocated";
}
return NULL;
}
73 changes: 73 additions & 0 deletions SinglyLinkedList/NewList/NewList/List.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#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
} Error;

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

// 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* firstValue, const char* secondValue, 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 for checking the list for the content of a single element
bool isOneElement(List* list);

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

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

// Function for finding a position to the last element
Position* last(List* list, Error* error);

// Returns the value of any element
char* getFirstValue(Position* position);

// Returns the value of any element
char* getSecondValue(Position* position);

// Function for comparing lists
bool compareList(List* firstList, List* secondList);

// Function for error decoding
const char* decodingError(Error error);
11 changes: 11 additions & 0 deletions SinglyLinkedList/NewList/NewList/Main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "TestList.h"
#include <stdio.h>

int main()
{
if (!allTest())
{
printf("Test failed");
return -1;
}
}
Loading