Skip to content

new C receipes #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions recipes/C/570000_double_linked_list/README.md
Original file line number Diff line number Diff line change
@@ -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.
57 changes: 57 additions & 0 deletions recipes/C/570000_double_linked_list/double_linked_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

#include <stdlib.h>
#include <stdio.h>


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;
}
6 changes: 6 additions & 0 deletions recipes/C/570001_struct_pointers_examples/README.md
Original file line number Diff line number Diff line change
@@ -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.
111 changes: 111 additions & 0 deletions recipes/C/570001_struct_pointers_examples/struct_pointers_example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <stdio.h>
#include <stdlib.h>

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;
}