#include<stdio.h>
struct node
{
int data;
struct node* next;
};
void addatbeg(struct node**,int);
void append(struct node**,int);
void display(struct node*);
void delete(struct node**,int num);
void count(struct node*);
void insert(struct node**,int,int);
void reverse(struct node**);
void reversing(struct node*);
void mid(struct node*);
main()
{
struct node *head;
head = NULL;
addatbeg(&head,10);
addatbeg(&head,20);
addatbeg(&head,30);
display(head);
append(&head,60);
append(&head,70);
printf("\n");
display(head);
delete(&head,60);
printf("\n");
printf("after deletion the linked list be a\n");
display(head);
printf("\n");
printf("the number of nodes will be\n");
count(head);
insert(&head,333,20);
printf("\n");
display(head);
reverse(&head);
printf("\n");
printf("this is reverse the nodes\n");
display(head);
reversing(head);
printf("\n");
printf("this is reverse printing\n");
display(head);
printf("\n");
printf("the mid element is\n");
mid(head);

}
void addatbeg(struct node **q,int num)
{
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->data = num;
temp->next = *q;
*q = temp;
}
void display(struct node *q)
{
while(q!=NULL)
{
printf("%d ",q->data);
q = q->next;
}
}
void append(struct node**q,int num)
{
struct node *temp,*r;
if(q==NULL)
{
temp = (struct node*)malloc(sizeof(struct node));
temp->data = num;
temp->next = *q;
*q = temp;
}
else
{
temp = *q;
while(temp->next!=NULL)
temp = temp->next;
r = (struct node*)malloc(sizeof(struct node));
r->data = num;
r->next = NULL;
temp->next = r;
}
}
void delete(struct node **q,int num)
{
struct node* temp,*old;
temp = *q;
while(temp!=NULL)
{
if(temp->data == num)
{
if(temp == *q)
{
*q = NULL;
free(temp);
}
else
{
old->next = temp->next;
free(temp);
}
}
else
{


old = temp;
temp = temp->next;
}

}
}
void count(struct node*q)
{
int c=0;
while(q!=NULL)
{
c++;
q = q->next;
}
printf("the nuber of nodes = %d\n",c);
}
void insert(struct node**q,int num,int n)
{
struct node *temp,*r;
temp = *q;
while(temp!=NULL)
{
if(temp->data == n)
{
r = (struct node*)malloc(sizeof(struct node));
r->data = num;
r->next = temp->next;
temp->next = r;
}
temp = temp->next;
}
}
void reverse(struct node**q)
{
struct node *temp,*prev=NULL,*rev;
temp = *q;
while(temp!=NULL)
{
rev = temp;
temp = temp->next;
rev->next = prev;
prev = rev;
}
*q = rev;
}
void reversing(struct node*q)
{
if(q==NULL)
{
reversing(q->next);
printf("%d ",q->data);
}
}
void mid(struct node*q)
{
int m=0;
if(q->next==NULL)
{
printf("only one node is there\n");
}
if(q->next->next==NULL)
{
printf(" two nodes is there\n");
}
while( (q->next!=NULL) && (q->next->next!=NULL))
{
m++;
q = q->next;
}
printf("%d\n",m);
}
-------------------------------------------------------------------------------------------------------------------------------------------------
30 20 10
30 20 10 60 70
after deletion the linked list be a
30 20 10 70
the number of nodes will be
the nuber of nodes = 4
30 20 333 10 70
this is reverse the nodes
70 10 333 20 30
this is reverse printing
70 10 333 20 30
the mid element is
3

0 comments:


Inline functions :
#include
using namespace std;
class A
{
public:
void fun();
};
inline void A::fun()
{
cout&lt;&lt;"this is inline function\n";
}
main()
{
A a;
a.fun();
}
Op :
this is inline function.
#include
using namespace std;
inline int add(int x,int y)
{
int r;
r = x + y;
}
main()
{
cout&lt;
}
Op :
30

0 comments:

4.2 Difference between Structure and Union



#include<stdio.h>

union un{

        int i;

        char ch;

        float f;

}

main()

{

        union un u1;

        u1.i = 10;

        u1.ch = 'a';

        u1.f = 1.9999;

        printf("%d %c %f ",u1.i,u1.ch,u1.f);

}


Op :

1073740985 ¹ 1.999900

0 comments:

3.4 Difference between passing structure by value and by pointer

By Value :



#include<stdio.h>

struct node

{
        int date;
        int num;
        char name;
};
void fun(struct node n);

main()
{
        struct node n;
        fun(n);
}
void fun(struct node n1)
{
        n1.date = 10;
        n1.num  = 99;
        n1.name = 'a';
        printf("%d %d %c\n",n1.date,n1.num,n1.name);
        printf("size of node = %d\n",sizeof(struct node));

}

op:

10 99 a
size of node = 12
Note : passing a structure it takes 12 bytes
 ------------------------------------------------------------------------------------------------------------------------------------
 By Pointer :


#include<stdio.h>

struct node

{

        int date;

        int num;

        char name;

};

void fun(struct node *n);



main()

{

        struct node n;

        fun(&n);

}

void fun(struct node *n1)

{

        n1->date = 10;

        n1->num  = 99;

        n1->name = 'a';

        printf("%d %d %c\n",n1->date,n1->num,n1->name);

        printf("size of node = %d\n",sizeof(n1));



}


Op :

10 99 a

size of node = 4


Note : passing a pointer it takes only 4 bytes.

------------------------------------------------------------------------------------------------------------------------------------ 

Differences :

1 Structure passing through a pointer faster than passing through a value.

2 Structure passing through a pointer we saves the memory. Because structure passing through a pointer it takes only 4 bytes in above program.The same program strucutre passing through a value it takes 12 bytes.
    

0 comments:

3.3 Passing Structure by Pointer



#include<stdio.h>
struct node
{
        int date;
        int num;
        char name;
};
void fun(struct node *n);

main()
{
        struct node n;
        fun(&n);
}
void fun(struct node *n1)
{
        n1->date = 10;
        n1->num  = 99;
        n1->name = 'a';
        printf("%d %d %c\n",n1->date,n1->num,n1->name);
        printf("size of node = %d\n",sizeof(n1));

}




Op :

10 99 a

size of node = 4
 

0 comments: