Added list structure.

Added a generic list structure. Some functions still need to be added.
This commit is contained in:
Andrew Lalis 2017-05-23 18:02:10 +02:00
parent 167b450ce3
commit f8cc4c0a2b
2 changed files with 123 additions and 0 deletions

109
source/handylist.h Normal file
View File

@ -0,0 +1,109 @@
/*
handystack.h
Created By:
Andrew Lalis (andrewlalisofficial@gmail.com)
23 May 2017
This file is free to use, as long as the above comment remains in the file.
Please contact the author regarding bugs and/or feature requests.
*/
#ifdef LIST_TYPE
#include "templates.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
/*
Linked List:
The linked list is a list of items of arbitrary value that are linked via a pointer to the next element, and the last element has a null pointer. The list does not try to maintain any particular order.
*/
#define LIST_NODE TEMPLATE(LIST_TYPE,list)
/*
List structure:
Each list node will contain a piece of data, which is of an arbitrary type, and a pointer to the next element.
*/
typedef struct LIST_NODE LIST_NODE;
struct LIST_NODE {
LIST_TYPE data;
LIST_NODE* next;
};
/*
Size of List:
returns size of list.
*/
int TEMPLATE(size,LIST_NODE)(LIST_NODE* list){
int size = 0;
while (list != NULL){
size++;
list = list->next;
}
return size;
}
/*
New list:
allocates memory for a new list item, and returns it.
*/
LIST_NODE* TEMPLATE(new,LIST_NODE)(LIST_TYPE data, LIST_NODE* next){
LIST_NODE *n = malloc(sizeof(LIST_NODE));
assert(n != NULL);
n->data = data;
n->next = next;
return n;
}
/*
Free List:
de-allocates memory for a list, and all next items recursively.
*/
void TEMPLATE(free,LIST_NODE)(LIST_NODE* list){
if (list->next != NULL){
TEMPLATE(free,LIST_NODE)(list->next);
}
free(list);
list = NULL;
}
/*
Add to the list:
Appends an item to the end of the list.
*/
void TEMPLATE(add,LIST_NODE)(LIST_TYPE data, LIST_NODE* list){
while (list->next != NULL){
list = list->next;
}
list->next = TEMPLATE(new,LIST_NODE)(data, NULL);
}
/*
Insert List;
Inserts an item at the given index, so that the item can be retreived with the get function at that index.
*/
/*
Get list item:
Returns the item in a specific index of the list, or fails.
*/
LIST_TYPE TEMPLATE(get,LIST_NODE)(int index, LIST_NODE* list){
int i = 0;
while (i < index){
if (list->next != NULL){
list = list->next;
} else {
printf("Index specified does not exist in list.\n");
exit(-1);
}
i++;
}
return list->data;
}
#endif

14
test.c
View File

@ -7,6 +7,20 @@ int main(int argc, char* argv[]){
#define STACK_TYPE double #define STACK_TYPE double
#include "source/handystack.h" #include "source/handystack.h"
double_stack s = new_double_stack(); double_stack s = new_double_stack();
push_double_stack(42.5, &s);
print_double_stack(s); print_double_stack(s);
#ifdef LIST_TYPE
#undef LIST_TYPE
#endif
#define LIST_TYPE float
#include "source/handylist.h"
float_list* l = new_float_list(5.0, NULL);
add_float_list(6.0, l);
add_float_list(7.0, l);
add_float_list(8.0, l);
printf("%d\n", size_float_list(l));
printf("%f\n", get_float_list(3, l));
free_float_list(l);
} }