Monday, 18 June 2018

File Input/Output File Scan

By using File Input/Output , we gonna count:-
No. of characters
No. of blanks
No. of tabs
No. of lines

So, here the code for the above.

/*Pre Processors */
#include<stdio.h>
#include<conio.h>

int main()     /*Main function*/
{
    FILE *fp;
    char ch;
    clrscr();
    int nol=0, not=0, nob, noc=0;
    fp=fopen("EMPLOYEE.TXT","r");
    while(1)
    {
        ch=fgetc(fp);
        if(ch==EOF)
        break;
        noc++;
        if(ch==' ')
        nob++;
        if(ch=='\n')
        nol++;
        if(ch=='\t')
        not++;
    }
    fclose(fp);
    printf("\nNumber of characters:- %d",noc);
    printf("\nNumber of blanks:- %d",nob);
    printf("\nNumber of tabs:- %d",not);
    printf("\nNumber of lines:- %d",nol);
    getch();
    return 0;
}

Monday, 11 June 2018

Program to read an existing file using File Handling

File Handling is concept that is used to read or write a file in your hard disk using some lines of code.
Here we gonna do the same using C langugae

/*Pre Processor used throughout the program*/
#include<stdio.h>
#include<conio.h>

int main()
{
    FILE *fp;
    char ch;
    clrscr();
    fp=fopen("Employee.txt","r");
    while(1)
    {
        ch=fgetc(fp);
        if(ch==EOF)
        break;
        printf("%c",ch);
    }
    printf("\n");
    fclose(fp);
    getch();
    return 0;

}


Sunday, 10 June 2018

Program to Write a Text File using FIle Handling

File Input/Output is used to store or fetch desirable date using C language. It is basically come in use when we want to store data and make sure it's privacy.

So, Here a Simple program for the creation of a Text format file using C language that comes under File Input/Output.

/*Pre Processors used throughout the program*/
 #include<stdio.h>
 #include<conio.h>

 int main()
 {
    FILE *fp;                     /*Creation of a file type pointer*/
    char ch='Y';                
    struct emp                    /*Structure of a user defined data type i.e emp*/
    {
        char name[40];
        int age;
        float bs;
    };
    clrscr();
    struct emp e;
    fp=fopen("Employee.txt","w");             /*create a txt format file using fopen*/
    if(fp==NULL)
    {
        puts("\nCannot open file");
    }
    while(ch=='Y')
    {
        printf("\nEnter your data");
        printf("\nEnter Name:- ");
        scanf("%s",&e.name);
        printf("\nEnter Age:- ");
        scanf("%d",&e.age);
        printf("\nEnter Salary:- ");
        scanf("%f",&e.bs);

        fprintf(fp,"Name:- " );
        fprintf(fp,"%s \n",e.name);
        fprintf(fp,"Age:- ");
        fprintf(fp,"%d \n",e.age);
        fprintf(fp,"Salary:- ");
        fprintf(fp,"%f \n\n",e.bs);

        printf("\nDo you want to add another data(Y/N):- ");
        fflush(stdin);
        ch=getche();
    }
    fclose(fp);                             /*closing of file using fclose*/
    return 0;
 }



And the file that store hold the data of your program is below:-



Sunday, 13 May 2018

Stack Representation using Linked List

stack is  basically a program which works on the concept of LIFO i.e, last in first out which means last element will be deleted first.
The is all about the implementation of stack in linked list.

/*All the Pre Processors*/
#include<stdio.h>
#include<conio.h>
#include<stdio.h>

#define MAX 25

struct stack                           /*Structure of Stack*/
{
    int stack[MAX];
    int top;
};

typedef struct stack NODE;

void push(NODE *);              /*Function to push an element in Stack*/
void pop(NODE *);                /*Function to delete or pop an element*/

void main()
{
    NODE *ps;
    int i,p;
    clrscr();
    printf("\nEnter no. of element you want to push:- ");
    scanf("%d",&i);
    while(i>0)
    {
        push(ps);
        i--;
    }
    printf("\nDo you want to pop element(1-->Yes and 0-->No):- ");
    scanf("%d",&p);
    if(p==1)
    {
        pop(ps);
    }
    getch();
}

void push(NODE *pu)
{
    int item;
    if(pu->top==MAX-1)
    {
        printf("\nSTACK IS FULL");
    }
    else
    {
        printf("\nEnter value:- ");
        scanf("%d",&item);
        pu->stack[++pu->top]=item;
    }
}

void pop(NODE *po)
{
    int item;
    if(po->top==-1)
    {
        printf("\nStack is Empty");
    }
    else
    {
        item=po->stack[po->top--];
        printf("\nElement Deleted :- %d",item);
    }
}


Saturday, 12 May 2018

Completed Insertion of Singly Linked List

 I made so many programs  on Insertion for Singy Linked separately but this time i gonna combine all those insertion cases into a single program to give you a  better understanding about it....

 /*All the preprocessors*/
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>


struct node                                    /*Structure of Singly Linked List*/
{
    int data;                                                   
    struct node *ref;

};

