How to Pass an Array to Function

C code for passing array to a function and returning pointer from function.
#include<stdio.h>
#include<stdlib.h>
void func(int *arr, int n);
int main()
{
int i;
int arr[]={1,2,3,4,5};

    printf("\nArray element after function : \n");
func(arr,5);//its a pass by reference only as name of the arry is the base address
    for(i=0;i<5;i++)
    {
        printf("%d ",arr[i]);//11 12 13 14 15
    }
    printf("\n");
    return 0;
}
void func(int *p, int n)
{
int *arr=p;
    int i;
    for(i=0;i<n;i++)
    {
        arr[i]=p[i]+10;
    }

}//end of main

Example 2:
/*
   c code to pass array to a function
 */
#include<stdio.h>
#include<stdlib.h>
int* func(int *arr, int n);
int main()
{
int i;
int arr[]={1,2,3,4,5};
int *p;
p=func(arr,5);
    printf("\nArray element after function : \n");
    for(i=0;i<5;i++)
    {
        printf("%d ",p[i]);//11,12,13,14,15
    }
    printf("\n");
    return 0;
}
int* func(int *arr, int n)
{
    int i;
    int *p=arr;
    p=(int *)malloc(n*sizeof(int));
    for(i=0;i<n;i++)
    {
        p[i]=arr[i]+10;//arr[i] is must not p[i]
    }
    return p;
}

Example 3: not a good program
/*
   c code to pass array to a function
 */
#include<stdio.h>
#include<stdlib.h>
int* func(int *arr, int n);
int main()
{
    int i;
    int arr[]={1,2,3,4,5};

    printf("\nArray element after function : \n");
    func(arr,5);
    for(i=0;i<5;i++)
    {
        printf("%d ",arr[i]);//1 2 3 4 5
    }
    printf("\n");
    return 0;
}
int*  func(int *p, int n)
{
    int *arr=p;
    arr=(int *)malloc(n*sizeof(int));
    int i;
    for(i=0;i<n;i++)
    {
        arr[i]=arr[i]+10;//arr[i] is must not p[i]
    }
    return arr;/*if returning a pointer then function should have been called from a pointer */

}

Pointer to Array of Pointers in C

