ASCOT5
Loading...
Searching...
No Matches
list.c
Go to the documentation of this file.
1
8#include <stdlib.h>
9#include "list.h"
10
17 (*list) = (list_int_node*) malloc(sizeof(list_int_node));
18 (*list)->data = 0;
19 (*list)->next = NULL;
20
21}
22
29 list_int_node* node = (*list);
30 int n = list_int_size(*list);
31 list_int_node* next_node = (*list)->next;
32 int i;
33 for(i = 0; i < n; i++) {
34 free(node);
35 node = next_node;
36 next_node = next_node->next;
37 }
38 free(node);
39 (*list) = NULL;
40}
41
48void list_int_add(list_int_node* list, int data) {
49 list_int_node* node = list;
50 while(node->next != NULL) {
51 node = node->next;
52 }
53
54 list_int_node* new_node = (list_int_node*) malloc(sizeof(list_int_node));
55 new_node->next = NULL;
56
57 node->next = new_node;
58 node->data = data;
59}
60
69int list_int_get(list_int_node* list, int index) {
70 int i = 0;
71 list_int_node* node = list;
72 while(i < index) {
73 node = node->next;
74 i++;
75 }
76 return node->data;
77}
78
87 int i = 0;
88 list_int_node* node = list;
89 while(node->next != NULL) {
90 node = node->next;
91 i++;
92 }
93 return i;
94}
int list_int_size(list_int_node *list)
Get list size.
Definition list.c:86
void list_int_create(list_int_node **list)
Create an empty list.
Definition list.c:16
void list_int_free(list_int_node **list)
Deallocate this list and all lists it is linked to.
Definition list.c:28
int list_int_get(list_int_node *list, int index)
Retrieve the data stored in a list node.
Definition list.c:69
void list_int_add(list_int_node *list, int data)
Add new node to the end of the chain.
Definition list.c:48
Header file for list.c.
Linked list node that stores int data.
Definition list.h:12
struct list_int_node * next
Definition list.h:13
int data
Definition list.h:14