diff --git a/learnc/a.exe b/learnc/a.exe index 6e32960..7e2ac55 100755 Binary files a/learnc/a.exe and b/learnc/a.exe differ diff --git a/learnc/learnc14.c b/learnc/learnc14.c new file mode 100755 index 0000000..25b7eee --- /dev/null +++ b/learnc/learnc14.c @@ -0,0 +1,29 @@ + #include + + int main() { + int intarray[5] = {10,20,30,40,50}; + //-----------------------^ + int *pointer = &intarray[2]; + + // Array of 3 pointers + int *parray[3]; + + // Copy last three addresses of intarray into parray + // Use parray and pointer + int i; + for (i = 0; i < 3; i++) { + // Insert code here + parray[i] = pointer + i; + } + + // Test code + for (i = 0; i < 3; i++) { + if (parray[i] == &pointer[i]) { + printf("Matched!\n"); + } else { + printf("Fail\n"); + } + } + + return 0; + } \ No newline at end of file diff --git a/learnc/learnc15.c b/learnc/learnc15.c new file mode 100755 index 0000000..e2a7144 --- /dev/null +++ b/learnc/learnc15.c @@ -0,0 +1,36 @@ + #include + + void f1(int var) + { + printf("this is f1 and var is: %d\n", var); + } + + void f2(int var) + { + printf("this is f2 and var is: %d\n", var); + } + + void f3(int var) + { + printf("this is f3 and var is: %d\n", var); + } + + int main() + { + /* define an array full of function pointers + to the above functions, that take an `int` as + their only argument */ + void(*fp[])(int) = {f1, f2, f3}; + + int c = 0; + while(c < 3) + { + /* call the functions using the function pointers + of the array at index `c` with `c` as an argument */ + fp[c](c); + + ++c; + } + + return 0; + } \ No newline at end of file diff --git a/learnc/learnc16.c b/learnc/learnc16.c new file mode 100755 index 0000000..d523bf7 --- /dev/null +++ b/learnc/learnc16.c @@ -0,0 +1,78 @@ +#include +#include + +typedef struct node { + int val; + struct node * next; +} node_t; + +void print_list(node_t * head) { + node_t * current = head; + + while (current != NULL) { + printf("%d\n", current->val); + current = current->next; + } +} + +int pop(node_t ** head) { + int retval = -1; + node_t * next_node = NULL; + + if (*head == NULL) { + return -1; + } + + next_node = (*head)->next; + retval = (*head)->val; + free(*head); + *head = next_node; + + return retval; +} + +int remove_by_value(node_t ** head, int val) { + /* TODO: fill in your code here */ + int i = 0; + int retval = -1; + node_t * current = *head; + node_t * temp_node= NULL; + + if (val == 0) { + return pop(head); + } + + for (i = 0; i < val-1; i++) { + if (current->next == NULL) { + return -1; + } + current = current->next; + } + if (current->next == NULL) { + return -1; + } + + temp_node = current->next; + retval = temp_node->val; + current->next = temp_node->next; + free(temp_node); + + return retval; +} + +int main() { + + node_t * test_list = (node_t *) malloc(sizeof(node_t)); + test_list->val = 1; + test_list->next = (node_t *) malloc(sizeof(node_t)); + test_list->next->val = 2; + test_list->next->next = (node_t *) malloc(sizeof(node_t)); + test_list->next->next->val = 3; + test_list->next->next->next = (node_t *) malloc(sizeof(node_t)); + test_list->next->next->next->val = 4; + test_list->next->next->next->next = NULL; + + remove_by_value(&test_list, 3); + + print_list(test_list); +} \ No newline at end of file