C Code for pointer to array of char* ie pointer to strings.
#include<stdio.h>
int main()
{
    /*array of strings*/
char arr[3][10]={
            "Ram",/*first string ie first char array*/
            "Bhagwan",
            "Yeshu"
            };
/*pointer to array.Each array is 1-d array of chars ie each array is string*/
char (*p)[10];
p=(char *)arr;/*  p=arr+0 so p first points to first char array ie first string Ram*/

char* pp;

How to use Pointer to An Array of Integers in C

What is Pointer to an Array in C?
int (*p)[4];//p is pointer to array and each array is having 4 its
 so,p points to base address of first array ie address of other elements of first array.
 p+1 points to base address of second array and not the 1st element of first array.
 p+2 points to base address of third array  and not the 2nd element of first array.
 To access the individual elements of each array:
 assign the pointer to array to a pointer to int here pp and access through *(pp+i).

#include<stdio.h>
int main()
{
    /*array of strings*/
    int arr[3][4]={
        {0,1,2,3},/*first string ie first char array*/
        {4,5,6,7},
        {8,9,1,2},
    };
    /*pointer to array.Each array is 1-d array of chars ie each array is string*/
    int (*p)[4];
    p=(int *)arr;/*  p=arr+0 so p first points to first char array ie first string Ram*/

    int* pp;

Pointer to use Array of Integers in C

How to use pointer to an array of integers:
 int a[]={10,20,30};//array of ints
 int (*p)[3];//p is pointer to array
 so if int *q=p then
 q points to first array {10,20,30}
 q+1 points to second array {garbage here}
 q+2 points to third array {garbage here}

Note:here only one 1-d array so using pointer to array is not useful.
So, generally pointer to array is used with multdimensional arrays

/*
 example of pointer to array, best with 2-d array as 2d array is array of multiple 1-d arrays

 int a2d[3][3]={//rray of three 1-d arrays each having 3 element
     {10,20,30},
     {40,50,60},
     {70,80,90}
 };
 int (*ptr)[3];//ptr is pointer to array of 3 ints so contins address
 ptr=a2d;
 */
 /*first row of 2d array is first 1-d array. ptr+0 contains the base address of first array{10,20,30}; */
 /*second row is second array ptr+2 contains the base address of second array{40,50,60};*/
 /*third row is third array ptr+2 contains the base address of third array{70,80,90};*/
/*
 so incremting ptr++ will give the base address of next array and not the next element of first array and so on
 so check how to access individual elements of array using pointer to array at last of code
 */
#include<stdio.h>
int main()
{
 int a[]={10,20,30};//array of ints
 int (*p)[3];/*p is pointer to array of 3 ints so contins address*/

 p=a;

Array of Strings in C

Array of pointers to string.First its important to recall what is string in C? In C string is an array of characters terminated by \0.string can be represented in two ways:
 char str[]="Hello";//str is string
 or
 char* str="Hello";//here str is pointer so it stores the base address of the string ie base addrees of the array of characters.

 So, Now its quite clear that array of pointer to string means array of base addresses of strings.

 char* stra[]={"Programmer","are","Great"};//stra is array of pointers.

 so stra[0] contains base address of "Programmers".
 so stra[1] contains base address of "are".
 so stra[2] contains base address of "Great".

full Example code:

How to use Array of integer Pointers in C

Array of pointers to integers.i.e.Array of integer pointers.Since,Integer pointer store address of one integer.Therefore,Array of pointers to integers will store addresses many integers.
#include<stdio.h>
int main()
{
    int a[]={10,20,30};/*array of integers*/
    int* arr[]={a,a+1,a+2};/*array of integer pointers so contains addresses*/
 
    printf("\nAddresses of integers:\n");
    printf("address of a=%u\n",a);/*no & needed as name of the array gives base address.*/
    printf("address of a+1=%u\n",(a+1));
    printf("address of a+2=%u\n",a+2);

    printf("\nAddrs of integers are stored in arr so each arr[i] is an address:\n");
    printf("arr[0]=%u\n",arr[0]);
    printf("arr[1]=%u\n",arr[1]);
    printf("arr[2]=%u\n",arr[2]);

Array of Pointers to integer in C

/*Array of pointers to integers i.e. Array of integer pointers. Since integer pointer store one address.So,array of integer pointers is used to store  many addresses .*/
#include<stdio.h>
int main()
{
    printf("\nBefore starting array of pointers Understand pointer \n");
    printf("\nint *p; tells to compiler that p will be used to store single address of integer\n");
    int *p;//p will be used to store address of a pointer
    int i=10;
    p=&i;//pointer is used to store address of int i
    printf("address of i=%u or p=%u\n",&i,p);
    printf("value of i=%d or *p=%u\n",i,*p);


    printf("\nNow understanding array of pointer to integers.\n");
    printf("\narray of pointers to integers is used to store many addresses of integers\n");
    int a=10,b=20,c=30;
    int* arr[]={&a,&b,&c};/*arr is array of integer pointers so contains addresses*/
 
    printf("\nAddresses of integers:\n");
    printf("address of a=%u\n",&a);
    printf("address of b=%u\n",&b);
    printf("address of c=%u\n",&c);

Circular Queue, Circular linked list

/*linked list as circular queue*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};

void addq(struct node **f, struct node **r, int d);
void deleteq(struct node **f, struct node **r);
void display(struct node *f);

Singly linked list all operations by passing head

/*Code for:
  linked list through head
  1.insert at beginning
  2.inser after given position
  3.inser at end/append
  4.display
  5.reverse
  6.delete from beginning
  7.delete the given node value
  8.countnumber of nodes
 */
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *head;//*head is start

void insertBegin(struct node **head, int d);
void insertPosition(struct node **head, int position, int data);
void insertEnd(struct node **head, int d);
void display(struct node *head);
void count(struct node *head);
void reverse(struct node **head);
void deleteBeginning(struct node **head);
void deleteKey(struct node **head);

Linked list all operations without head pass in c

/*   code for single linked list operations:
   1.insert at Begin
   2.insert at Last//append
   3.Insert at Given Position
   4.Display
   5.reverse
   6.delete at beginning
   7.delete given node
 */
#include<stdio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node *next;
};
struct node *start;

void insertAtBegin(int d);
void insertAtLast(int d);
void insertAtPosition(int d,int p);
void display();
void reverse();
void deleteAtBegin();
void deleteKey();
void count();

Dynamic Memory Allocation

C Programs  for dynamic memory allocations in C
/*code for One Dimensional array dynamic memory allocaion*/
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 5

int main()
{
int *ptr;
ptr=(int *)malloc(MAXSIZE*sizeof(int));

int i;
for(i=0;i<MAXSIZE;i++)
{
printf("ptr[%d]=%d \n",i,i);
}

printf("\n");

return 0;
}
/*
ptr[0]=0
ptr[1]=1
ptr[2]=2
ptr[3]=3
ptr[4]=4
*/

/*
code for 2 D alternate dynamic allocation
*/

#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOLUMN 4

int main()
{
int *ptr,i,j;
ptr=(int *)malloc(MAXROW*MAXCOLUMN*sizeof(int));

for(i=0;i<MAXROW;i++)
{
for(j=0;j<MAXCOLUMN;j++)
{
printf("%d+%d=(%d) ",i,j,i+j);
}
printf("\n");
}
printf("\n");
return 0;
}
/*
0+0=(0) 0+1=(1) 0+2=(2) 0+3=(3)
1+0=(1) 1+1=(2) 1+2=(3) 1+3=(4)
2+0=(2) 2+1=(3) 2+2=(4) 2+3=(5)

*/



/*
code for 2-Dimensional array
*/

#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOLUMN 4
int main()
{
int **ptr,i,j;

ptr=(int **)malloc(MAXROW*sizeof(int *));

for(i=0;i<MAXROW;i++)
{
ptr[i]=(int *)malloc(MAXCOLUMN*sizeof(int ));
}


for(i=0;i<MAXROW;i++)
{
for(j=0;j<MAXCOLUMN;j++)
{
printf("%d+%d=(%d) ",i,j,i+j);
}
printf("\n");
}

return 0;
}
/*
0+0=(0) 0+1=(1) 0+2=(2) 0+3=(3)
1+0=(1) 1+1=(2) 1+2=(3) 1+3=(4)
2+0=(2) 2+1=(3) 2+2=(4) 2+3=(5)
*/



/*
dynamic memory allocation for 3-Dimensinal array
*/
#include<stdio.h>
#include<stdlib.h>
#define MAXX 2
#define MAXY 3
#define MAXZ 4
int main()
{
int ***ptr,i,j,k;
ptr=(int ***)malloc(MAXX*sizeof(int **));

for(i=0;i<MAXX;i++)
{
ptr[i]=(int **)malloc(MAXX*sizeof(int *));
for(j=0;j<MAXY;j++)
{
ptr[i][j]=(int *)malloc(MAXY*sizeof(int));
}
}

for(i=0;i<MAXX;i++)
{
for(j=0;j<MAXY;j++)
{
for(k=0;k<MAXZ;k++)
{
//printf("%d ",i);
printf("%d+%d+%d=(%d) ",i,j,k,i+j+k);
}
printf("\n");
}
printf("\n");
}

return 0;
}
/*
//when i
0 0 0 0
0 0 0 0
0 0 0 0

1 1 1 1
1 1 1 1
1 1 1 1


//when i+j+k
0+0+0=(0) 0+0+1=(1) 0+0+2=(2) 0+0+3=(3)
0+1+0=(1) 0+1+1=(2) 0+1+2=(3) 0+1+3=(4)
0+2+0=(2) 0+2+1=(3) 0+2+2=(4) 0+2+3=(5)

1+0+0=(1) 1+0+1=(2) 1+0+2=(3) 1+0+3=(4)
1+1+0=(2) 1+1+1=(3) 1+1+2=(4) 1+1+3=(5)
1+2+0=(3) 1+2+1=(4) 1+2+2=(5) 1+2+3=(6)
*/

….


-----

/*
code for dynamic memory allocation for structure
*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int empid;
char *name;
};
int main()
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->name=(char *)malloc(sizeof(30));
newnode->empid=1020;
newnode->name="Aditya";

printf("\nempid=%d and name=%s\n\n",newnode->empid,newnode->name);

newnode->empid=1021;
newnode->name="Birla";
printf("\nempid=%d and name=%s\n\n",newnode->empid,newnode->name);

return 0;
}
/*
empid=1020 and name=Aditya

empid=1021 and name=Birla
*/

/*
code Showing PROBLEM of returning local array address and solution
1. static array
2. dynamic memory alllocation
*/

#include<stdio.h>
#include<stdlib.h>//malloc
#include<string.h>//strcpy
char* func1();
char* func11();
char* func2();

int main()
{
char *ptr1;
ptr1=func1();
printf("func1 returnd: %s\n",ptr1);//prints some GARBAGE as array of func1 was local/auto

char *ptr11;
ptr11=func11();
printf("func11 returnd: %s\n",ptr11);//prints Hello India11


char *ptr2;
ptr2=func2();
printf("func2 returnd: %s\n",ptr2);//prints Hello India 2


printf("\n\n");
return 0;
}
/*PROBLEM with returning local array*/
char* func1()
{
char buff1[20];//local array
//buff1="Hello India1";/*error:incompatible types when assigning to type âar[20]ârom type âar *â*/
strcpy(buff1,"HEllo India1");//warning
return (buff1);//warning returning local array is not ok
}

/*SOLUTION 1: use static array*/
char* func11()
{
static char buff11[20];//istatic local array
strcpy(buff11,"HEllo India11");//warning
return (buff11);//returning static local array is okay
}

/*SOLUTION 2: dynamic memory allocation*/
char* func2()
{
char *buff2;
buff2=(char *)malloc(20);
buff2="Hello India2";
return (buff2);/*returning dynamically allocated memory is okay*/
}
/*
func1 returnd: á/c· ú¿
func11 returnd: HEllo India11
func2 returnd: Hello India2
*/


/*
Difference between:
1. char p1[5] //Array of characters
2. char* p2[5] //Array of character pointers
3. char (*p3)[5] //Pointer to Array of character
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int *ptr[10];//ptr is array of 10 integer pointers
printf("\nptr[0]=%p\n",ptr[0]);//0x8049ff4

int arr[10]={0,1,2,3,4,5,6,7,8,9};

//assgining ptr[0] with the address of arr[0]
*ptr=arr;
//*ptr=arr[0];//warning: assignment makes pointer from integer without a cast
printf("\n ptr[0]=%p \n arr[0]=%p\n",ptr[0],arr);//0x8049ff4

/*with characters */

char p1[5];//p1 is 5-element array of chars
char *p2[5];// p2 is 5-element array of char *
char (*p3)[5];//p3 is pointer to 5-element array of chars

strcpy(p1, "Hello");

p2[0] = (char *) malloc(5 * sizeof(char));
strcpy(p2[0], "India is Great");

p3 = &p1;

printf("\n p1 = %s\np2[0] = %s\np3 = %s\n", p1, p2[0], *p3);
printf("\n");
return 0;
}
/*
___________
p1 |h| e | l | l | o |
---------------


"adi" "mahi" "soni" "jaya" "bangalore"
^          ^        ^           ^      ^
|            |          |            |        |
____________________________________
|0x9999|0x7777|0x3333|0x2222|0x6666|
|___0__|___1__|___2__|___3__|___4__|
p2




________ _____________________
|0x7777| -------> | H | e | l | l | o |
|______| |_0_|_1_|_2_|_3_|_4_|
p3 ^
|
0x7777
*/
/*
ptr[0]=0xb7765fdc

ptr[0]=0xbfe81c34
arr[0]=0xbfe81c34

p1 = Hello
p2[0] = India is Great
p3 = Hello

*/


C code for removing duplicate nodes in linked list


linked list implementation in C
//removing duplicate elements without sorting
//using two loops
#include<stdio.h>
#include<malloc.h>
void insert(int d);
void display();
void deletee();
void reverse();
void removeDup();
struct node
{
int data;
struct node *next;
};
struct node *start;

int main()
{
printf("\n\n");
start=NULL;//starting with empty list
insert(10);
insert(10);
insert(10);
insert(15);
insert(15);
insert(15);
insert(23);
insert(15);
insert(23);
insert(15);
insert(77);
insert(99);
insert(99);
insert(99);
printf("\ndisplay() called:\n");
display();
printf("\n\n");

printf("\nremoveDup() called:\n");
removeDup();
display();
printf("\n\n");

printf("\ndeletee() called\n");
deletee();
display();
printf("\n\n");

printf("\nreverse() called\n");
reverse();
display();
printf("\n\n");

return 0;
}

void insert(int d)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=d;
newnode->next=start;
start=newnode;
}
void display()
{
struct node *ptr=start;
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
}
void deletee()
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp=start;
start=start->next;
if(temp!=NULL)
{
free(temp);
}
}

void reverse()
{
struct node *next=(struct node*)malloc(sizeof(struct node));
struct node *result=(struct node*)malloc(sizeof(struct node));
struct node *current=(struct node*)malloc(sizeof(struct node));
next=NULL;
result=NULL;
current=start;
while(current!=NULL)
{
next=current->next;
current->next=result;
result=current;
current=next;
}
start=result;
}


void removeDup()
{
struct node *current=(struct node*)malloc(sizeof(struct node));
current=start;
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp=NULL;
struct node *ptr1, *ptr2;
ptr1=start;
while(ptr1!=NULL )//&& ptr1->next!=NULL)
{
ptr2=ptr1;
while(ptr2->next!=NULL)//->next!=NULL)
{
if(ptr1->data==ptr2->next->data)
{
temp=ptr2->next;
ptr2->next=ptr2->next->next;
free(temp);
}
else
{
ptr2=ptr2->next;
}
}
ptr1=ptr1->next;
}
}
/*

display() called:
99 99 99 77 15 23 15 23 15 15 15 10 10 10


removeDup() called:
99 77 15 23 10


deletee() called
77 15 23 10


reverse() called
10 23 15 77


*/

C code for finding merge point of two linked lists


//finding the merge point of two lists
//list1 contains 10,15,30
//list 2 contains 3,6,9,15,30 such that
//15 in both the lists are stored in same location ie merge point
/*ALGO:
-count the list1 say c1
-count the list2 say c2
-absoulte difference say d
-traverse the longer list by d nodes
-then traverse both the lists simueltaeously
-when two nodes gets equal in address that is point of merge
*/
#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
};


