aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorleiyu3 <s444814187@gmail.com>2022-09-22 12:33:41 -0400
committerleiyu3 <s444814187@gmail.com>2022-09-22 12:33:41 -0400
commitcaa60ca993d1659b38cbcd441ad9e31b68181bf9 (patch)
treef48bebaddb1176de91cc3391b0aa597f952b94bf
parentaac6ac4afc6d919771c2c342d85be565a26ba013 (diff)
downloadlinked_list_c-caa60ca993d1659b38cbcd441ad9e31b68181bf9.tar.gz
linked_list_c-caa60ca993d1659b38cbcd441ad9e31b68181bf9.zip
fixes
-rw-r--r--liblinked_list.c56
-rw-r--r--linked_list.h24
-rw-r--r--linked_list_test.c30
3 files changed, 59 insertions, 51 deletions
diff --git a/liblinked_list.c b/liblinked_list.c
index 83689a5..7c0706b 100644
--- a/liblinked_list.c
+++ b/liblinked_list.c
@@ -2,11 +2,10 @@
2#include <stdlib.h> 2#include <stdlib.h>
3#include "linked_list.h" 3#include "linked_list.h"
4 4
5int count_ll(node* ptr){ 5int count_ll(struct node* ptr){
6 /* 6 /*
7 Count the number of nodes in a linked list. 7 Count the number of nodes in a linked list.
8 */ 8 */
9 // TODO:: What if there is 0 item in the linked list??
10 int count = 0; 9 int count = 0;
11 while (ptr != NULL){ 10 while (ptr != NULL){
12 count++; 11 count++;
@@ -15,31 +14,42 @@ int count_ll(node* ptr){
15 return count; 14 return count;
16} 15}
17 16
18void prepend_ll(node **ptr, int value){ 17struct node* init_ll(int value){
18 // can make this create node
19 // and then let other function call this
20 // since this is a repeated function in prepend append
21 struct node* ptr = malloc(sizeof(struct node));
22 ptr->value = value;
23 ptr->next = NULL;
24
25 return ptr;
26}
27
28void prepend_ll(struct node **ptr, int value){
19 /* 29 /*
20 Add a node to the front of the linked list 30 Add a node to the front of the linked list
31 Push
21 */ 32 */
22 node* new = malloc(sizeof(node)); 33 struct node* new = malloc(sizeof(struct node));
23 new->value = value; 34 new->value = value;
24 new->next = *ptr; 35 new->next = *ptr;
25 *ptr = new; 36 *ptr = new;
26
27} 37}
28 38
29void append_ll(node* ptr, int value){ 39void append_ll(struct node* ptr, int value){
30 /* 40 /*
31 Add a node to the end of the linked list 41 Add a node to the end of the linked list
32 */ 42 */
33 node* new = malloc(sizeof(node)); 43 struct node* new = malloc(sizeof(struct node));
34 new->value = value;
35 new->next = NULL;
36 while (ptr->next != NULL){ 44 while (ptr->next != NULL){
37 ptr = ptr->next; 45 ptr = ptr->next;
38 } 46 }
47 new->value = value;
48 new->next = NULL;
39 ptr->next = new; 49 ptr->next = new;
40} 50}
41 51
42void print_ll(node* ptr){ 52void print_ll(struct node* ptr){
43 /* 53 /*
44 Print out the linked list 54 Print out the linked list
45 */ 55 */
@@ -54,11 +64,17 @@ void print_ll(node* ptr){
54 printf("\n"); 64 printf("\n");
55} 65}
56 66
57void free_ll(node* ptr){ 67void free_ll(struct node* ptr){
58 /* 68 /*
59 Safely free all nodes inside linked list. 69 Safely free all nodes inside linked list.
60 */ 70 */
61 node* next; 71 if (ptr == NULL){
72 // if head is NULL then the list must be empty
73 // no memory to free
74 return;
75 }
76
77 struct node* next;
62 while (ptr->next != NULL){ 78 while (ptr->next != NULL){
63 next = ptr->next; 79 next = ptr->next;
64 free(ptr); 80 free(ptr);
@@ -66,7 +82,7 @@ void free_ll(node* ptr){
66 } 82 }
67} 83}
68 84
69int update_node(node* ptr, int index, int value){ 85int update_node(struct node* ptr, int index, int value){
70 /* 86 /*
71 Update value of a node inside linked list 87 Update value of a node inside linked list
72 */ 88 */
@@ -87,13 +103,13 @@ int update_node(node* ptr, int index, int value){
87 return 0; 103 return 0;
88} 104}
89 105
90int delete_node(node **ptr, int index){ 106int delete_node(struct node **ptr, int index){
91 /* 107 /*
92 Delete a node given the index 108 Delete a node given the index
93 */ 109 */
94 110
95 // TODO:: What if the node doesn't exist? 111 // TODO:: What if the node doesn't exist?
96 node* prev; 112 struct node* prev;
97 if (index == 0){ 113 if (index == 0){
98 prev = *ptr; 114 prev = *ptr;
99 *ptr = (*ptr)->next; 115 *ptr = (*ptr)->next;
@@ -101,7 +117,7 @@ int delete_node(node **ptr, int index){
101 return 0; 117 return 0;
102 } 118 }
103 119
104 node* root = *ptr; 120 struct node* root = *ptr;
105 for (int i = 0; i <= index; i++){ 121 for (int i = 0; i <= index; i++){
106 if (i == index){ 122 if (i == index){
107 prev->next = root->next; 123 prev->next = root->next;
@@ -116,7 +132,7 @@ int delete_node(node **ptr, int index){
116 exit(-1); 132 exit(-1);
117} 133}
118 134
119int get_value(node *ptr, int index){ 135int get_value(struct node *ptr, int index){
120 /* 136 /*
121 Get the value of a specific node given the index 137 Get the value of a specific node given the index
122 */ 138 */
@@ -129,7 +145,7 @@ int get_value(node *ptr, int index){
129 145
130} 146}
131 147
132node* search_node(node *ptr, int value){ 148struct node* search_node(struct node *ptr, int value){
133 /* 149 /*
134 Search for a node given the value and returns it 150 Search for a node given the value and returns it
135 */ 151 */
@@ -147,8 +163,8 @@ node* search_node(node *ptr, int value){
147 return ptr; 163 return ptr;
148} 164}
149 165
150void reverse_ll(node** ptr){ 166void reverse_ll(struct node** ptr){
151 node *prev, *cur, *next; 167 struct node *prev, *cur, *next;
152 int n = count_ll(*ptr); 168 int n = count_ll(*ptr);
153 169
154 if (n == 0){ 170 if (n == 0){
diff --git a/linked_list.h b/linked_list.h
index 1b2c3ca..40df9d7 100644
--- a/linked_list.h
+++ b/linked_list.h
@@ -1,22 +1,22 @@
1#ifndef __LINKED_LIST_H__ 1#ifndef __LINKED_LIST_H__
2#define __LINKED_LIST_H__ 2#define __LINKED_LIST_H__
3 3
4typedef struct node node;
5struct node{ 4struct node{
6 int value; 5 int value;
7 node* next; 6 struct node* next;
8}; 7};
9 8
10int count_ll(node* ptr); 9int count_ll(struct node* ptr);
11void prepend_ll(node **ptr, int value); 10struct node* init_ll(int value);
12void append_ll(node* ptr, int value); 11void prepend_ll(struct node **ptr, int value);
13void print_ll(node* ptr); 12void append_ll(struct node* ptr, int value);
14void free_ll(node* ptr); 13void print_ll(struct node* ptr);
15int update_node(node* ptr, int index, int value); 14void free_ll(struct node* ptr);
16int delete_node(node **ptr, int index); 15int update_node(struct node* ptr, int index, int value);
17int get_value(node *ptr, int index); 16int delete_node(struct node **ptr, int index);
18node* search_node(node *ptr, int value); 17int get_value(struct node *ptr, int index);
19void reverse_ll(node **ptr); 18struct node* search_node(struct node *ptr, int value);
19void reverse_ll(struct node **ptr);
20 20
21 21
22#endif 22#endif
diff --git a/linked_list_test.c b/linked_list_test.c
index a71a1ff..7fab681 100644
--- a/linked_list_test.c
+++ b/linked_list_test.c
@@ -3,27 +3,19 @@
3#include "linked_list.h" 3#include "linked_list.h"
4 4
5int main(){ 5int main(){
6 6 // should I make it so that when you make a node you have to first initialize it with a value?
7 node* ll = malloc(sizeof(node)); 7 // this means that we essentially can not have an empty list.
8 ll->value = 1; 8
9 ll->next = NULL; 9 // NULL will count as an empty list
10 10 struct node* ll = init_ll(1);
11 11
12 /* prepend_ll(&ll, 1); */
13 printf("%d\n",count_ll(ll));
12 print_ll(ll); 14 print_ll(ll);
13 reverse_ll(&ll); 15 /* delete_node(&ll,0); */
14 print_ll(ll);
15 /* delete_node(&ll, 0); */
16
17 /* print_ll(ll); */
18 /* printf("The length of ll is %d\n", count_ll(ll)); */
19
20 /* delete_node(&ll, 0); */
21 /* print_ll(ll); */
22 /* printf("The length of ll is %d\n", count_ll(ll)); */
23 /* printf("%d\n", search_node(ll, 10)->value); */
24 16
25 /* printf("%d\n", get_value(ll, 3)); */ 17 /* printf("%d\n",count_ll(ll)); */
26 18
27 /* free_ll(ll); */ 19 free_ll(ll);
28 return 0; 20 return 0;
29} 21}