void create(struct node **);                /*Create a Singly Linked List*/
void display(struct node *);                /*Display a Singly Linked List*/
void addfirst(struct node **);             /*Add first in Singly Linked List*/
void addlast(struct node **);             /*Add last in Singly Linked List*/
void addmid(struct node **);            /*Add middle in Singly Linked List*/

void main()
{
    struct node *head=NULL;
    int i,af,al,am;
    clrscr();
    printf("\nEnter no. of element you want to insert :-  ");
    scanf("%d",&i);
    while(i>0)
    {
        create(&head);
        i--;
    }
    printf("\nDo you want to insert at first (1-->Yes and 0-->No):- ");
    scanf("%d",&af);
    if(af==1)
    {
        addfirst(&head);
    }
    printf("\nDo you want to insert at last (1-->Yes and 0-->No):- ");
    scanf("%d",&al);
    if(af==1)
    {
        addlast(&head);
    }
    printf("\nDo you want to insert 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("\nEnter value:- ");
        scanf("%d",&temp->data);
        temp->ref=NULL;
        *q=temp;
    }
    else
    {
        r=(struct node *)malloc(sizeof(struct node));
        printf("\nEnter value :- ");
        scanf("%d",&r->data);
        r->ref=NULL;
        while(temp->ref!=NULL)
        {
            temp=temp->ref;
        }
        temp->ref=r;
    }
}

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

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

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

void addmid(struct node **q)
{
    int i,pos;
    struct node *temp, *r;
    temp=*q;
    r=(struct node *)malloc(sizeof(struct node));
    printf("\nEnter the position for insertion:- ");
    scanf("%d",&pos);
    printf("\nEnter value:- ");
    scanf("%d",&r->data);
    r->ref=NULL;
    for(i=2;i<pos;i++)
    {
        temp=temp->ref;
        if(temp==NULL)
        break;
    }
    if(temp!=NULL)
    {
        r->ref=temp->ref;
        temp->ref=r;
        printf("\n\n\n\nALL THE DATA INSERTED SUCCESSFULLY");
    }

}


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


Monday, 26 March 2018

Stack representation Using Array

Stack is a basic concept in Data Structure , It totally work on-
L: lAST
I: IN
F: FIRST
O: OUT

That means the last inserted element would be deleted first . So, here the program for the same

/* pre processor used throughout the program*/
#include<stdio.h>
#include<conio.h>
#define MAX 50

/*Some variable declared that we gonna used in the program*/
int stack[MAX],n,top,vk,i;

void push();     /*function for push the value in stack*/
void pop();       /*function for pop value from stack*/
void dsp();       /*function to display element in the stack*/

void main()
{
int po,pu;
clrscr();
top=-1;
printf("\nSpecify size for your stack:- ");
scanf("%d",&n);
printf("\n\nEnter no. of element you want to push:- ");
scanf("%d",&pu);
for(i=0;i<pu;i++)
{
push();
}
printf("\n\nDo you want to pop element (1--> Yes and 0-->No):- ");
scanf("%d",&po);
if(po==1)
{
pop();
}
dsp();
getch();
}

void push()
{
if(top>=n-1)
{
printf("\nStack is over flow");
}
else
{
printf("\n\tEnter a value to push:- ");
scanf("%d",&vk);
top++;
stack[top]=vk;
}
}

void pop()
{
if(top<=-1)
{
printf("\nStack is empty");
}
else
{
printf("\nPopped value:- %d",stack[top]);
top--;
}
}

void dsp()
{
if(top>=0)
{
for(i=top;i>=0;i--)
printf("\nOutput:- %d",stack[i]);
}
else
{
printf("\nSTACK IS EMPTY");
}
}


Saturday, 24 March 2018

Deletion at last in Singly Linked List

The deletion of linked list is come in 3 case :-
deletion at first
deletion at last
deletion at given point

But this time we are working on deletion at last .
So, here the code for the same hope so it will help you to understand it easily.

/*pre processor*/
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>

/*Structure of node */
struct node
{
int data;
struct node *ref;
};

/*function that we gonna use throughout the program */
void create(struct node **);
void display(struct node *);
void delast(struct node**);

void main()
{
struct node *head=NULL;
int num,i,l;
clrscr();
printf("\n\tEnter no. of element you want to insert:- ");
scanf("%d",&num);
for(i=0;i<num;i++)
{
create(&head);
}
printf("\nDo You want any deletion at last(1-->Yes and 0-->No):- ");
scanf("%d",&l);
if(l==1)

delast(&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("\nEnter value:- ");
scanf("%d",&temp->data);
temp->ref=NULL;
*q=temp;
}
else
{
r=(struct node *)malloc(sizeof(struct node));
printf("\nEnter value:- ");
scanf("%d",&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\tOUTPUT:- %d",q->data);
q=q->ref;
}
}

void delast(struct node **q)
{
struct node *temp=*q;
struct node *t;
if(temp->ref==NULL)
{
free(temp);
*q=NULL;
}
else
{       while(temp->ref!=NULL)
{
t=temp;
temp=temp->ref;
}

free(t->ref);
t->ref=NULL;
}
}