aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore5
-rw-r--r--Makefile16
-rw-r--r--liblinked_list.c179
-rw-r--r--linked_list.h22
-rw-r--r--readme.md33
5 files changed, 255 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..47c0732
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
1a.out
2linked_list_test*
3*.a
4*.o
5
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..9bf42df
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,16 @@
1CC=clang
2CFLAGS=-Wall -g
3BINS= linked_list.o liblinked_list.a linked_list_test
4all: $(BINS)
5
6linked_list.o: linked_list.h liblinked_list.c
7 $(CC) $(CFLAGS) -c liblinked_list.c
8
9liblinked_list.a: liblinked_list.o
10 ar -cvq liblinked_list.a liblinked_list.o
11
12linked_list_test: linked_list_test.c liblinked_list.a
13 $(CC) $(CFLAGS) -o $@ $^
14
15clean:
16 rm *.a *.o linked_list_test
diff --git a/liblinked_list.c b/liblinked_list.c
new file mode 100644
index 0000000..83689a5
--- /dev/null
+++ b/liblinked_list.c
@@ -0,0 +1,179 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include "linked_list.h"
4
5int count_ll(node* ptr){
6 /*
7 Count the number of nodes in a linked list.
8 */
9 // TODO:: What if there is 0 item in the linked list??
10 int count = 0;
11 while (ptr != NULL){
12 count++;
13 ptr = ptr->next;
14 }
15 return count;
16}
17
18void prepend_ll(node **ptr, int value){
19 /*
20 Add a node to the front of the linked list
21 */
22 node* new = malloc(sizeof(node));
23 new->value = value;
24 new->next = *ptr;
25 *ptr = new;
26
27}
28
29void append_ll(node* ptr, int value){
30 /*
31 Add a node to the end of the linked list
32 */
33 node* new = malloc(sizeof(node));
34 new->value = value;
35 new->next = NULL;
36 while (ptr->next != NULL){
37 ptr = ptr->next;
38 }
39 ptr->next = new;
40}
41
42void print_ll(node* ptr){
43 /*
44 Print out the linked list
45 */
46 while (ptr != NULL){
47 printf("%d", ptr->value);
48 ptr = ptr->next;
49 if (ptr == NULL){
50 break;
51 }
52 printf(" - ");
53 }
54 printf("\n");
55}
56
57void free_ll(node* ptr){
58 /*
59 Safely free all nodes inside linked list.
60 */
61 node* next;
62 while (ptr->next != NULL){
63 next = ptr->next;
64 free(ptr);
65 ptr = next;
66 }
67}
68
69int update_node(node* ptr, int index, int value){
70 /*
71 Update value of a node inside linked list
72 */
73 // TODO:: What if the index is out of range?
74
75 if (index >= count_ll(ptr)){
76 // index out of range
77 return 1;
78 }
79
80 int count = 0;
81 while (count < index){
82 ptr = ptr->next;
83 count++;
84 }
85
86 ptr->value = value;
87 return 0;
88}
89
90int delete_node(node **ptr, int index){
91 /*
92 Delete a node given the index
93 */
94
95 // TODO:: What if the node doesn't exist?
96 node* prev;
97 if (index == 0){
98 prev = *ptr;
99 *ptr = (*ptr)->next;
100 free(prev);
101 return 0;
102 }
103
104 node* root = *ptr;
105 for (int i = 0; i <= index; i++){
106 if (i == index){
107 prev->next = root->next;
108 free(root);
109 return 0;
110 }
111 prev = root;
112 root = root->next;
113 }
114
115 printf("Node not found\n");
116 exit(-1);
117}
118
119int get_value(node *ptr, int index){
120 /*
121 Get the value of a specific node given the index
122 */
123 // TODO:: What if the index is out of range??
124
125 for (int i = 0; i < index; i++){
126 ptr = ptr->next;
127 }
128 return ptr->value;
129
130}
131
132node* search_node(node *ptr, int value){
133 /*
134 Search for a node given the value and returns it
135 */
136
137 while (1){
138 if (ptr == NULL){
139 fprintf(stderr, "Value doesn't exitst in linked list\n");
140 exit(-1);
141 }
142 if (ptr->value == value){
143 break;
144 }
145 ptr = ptr->next;
146 }
147 return ptr;
148}
149
150void reverse_ll(node** ptr){
151 node *prev, *cur, *next;
152 int n = count_ll(*ptr);
153
154 if (n == 0){
155 return;
156 }
157
158 if (n == 1){
159 return;
160 }
161
162 prev = *ptr;
163 cur = prev->next;
164 next = cur->next;
165 prev->next = NULL;
166
167 cur->next = prev;
168
169 for (int i = 0; i < n-2; i++){
170 prev = cur;
171 cur = next;
172 next = next->next;
173 cur->next = prev;
174 }
175
176
177 *ptr = cur;
178}
179
diff --git a/linked_list.h b/linked_list.h
new file mode 100644
index 0000000..1b2c3ca
--- /dev/null
+++ b/linked_list.h
@@ -0,0 +1,22 @@
1#ifndef __LINKED_LIST_H__
2#define __LINKED_LIST_H__
3
4typedef struct node node;
5struct node{
6 int value;
7 node* next;
8};
9
10int count_ll(node* ptr);
11void prepend_ll(node **ptr, int value);
12void append_ll(node* ptr, int value);
13void print_ll(node* ptr);
14void free_ll(node* ptr);
15int update_node(node* ptr, int index, int value);
16int delete_node(node **ptr, int index);
17int get_value(node *ptr, int index);
18node* search_node(node *ptr, int value);
19void reverse_ll(node **ptr);
20
21
22#endif
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..c302e1b
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,33 @@
1# Linked List in C
2This is a c library for linked list done as a learning project.
3
4The nodes stores integer values but can be switched to store any data you desire.
5
6Hopefully by viewing the source code you can learn how different operations on the linked list is implemented.
7
8# What has been implemented?
9
10First initialize your linked list like so:
11
12```c
13node* ll = malloc(sizeof(node));
14ll->value = 1;
15ll->next = NULL;
16```
17
18Then the following functions are available for you to interact with the linked list.
19
20These are the functions available in the header file.
21
22```c
23int count_ll(node* ptr);
24void prepend_ll(node **ptr, int value);
25void append_ll(node* ptr, int value);
26void print_ll(node* ptr);
27void free_ll(node* ptr);
28int update_node(node* ptr, int index, int value);
29int delete_node(node **ptr, int index);
30int get_value(node *ptr, int index);
31node* search_node(node *ptr, int value);
32void reverse_ll(node **ptr);
33```