Created
October 19, 2021 10:11
-
-
Save yurii-litvinov/5ffb6d993ddfcbb007f4b19413727da6 to your computer and use it in GitHub Desktop.
Buggy implementation of a linked list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "List.h" | |
#include <stdlib.h> | |
typedef struct ListElement | |
{ | |
int value; | |
struct ListElement* next; | |
} ListElement; | |
typedef struct List | |
{ | |
ListElement* head; | |
} List; | |
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); | |
position = list->head; | |
} | |
free(list); | |
} | |
void deletePosition(Position* position) | |
{ | |
free(position); | |
} | |
void add(List* list, Position* position, int value) | |
{ | |
ListElement* newElement = calloc(1, sizeof(ListElement)); | |
newElement->value = value; | |
if (position->position == NULL) | |
{ | |
list->head = newElement; | |
return; | |
} | |
newElement->next = position->position->next; | |
position->position->next = newElement; | |
} | |
Position* first(List* list) | |
{ | |
Position* positionFirst = malloc(sizeof(Position)); | |
positionFirst->position = list->head; | |
return positionFirst; | |
} | |
Position* next(Position* position) | |
{ | |
Position* newPosition = malloc(sizeof(Position)); | |
newPosition->position = position->position->next; | |
return newPosition; | |
} | |
bool last(Position* position) | |
{ | |
return position->position == NULL; | |
} | |
int get(List* list, Position* position) | |
{ | |
return position->position->value; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <stdbool.h> | |
typedef struct List List; | |
typedef struct Position Position; | |
List* createList(); | |
void deleteList(List* list); | |
void deletePosition(Position* position); | |
void add(List* list, Position* position, int value); | |
Position* first(List* list); | |
Position* next(Position* position); | |
bool last(Position* position); | |
int get(List* list, Position* position); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "List.h" | |
int main() | |
{ | |
List* myList = createList(); | |
add(myList, first(myList), 1); | |
add(myList, first(myList), 2); | |
for (Position* i = first(myList); !last(i); i = next(i)) | |
{ | |
printf("%d ", get(myList, i)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment