diff --git a/recipes/C/570000_double_linked_list/README.md b/recipes/C/570000_double_linked_list/README.md new file mode 100644 index 000000000..1bb08dc5c --- /dev/null +++ b/recipes/C/570000_double_linked_list/README.md @@ -0,0 +1,6 @@ +# Double Linked List +Originally published: 2020-05-16 5:10:00 +Last updated: 2020-03-16 5:10:00 +Author: Prithi Pal Singh + +minimalistic implementation of doubly linked list in C. diff --git a/recipes/C/570000_double_linked_list/double_linked_list.c b/recipes/C/570000_double_linked_list/double_linked_list.c new file mode 100644 index 000000000..5759dc5fa --- /dev/null +++ b/recipes/C/570000_double_linked_list/double_linked_list.c @@ -0,0 +1,57 @@ + +#include +#include + + +struct binode { + int value ; + struct binode *next_ptr ; + struct binode *back_ptr ; +}; + +void createDLLFromArray(struct binode *dll, int *arr, int arr_size){ + + // first binode configuration + dll->back_ptr = NULL; + dll->value = *arr; // first element of array + + struct binode *prev = dll ; + + // second node and onwards.. + for(int i = 1 ; i < arr_size ; i ++ ){ + + struct binode *new_node = (struct binode *)malloc(sizeof(struct binode)) ; + new_node->value = *(arr+i) ; + new_node->back_ptr = prev ; + prev->next_ptr = new_node ; + prev = new_node ; + } + + prev->next_ptr = NULL; + +} + +void printDLL(struct binode *startNode){ + + struct binode *ptr = startNode ; + + printf("NULL <=> "); + while(ptr!=NULL){ + printf("%d <=> ",ptr->value); + ptr = ptr->next_ptr ; + } + + printf("NULL \n"); +} + + +int main(){ + + struct binode my_node; + int arr[10] = {2,4,6,8,10,12,14,16,18,20} ; + + createDLLFromArray(&my_node,arr,10) ; + printDLL(&my_node); + + return 0; +} diff --git a/recipes/C/570001_struct_pointers_examples/README.md b/recipes/C/570001_struct_pointers_examples/README.md new file mode 100644 index 000000000..258e2597e --- /dev/null +++ b/recipes/C/570001_struct_pointers_examples/README.md @@ -0,0 +1,6 @@ +# Struct Pointers Understanding +Originally published: 2020-03-16 5:00:00 +Last updated: 2020-03-16 5:00:00 +Author: Prithi Pal Singh + +This is a piece of C code I wrote to understand using structs, pointers (normal and double pointers) and their usage around. So for different scenarios, like accessing struct using single pointer, 2d dynamically increasing space using double pointers and other cases also. diff --git a/recipes/C/570001_struct_pointers_examples/struct_pointers_example.c b/recipes/C/570001_struct_pointers_examples/struct_pointers_example.c new file mode 100644 index 000000000..3b99c5eb9 --- /dev/null +++ b/recipes/C/570001_struct_pointers_examples/struct_pointers_example.c @@ -0,0 +1,111 @@ +#include +#include + +struct value { + int v ; +}; +struct node { + int val1 ; + int val2 ; + int *val3 ; +}; + +struct node2{ + int v1; + int v2; +}; + +struct node* createStructArr(int n, int **b){ + // defined n + struct node *ptr = (struct node *)malloc(n*sizeof(struct node)) ; + + // stores ppinter to the val1 for each struct. + b = (int **)malloc(sizeof(int)*n); + + struct node *p = ptr ; + for(int i = 0 ; i < n ; i ++ ){ + p->val1 = i ; + p->val2 = i+1; + + + b[i] = &(p->val1) ; + + // CREATING MALLOC FOR POINTER SO THAT IT IS NOT LOST AFTER EXITING STACK FRAME OF THIS FUNCTION. + int *v3 = (int *)malloc(sizeof(int)); + *v3=i+2; + p->val3 = v3 ; + p++; + + + + } + return ptr; +} + +void printStructArr(struct node *a, int n){ + + + int first,second; + struct node *ptr = a ; + + + for(int i = 0 ; i < n ; i ++ ){ + + // ACCESSING VIA ANOTHER MOVING POINTER. + // NOT TOUCH THE BASE POINTER a + printf("(%d,%d,%d) ", ptr->val1,ptr->val2,*(ptr->val3)) ; + ptr++; + + // ACCESSING VIA BASE POINTER idx notation + printf("(%d,%d,%d) ", a[i].val1,a[i].val2,*(a[i].val3)); + + // ACCESSING VIA BASE POINTER pointer notation + printf("(%d,%d,%d) ", (a+i)->val1, (a+i)->val2, *((a+i)->val3)); + + + printf("\n"); + } + +} + +void createAccessArr(){ + + // dynamically increasing array without declaring dimension at time of declaration. + struct node2 *n = (struct node2 *)malloc(sizeof(*n)); + struct node2 *ptr = n ; + + for(int i = 0 ; i < 10 ; i ++ ){ + + // ACCESSING THROUGH MOVING PTR. + ptr->v1 = i ; + ptr->v2 = i+1 ; + ptr++; + + // ACCESSING THROUGH BASE PTR pointer notation + (n+i)->v1 = i ; + (n+i)->v2 = i+3 ; + + } + +} + +void printVal2FromVal1Reference(int **a, int n){ + + int *addr ; + for(int i = 0 ; i < n ; i++ ){ + + printf("%p ",addr); + } +} + +int main() + +{ + int **b ; + int n = 10 ; + struct node *p = createStructArr(n,b); + printStructArr(p,n); + printVal2FromVal1Reference(b,n); + return 0; +} +