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
91 changes: 70 additions & 21 deletions List/List/List.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
typedef struct List
{
struct ListElement* head;
int size;
} List;

// A structure containing a pointer� to the next and previous list item and a value variable for the list items
Expand Down Expand Up @@ -47,6 +48,7 @@ void removeElement(Position* position, List* list)
{
return;
}
list->size--;
if (position->position == list->head)
{
list->head = list->head->next;
Expand All @@ -67,15 +69,39 @@ void removeElement(Position* position, List* list)
free(position);
}

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

Position* last(List* list, Error* error)
{
if (*error != NOT_ERROR)
{
return NULL;
}
Position* position = malloc(sizeof(Position));
if (position == NULL)
{
*error = INSUFFICIENT_MEMORY;
return NULL;
}
position->position = list->head;
while (position->position->next != NULL)
{
position = next(position);
}
return position;
}

Expand All @@ -91,51 +117,57 @@ Position* previous(Position* position)
return position;
}

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

int get(List* list, Position* position, int* error)
int get(List* list, Position* position, Error* error)
{
*error = 0;
if (*error != NOT_ERROR)
{
return 0;
}
if (position->position == NULL)
{
*error = 6;
*error = ELEMENT_IS_MISSING;
return 0;
}
return position->position->value;
}

Position* find(int value, List* list, int* error)
Position* find(int value, List* list, Error* error)
{
*error = 0;
if (*error != NOT_ERROR)
{
return NULL;
}
if (list->head == NULL)
{
*error = 6;
*error = EMPTY_LIST;
return NULL;
}
if (value < list->head->value)
{
*error = 5;
*error = ELEMENT_IS_MISSING;
return NULL;
}
Position* position = first(list, error);
if (*error == 3)
if (*error == INSUFFICIENT_MEMORY)
{
return NULL;
}
while (position->position->value <= value && position->position->next != NULL)
{
if (position->position->value == value)
{
*error = 8;
*error = POSITION_FOUND;
}
next(position);
}
if (position->position->value == value)
{
*error = 8;
*error = POSITION_FOUND;
}
if (position->position == list->head || position->position->next == NULL && position->position->value <= value)
{
Expand All @@ -145,40 +177,52 @@ Position* find(int value, List* list, int* error)
return position;
}

Position* findPosition(int value, List* list, int* error)
Position* findPosition(int value, List* list, Error* error)
{
*error = 0;
if (*error != NOT_ERROR)
{
return NULL;
}
Position* position = find(value, list, error);
if (*error == 8)
if (*error == POSITION_FOUND)
{
*error = NOT_ERROR;
return position;
}
*error = 6;
*error = ELEMENT_IS_MISSING;
return NULL;
}

void add(List* list, int value, int* error)
void add(List* list, int value, Error* error)
{
if (*error != NOT_ERROR)
{
return;
}
ListElement* element = malloc(sizeof(ListElement));
if (element == NULL)
{
*error = 3;
*error = INSUFFICIENT_MEMORY;
return;
}
element->value = value;
Position* position = find(value, list, error);
if (*error == 6)
if (*error == EMPTY_LIST)
{
list->head = element;
element->next = NULL;
*error = NOT_ERROR;
list->size = 1;
free(position);
return;
}
if (*error == 5)
list->size++;
if (*error == ELEMENT_IS_MISSING)
{
element->next = list->head;
list->head->previous = element;
list->head = element;
*error = NOT_ERROR;
free(position);
return;
}
Expand Down Expand Up @@ -207,6 +251,11 @@ void print(List* list)
}
}

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

void freePosition(Position* position)
{
free(position);
Expand Down
24 changes: 19 additions & 5 deletions List/List/List.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,28 @@ typedef struct List List;
// Structure describing the position of an item in the list
typedef struct Position Position;

typedef enum Error
{
NOT_ERROR,
EMPTY_LIST,
INSUFFICIENT_MEMORY,
ELEMENT_IS_MISSING,
POSITION_FOUND
} Error;

Choose a reason for hiding this comment

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

👍

// 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);
void add(List* list, int value, Error* error);

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

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

// Function to remove an item from the list
void removeElement(Position* position, List* list);
Expand All @@ -25,10 +36,10 @@ void removeElement(Position* position, List* list);
Position* next(Position* position);

// Function for finding a pointer to an element
Position* findPosition(int value, List* list, int* error);
Position* findPosition(int value, List* list, Error* error);

// Function for finding an element
int get(List* list, Position* position, int* error);
int get(List* list, Position* position, Error* error);

// Function for printing a list
void print(List* list);
Expand All @@ -37,4 +48,7 @@ void print(List* list);
Position* previous(Position* position);

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

// Function to return the size of the list
int returnSize(List* list);
Loading