void display(struct node *head1);
int countNode(struct node *head1);
int mergePoint(struct node *head1,struct node *head2,int d);
int main()
{
struct node *head1=(struct node *)malloc(sizeof(struct node));
head1->data=10;
head1->next=NULL;

struct node *head2=(struct node *)malloc(sizeof(struct node));
head2->data=3;
head2->next=NULL;

struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=6;
head2->next=newnode;

newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=9;
head2->next->next=newnode;

newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=15;
head2->next->next->next=newnode;
head1->next=newnode;
printf("head2->next->next->next=newnode=%p\n",head2->next->next->next);
printf("head1->next=%p\n",head1->next);

newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=30;
head1->next->next=newnode;
//head2->next->next->next->next=newnode;
head1->next->next->next=NULL;

display(head1);
printf("\n");
display(head2);
printf("\n");
int c1,c2,d;
c1=countNode(head1);
printf("\n");
c2=countNode(head2);
printf("\n");
if(c1>c2)
{
d=c1-c2;
printf("d=c1=%d\n",d);
}
else
{
d=c2-c1;
printf("d=c2=%d\n",d);
}
int t=0;
t=mergePoint(head1,head2,d);
printf("Merege point is at node:%d\n",t);
return 0;
}
//function to display the nodes
void display(struct node *h)
{
struct node *current=h;
while(current!=NULL)
{
printf("%d ",current->data);
current=current->next;
}

}
//function to count the nodes
int countNode(struct node *h)
{
int count=0;
struct node *current=h;
while(current!=NULL)
{
count=count+1;
current=current->next;
}
printf("size of list1=%d\n",count);

return count;
}
//function to give the merge point
int mergePoint(struct node *head1,struct node *head2,int d)
{
int i;
struct node *current1=head1;
struct node *current2=head2;
for(i=0;i<d;i++)
{
printf("i=%d,current2->data=%d\n",i,current2->data);
current2=current2->next;
}
while(current1!=NULL && current2!=NULL)
{
printf("while=\n");
if(current1==current2)
{
printf("\n\ncurrent1->data====%d\n\n",current1->data);
return current1->data;
}
current1=current1->next;
current2=current2->next;

}

}

