Merge pull request #1 from andrewlalis/andrew
Finalized linked list functions and readme
This commit is contained in:
commit
69daec541c
46
README.md
46
README.md
|
@ -26,11 +26,7 @@ int is_empty(Stack s);
|
||||||
```
|
```
|
||||||
|
|
||||||
### Description
|
### Description
|
||||||
The stack is probably the most basic storage structure, using the 'first-in,
|
The stack is probably the most basic storage structure, using the 'first-in, first-out' approach. To define a stack, the user must first know what type the stack is, either a primitive type, or a **struct**. To define a stack, you must first define the **STACK_TYPE** to *int*, *float*, *char*, or whatever type you wish. Then, include the *handystack* header file.
|
||||||
first-out' approach. To define a stack, the user must first know what type the stack
|
|
||||||
is, either a primitive type, or a **struct**. To define a stack, you must first define
|
|
||||||
the **STACK_TYPE** to *int*, *float*, *char*, or whatever type you wish. Then, include
|
|
||||||
the *handystack* header file.
|
|
||||||
```c
|
```c
|
||||||
#define STACK_TYPE int
|
#define STACK_TYPE int
|
||||||
#include "handystack.h"
|
#include "handystack.h"
|
||||||
|
@ -60,6 +56,46 @@ Where `FUNCTION` is the name of the function.
|
||||||
|
|
||||||
## Linked List
|
## Linked List
|
||||||
|
|
||||||
|
### Functions
|
||||||
|
```c
|
||||||
|
//Creates a new list.
|
||||||
|
list* new(data, next);
|
||||||
|
//Recursively frees a list.
|
||||||
|
void free(List *l);
|
||||||
|
//Returns the number of elements in a list.
|
||||||
|
int size(List *l);
|
||||||
|
//Returns a new list with an item added to the front.
|
||||||
|
List* add(data, List *l);
|
||||||
|
//Returns a new list with an item inserted at an index.
|
||||||
|
List* insert(data, index, List *l);
|
||||||
|
//Returns the item at an index.
|
||||||
|
data get(index, List *l);
|
||||||
|
//Returns a new list with an item at an index removed.
|
||||||
|
List* remove(index, List *l);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Description
|
||||||
|
The linked list is a list where each *node* is comprised of a piece of data and a pointer to the next node. The list can have items added, inserted, and removed. To define a list, first define the type of data the list will contain, and then include the *handylist* header file.
|
||||||
|
```c
|
||||||
|
#define LIST_TYPE float
|
||||||
|
#include "handylist.h"
|
||||||
|
```
|
||||||
|
To define multiple lists, you must first undefine **LIST_TYPE** like so.
|
||||||
|
` #undef LIST_TYPE `
|
||||||
|
Then you simply repeat the instructions above for whatever new list type you would like to make. Just be sure to not define two list types for the same type.
|
||||||
|
|
||||||
|
### Example
|
||||||
|
The following example creates a list of **char** and adds a few to the list, and prints the number added.
|
||||||
|
```c
|
||||||
|
#define LIST_TYPE char
|
||||||
|
#include "handylist.h"
|
||||||
|
char_list* myList = new_char_list('a', NULL);
|
||||||
|
myList = add_char_list('b', myList);
|
||||||
|
myList = add_char_list('c', myList);
|
||||||
|
myList = insert_char_list('g', 1, myList);
|
||||||
|
printf("List size: %d\n", size_char_list(myList));
|
||||||
|
```
|
||||||
|
|
||||||
[Back to Top](#table-of-contents)
|
[Back to Top](#table-of-contents)
|
||||||
|
|
||||||
## Heap (Priority Queue)
|
## Heap (Priority Queue)
|
||||||
|
|
|
@ -20,10 +20,13 @@ 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.
|
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.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
LIST_NODE: node for a list.
|
||||||
|
*/
|
||||||
#define LIST_NODE TEMPLATE(LIST_TYPE,list)
|
#define LIST_NODE TEMPLATE(LIST_TYPE,list)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
List structure:
|
List structures:
|
||||||
Each list node will contain a piece of data, which is of an arbitrary type, and a pointer to the next element.
|
Each list node will contain a piece of data, which is of an arbitrary type, and a pointer to the next element.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -48,7 +51,7 @@ int TEMPLATE(size,LIST_NODE)(LIST_NODE* list){
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
New list:
|
New list node:
|
||||||
allocates memory for a new list item, and returns it.
|
allocates memory for a new list item, and returns it.
|
||||||
*/
|
*/
|
||||||
LIST_NODE* TEMPLATE(new,LIST_NODE)(LIST_TYPE data, LIST_NODE* next){
|
LIST_NODE* TEMPLATE(new,LIST_NODE)(LIST_TYPE data, LIST_NODE* next){
|
||||||
|
@ -60,7 +63,7 @@ LIST_NODE* TEMPLATE(new,LIST_NODE)(LIST_TYPE data, LIST_NODE* next){
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Free List:
|
Free List node:
|
||||||
de-allocates memory for a list, and all next items recursively.
|
de-allocates memory for a list, and all next items recursively.
|
||||||
*/
|
*/
|
||||||
void TEMPLATE(free,LIST_NODE)(LIST_NODE* list){
|
void TEMPLATE(free,LIST_NODE)(LIST_NODE* list){
|
||||||
|
@ -68,26 +71,31 @@ void TEMPLATE(free,LIST_NODE)(LIST_NODE* list){
|
||||||
TEMPLATE(free,LIST_NODE)(list->next);
|
TEMPLATE(free,LIST_NODE)(list->next);
|
||||||
}
|
}
|
||||||
free(list);
|
free(list);
|
||||||
list = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Add to the list:
|
Add to the list:
|
||||||
Appends an item to the end of the list.
|
Appends an item to the front of the list.
|
||||||
*/
|
*/
|
||||||
void TEMPLATE(add,LIST_NODE)(LIST_TYPE data, LIST_NODE* list){
|
LIST_NODE* TEMPLATE(add,LIST_NODE)(LIST_TYPE data, LIST_NODE* list){
|
||||||
while (list->next != NULL){
|
LIST_NODE* newNode = TEMPLATE(new,LIST_NODE)(data,list);
|
||||||
list = list->next;
|
return newNode;
|
||||||
}
|
|
||||||
list->next = TEMPLATE(new,LIST_NODE)(data, NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Insert List;
|
Insert List;
|
||||||
Inserts an item at the given index, so that the item can be retreived with the get function at that index.
|
Inserts an item at the given index, so that the item can be retreived with the get function at that index.
|
||||||
*/
|
*/
|
||||||
void TEMPLATE(insert,LIST_NODE)(LIST_TYPE data, int index, LIST_NODE* list){
|
LIST_NODE* TEMPLATE(insert,LIST_NODE)(LIST_TYPE data, int index, LIST_NODE* list){
|
||||||
|
if (index == 0){
|
||||||
|
return TEMPLATE(add,LIST_NODE)(data, list);
|
||||||
|
}
|
||||||
|
if (list == NULL){
|
||||||
|
printf("List is null.\n");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
list->next = TEMPLATE(insert,LIST_NODE)(data, index - 1, list->next);
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -108,4 +116,22 @@ LIST_TYPE TEMPLATE(get,LIST_NODE)(int index, LIST_NODE* list){
|
||||||
return list->data;
|
return list->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Remove from list:
|
||||||
|
Removes an item at an index from the list.
|
||||||
|
*/
|
||||||
|
LIST_NODE* TEMPLATE(remove,LIST_NODE)(int index, LIST_NODE* list){
|
||||||
|
if (list == NULL){
|
||||||
|
printf("List is null.\n");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
if (index == 0){
|
||||||
|
LIST_NODE* newList = list->next;
|
||||||
|
free(list);
|
||||||
|
return newList;
|
||||||
|
}
|
||||||
|
list->next = TEMPLATE(remove,LIST_NODE)(index-1, list->next);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
41
test.c
41
test.c
|
@ -2,27 +2,40 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
int main(int argc, char* argv[]){
|
|
||||||
|
|
||||||
#define STACK_TYPE double
|
#define STACK_TYPE double
|
||||||
#include "source/handystack.h"
|
#include "source/handystack.h"
|
||||||
|
|
||||||
|
#define LIST_TYPE float
|
||||||
|
#include "source/handylist.h"
|
||||||
|
|
||||||
|
void printList(float_list* list){
|
||||||
|
int size = size_float_list(list);
|
||||||
|
for (int i = 0; i < size; i++){
|
||||||
|
printf("\tIndex: %d, Value: %f\n", i, get_float_list(i, list));
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 20; i++){
|
||||||
|
printf("-");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]){
|
||||||
|
|
||||||
|
|
||||||
double_stack s = new_double_stack();
|
double_stack s = new_double_stack();
|
||||||
push_double_stack(42.5, &s);
|
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);
|
float_list* l = new_float_list(5.0, NULL);
|
||||||
add_float_list(6.0, l);
|
l = add_float_list(6.0, l);
|
||||||
add_float_list(7.0, l);
|
l = add_float_list(7.0, l);
|
||||||
add_float_list(8.0, l);
|
l = add_float_list(8.0, l);
|
||||||
printf("%d\n", size_float_list(l));
|
printList(l);
|
||||||
printf("%f\n", get_float_list(3, l));
|
l = insert_float_list(15.5525, 2, l);
|
||||||
insert_float_list(15.05, 2, l);
|
l = insert_float_list(25, 0, l);
|
||||||
printf("%f\n", get_float_list(1, l));
|
printList(l);
|
||||||
|
l = remove_float_list(2, l);
|
||||||
|
printList(l);
|
||||||
free_float_list(l);
|
free_float_list(l);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue