Monday, 9 April 2018

Sparse Matrix Using 2D Array

Sparse Matrix refers to only those matrix whose more than half elements are zero.Here is the program to check your matrix for Sparse Matrix and calculation of zero elements in your matrix

#include<stdio.h>   /*Standard input output pre processor*/
#define MAX 15       /*size of MAX pre defined */

int main()
{
    int array[MAX][MAX],i,j,m,n,count=0;
    printf("\nEnter the no. of rows and columns for your matrix:- ");
    scanf("%d %d",&m,&n);
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%d",&array[i][j]);
            if(array[i][j]==0)
            {
                count++;
            }
        }
    }
    if(count>(m*n)/2)
    {
        printf("\n\nThe above matrix is SPARSE MATRIX");
        printf("\n\tHence their are %d number of zeros",count);
    }
    else
    {
        printf("\nThe above matrix is not a sparse matrix ");
    }
    return 0;
}


Saturday, 7 April 2018

C Programming Hello Linux -







Linux is an open source operating system that is worldwide popular but many of us didn't know how to use it.

So, today am gonna show how to run a simple C Program in Linux.

Wednesday, 4 April 2018

Linked LIst Practice

The program for the same i show you before but this time i use character as my data instead of integers just for the fun.
The program will just polish your skills .

#include<stdio.h>  /* input / output funtion*/
#include<malloc.h> /*dynamic memory allocation*/

struct node   /*structure of the node*/
{
    char data[56];
    struct node *ref;
};

void create(struct node **);  /*function to create linked list*/
void display(struct node *);   /*display the same on the output screen */

int main()
{
    struct node *head=NULL;
     int i;
    printf("\nEnter no. of element for your linked list:- ");
    scanf("%d",&i);
    while(i>0)
    {
        create(&head);
        i--;
    }
    display(head);
    return 0;
}

void create(struct node **q)
{
    struct node *temp ,*r;
    temp=*q;
    if(*q==NULL)
    {
        temp=(struct node *)malloc(sizeof(struct node));
        printf("\n\tEnter value:- ");
        scanf("%s",&temp->data);
        temp->ref=NULL;
        *q=temp;
    }
    else
    {
        r=(struct node *)malloc(sizeof(struct node));
        printf("\n\tEnter value:- ");
        scanf("%s",&r->data);
        r->ref=NULL;
        while(temp->ref!=NULL)
        {
            temp=temp->ref;
        }
        temp->ref=r;
    }
}

void display(struct node *q)
{
    while(q!=NULL)
    {
        printf("\n\t\tOutput:= %s",q->data);
        q=q->ref;
    }
}


Sunday, 1 April 2018

Insertion at mid in Doubly Linked List

If we gonna insertion at mid (at given position) then it is same as singly linked list but not that much simple. Basically, it will adjust the new element in such a way that previous node address and next node address will be hold by the same node.


#include<stdio.h>   /*input / output pre processor*/
#include<stdlib.h>  /*standard library function*/
#include<conio.h>  /*prototype*/
#include<malloc.h> /*dynamic memory allocation*/

struct node    /*structure of the node*/
{
struct node *pref;
int data;
struct node *ref;

};

void create(struct node **);   /*creation of the doubly linked list*/
void display(struct node *);   /*display the same*/
void addmid(struct node **);  /*insert element at the desirable positon*/

void main()
{
struct node *head=NULL;
int i,am,num;
clrscr();
printf("\nEnter no. of element for your node:- ");
scanf("%d",&num);
for(i=0;i<num;i++)
{
create(&head);
}
printf("\nDo you want any insertion at mid(1--> Yes and 0--> No):- ");
scanf("%d",&am);
if(am==1)
{
addmid(&head);
}
display(head);
getch();
}

void create(struct node **q)
{
struct node *temp, *r;
temp=*q;
if(*q==NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
printf("\n\tEnter value:- ");
scanf("%d",&temp->data);
temp->pref=NULL;
temp->ref=NULL;
*q=temp;
}
else
{
r=(struct node *)malloc(sizeof(struct node ));
printf("\n\tEnter value:- ");
scanf("%d",&r->data);
while(temp->ref!=NULL)
{
temp=temp->ref;
}
r->ref=NULL;
r->pref=temp;
temp->ref=r;
}
}