/*OUTPUT:
head2->next->next->next=newnode=0x9dc7048
head1->next=0x9dc7048
10 15 30
3 6 9 15 30
size of list1=3

size of list1=5

d=c2=2
i=0,current2->data=3
i=1,current2->data=6
while=
while=
current->data====15
Merege point is at node:15*/

C code for sorted linked list ie inserting in sorted fashion


sorted linked list latest
//insert as first elt
//insert before first elt
//inserting at middle
//inserting at last
//deleting the given node first,middle,last


#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node *start=NULL;

void insert(int d)
{
node *newnode=(node *)malloc(sizeof( node));
newnode->data=d;
newnode->next=NULL;

//insert as first elt
if(start==NULL)
{

//start->next=newnode;
start=newnode;
}
//insert before first elt
if(newnode->data < start->data)
{
newnode->next=start;
start=newnode;
return;
}
//inserting at middle
node* prev; node *ptr;
for(prev=start,ptr=start->next;ptr;prev=ptr,ptr=prev->next)
{
if(newnode->data < ptr->data)
{
prev->next=newnode;
newnode->next=ptr;
return;
}
}
//inserting at last
if(ptr==NULL)
{
prev->next=newnode;
newnode->next=NULL;//must
return;
}
}

void display()
{
node *ptr=start;
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;//must else infinite loop
}
printf("\n");
}

