From 8e9c6780ed08a67948d37ab52e55e6c61e570de3 Mon Sep 17 00:00:00 2001 From: Prithi Date: Mon, 16 Mar 2020 16:49:54 -0700 Subject: [PATCH 1/4] Created 570000_double_linked_list and code --- .../double_linked_list.c | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 recipes/C/570000_double_linked_list/double_linked_list.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; +} From a60ebb3d961a2cb141035d3011013dab03b6c105 Mon Sep 17 00:00:00 2001 From: Prithi Date: Mon, 16 Mar 2020 16:51:47 -0700 Subject: [PATCH 2/4] created README.md for 570000_double_linked_list --- recipes/C/570000_double_linked_list/README.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 recipes/C/570000_double_linked_list/README.md 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..19aeff34b --- /dev/null +++ b/recipes/C/570000_double_linked_list/README.md @@ -0,0 +1,6 @@ +# Double Linked List +Originally published: 2009-05-20 18:19:52 +Last updated: 2009-05-20 18:19:52 +Author: J Y + +minimalistic implementation of doubly linked list in C. From 0888ae59a51a111fb7ac33019540176e04cdf8d4 Mon Sep 17 00:00:00 2001 From: Prithi Date: Mon, 16 Mar 2020 17:00:07 -0700 Subject: [PATCH 3/4] Created 570001_struct_pointers_example --- .../570001_struct_pointers_examples/README.md | 6 + .../struct_pointers_example.c | 111 ++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 recipes/C/570001_struct_pointers_examples/README.md create mode 100644 recipes/C/570001_struct_pointers_examples/struct_pointers_example.c 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..6f6bb5a0f --- /dev/null +++ b/recipes/C/570001_struct_pointers_examples/README.md @@ -0,0 +1,6 @@ +# Struct Pointers Understanding +Originally published: 2019-03-16 5:00:00 +Last updated: 2019-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; +} + From 493bfdf44f37eb3574bb2e9f5fb8e73552a778fb Mon Sep 17 00:00:00 2001 From: Prithi Date: Mon, 16 Mar 2020 17:03:16 -0700 Subject: [PATCH 4/4] Made some changes in readme files --- recipes/C/570000_double_linked_list/README.md | 6 +++--- recipes/C/570001_struct_pointers_examples/README.md | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/recipes/C/570000_double_linked_list/README.md b/recipes/C/570000_double_linked_list/README.md index 19aeff34b..1bb08dc5c 100644 --- a/recipes/C/570000_double_linked_list/README.md +++ b/recipes/C/570000_double_linked_list/README.md @@ -1,6 +1,6 @@ # Double Linked List -Originally published: 2009-05-20 18:19:52 -Last updated: 2009-05-20 18:19:52 -Author: J Y +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/570001_struct_pointers_examples/README.md b/recipes/C/570001_struct_pointers_examples/README.md index 6f6bb5a0f..258e2597e 100644 --- a/recipes/C/570001_struct_pointers_examples/README.md +++ b/recipes/C/570001_struct_pointers_examples/README.md @@ -1,6 +1,6 @@ # Struct Pointers Understanding -Originally published: 2019-03-16 5:00:00 -Last updated: 2019-03-16 5:00:00 +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.