void display(struct node *q)
{
while(q!=NULL)
{
printf("\n\tOutput:- %d",q->data);
q=q->ref;
}
}

void addmid(struct node **q)
{
struct node *temp, *r;
int i=2,j;
r=*q;
printf("\nEnter the position for the insertion:- ");
scanf("%d",&j);
while(i<j)
{
r=r->ref;
i++;
}
temp=(struct node *)malloc(sizeof(struct node));
printf("\n\tEnter value:- ");
scanf("%d",&temp->data);
temp->pref=r;
temp->ref=r->ref;
r->ref->pref=temp;
r->ref=temp;
}


Insertion at first in Doubly Linked List

Basically it's not a new thing that we gonna do , Today we will insert the element at first in Doubly linked list as simple as singly linked list. Nothing to do extra , except few changes.
So, here the code for the same.

#include<stdio.h>    /*input / output pre processor*/
#include<conio.h>  /*prototype */
#include<stdlib.h>  /*standard library function*/
#include<malloc.h> /*dynamic memory allocation */

/*structure of the node */
struct node
{
struct node *pref;
int data;
struct node *ref;
};

void create(struct node **);  /*creates the doubly linked list*/
void display(struct node *);  /*display the same*/
void addfirst(struct node **); /*insert the element at first*/

void main()
{
int i,num,af;
struct node *head=NULL;
clrscr();
printf("\nEnter no. of element you want to insert:- ");
scanf("%d",&num);
for(i=0;i<num;i++)
{
create(&head);
}
printf("\nDo you want any insertion at first(1-->Yes and 0-->No):- ");
scanf("%d",&af);
if(af==1)
{
addfirst(&head);
}
display(head);
getch();
}

void create(struct node **q)
{
struct node *temp, *r;
temp=*q;
if(*q==NULL)
{
temp=(struct node *)malloc(sizeof(struct node ));
printf("\n\tEnter value:- ");
scanf("%d",&temp->data);
temp->pref=NULL;
temp->ref=NULL;
*q=temp;
}
else
{
r=(struct node *)malloc(sizeof(struct node));
printf("\n\tEnter value:- ");
scanf("%d",&r->data);
while(temp->ref!=NULL)
{
temp=temp->ref;
}
temp->ref=r;
r->pref=temp;
r->ref=NULL;
}
}

void display(struct node *q)
{
while(q!=NULL)
{
printf("\n\t\tOutput:- %d",q->data);
q=q->ref;
}
}

void addfirst(struct node**q)
{
struct node *r;
r=(struct node *)malloc(sizeof(struct node));
printf("\n\tEnter value:- ");
scanf("%d",&r->data);
r->pref=NULL;
r->ref=*q;
*q=r;
}


Traverse in Doubly Linked List

Traverse in Doubly Linked List is as simple as Singly Linked List but here a narrow difference is that it will hold the address of previous node as well as the address of next node.
Their nothing more to remind just need to understand the logic and code will be simple as per your understanding.


#include<stdio.h>    /*input / output pre processor*/
#include<conio.h>  /*prototype */
#include<stdlib.h>  /*standard library function*/
#include<malloc.h> /*malloc function for dynamic memory allocation*/

/*structure of the node */
struct node
{
struct node *pref;
int data;
struct node *ref;
};

void create(struct node **);   /*function for the creation of doubly linked list*/
void display(struct node *);   /*display the same*/

void main()
{
int i,num;
struct node *head=NULL;
clrscr();
printf("\nEnter no. of element you want to insert:- ");
scanf("%d",&num);
for(i=0;i<num;i++)
{
create(&head);
}
display(head);
getch();
}

void create(struct node **q)
{
struct node *temp, *r;
temp=*q;
if(*q==NULL)
{
temp=(struct node *)malloc(sizeof(struct node ));
printf("\n\tEnter value:- ");
scanf("%d",&temp->data);
temp->pref=NULL;
temp->ref=NULL;
*q=temp;
}
else
{
r=(struct node *)malloc(sizeof(struct node));
printf("\n\tEnter value:- ");
scanf("%d",&r->data);
while(temp->ref!=NULL)
{
temp=temp->ref;
}
temp->ref=r;
r->pref=temp;
r->ref=NULL;
}
}

void display(struct node *q)
{
while(q!=NULL)
{
printf("\n\t\tOutput:- %d",q->data);
q=q->ref;
}
}