//deleting the given node
void deleteNode()
{
int key;
printf("Enter elt to delete:\n");
scanf("%d",&key);
//deleting the first node
if(key==start->data)
{
node *temp;
temp=start;
start=start->next;
free(temp);
return;
}
//deleting middle node
node *ptr; node *prev;
for(prev=start,ptr=start->next;ptr;prev=ptr,ptr=prev->next)
{
if(ptr->data==key)
{
prev->next=ptr->next;
free( ptr);
return;
}
}
//deleteing last elt
if(ptr==NULL)
{
printf("Ket not present in the LL.\n");
return;
}
if(start==NULL)
{
printf("No elt to del in the LL.\n");
return;
}
}

int main()
{
insert(10);
insert(4);
insert(8);
insert(9);
insert(6);
insert(7);
insert(25);
display();
deleteNode();
display();
deleteNode();
display();
deleteNode();
display();
return 0;
}
/*
4 6 7 8 9 10 25
Enter elt to delete:
7
4 6 8 9 10 25
Enter elt to delete:
4
6 8 9 10 25
Enter elt to delete:
25
6 8 9 10*/

C code for reversing linked list recursively


//linked list implementation in C
//display, reverse recursively
//recursive function to reverse a list
#include<stdio.h>
#include<malloc.h>
void insert(int d);
void display();

struct node
{
int data;
struct node *next;
};
struct node *start;

int main()
{
start=NULL;//starting with empty list
insert(10);//first element
insert(15);
insert(11);
insert(99);//last element

struct node *current1=(struct node*)malloc(sizeof(struct node));
current1=start;
printf("\nOriginal List was:\n");
display();
printf("\nRecursively reversed List is:\n");
revRec(current1);

return 0;
}
void insert(int d)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=d;
newnode->next=start;
start=newnode;
}


void display()
{
struct node *ptr=start;
while(ptr!=NULL)
{
printf("ptr=%d and ptr->data=%d \n",ptr,ptr->data);
ptr=ptr->next;
}
}




void revRec(struct node *current1)
{
if(current1==NULL)//here we can not take current1 and initialized with start otherwise always same conditiuon
return;

else
revRec(current1->next);

printf("current1=%d and current1->data=%d \n",current1,current1->data);

}
/*
Original List was:
ptr=160849976 and ptr->data=99
ptr=160849960 and ptr->data=11
ptr=160849944 and ptr->data=15
ptr=160849928 and ptr->data=10

Recursively reversed List is:
current1=160849928 and current1->data=10
current1=160849944 and current1->data=15
current1=160849960 and current1->data=11
current1=160849976 and current1->data=99
*/
/*
In stack:
10 was stored in lowest address then 15 then 11 and last 99 in higgest addres
when printrd in revRec
printed last entered element ie 99 then 11 then 15 then 10 last
*/

C Code for sorting linked list elements nodes


//linked list implementation in C
//insert at beginning
//displaing elements
//sort the elements in ascending order
#include<stdio.h>
#include<malloc.h>
void insert(int d);
void display();
void sortAscend();

struct node
{
int data;
struct node *next;
};
struct node *start;

int main()
{
start=NULL;//starting with empty list
insert(10);//first element
insert(15);
insert(12);
insert(1);
insert(99);//last element

printf("\nOriginal List was:\n");
display();
sortAscend();

return 0;
}
void insert(int d)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=d;
newnode->next=start;
start=newnode;
}


void display()
{
struct node *ptr=start;
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->next;
}
printf("\n");
}


void sortAscend()
{
struct node *ptr1;
struct node *ptr2;
int temp;
if(start==NULL)
{
printf("List is empty.\n");
}
else
{
for (ptr1=start; ptr1; ptr1=ptr1->next)
{
for (ptr2=ptr1->next; ptr2; ptr2=ptr2->next)
{
if (ptr1->data > ptr2->data)
{
temp = ptr1->data;
ptr1->data = ptr2->data;
ptr2->data = temp;
}
}
}
printf("\nSorted List is:\n");
for (ptr1=start; ptr1!=NULL; ptr1=ptr1->next)
{
printf("%d\t", ptr1->data);
}
}
printf("\n");
}

/*
Original List was:
99 1 12 15 10

Sorted List is:
1 10 12 15 99
*/