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


Friday, 23 March 2018

Deletion in Singly linked list at given point

In a linear data structure linked list is a basic program while study about Data Structure.
 So, this time we gonna do "Deletion in linked list at given position/ point"

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

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

/*function is created to handle program*/
void create(struct node **);
void display(struct node *);
void dellast(struct node**, int );

void main()
{
struct node *head=NULL;
int num,i,l,pos;
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 (1-->Yes and 0-->No):- ");
scanf("%d",&l);
if(l==1)
{       printf("\nEnter the position for the deletion :- ");
scanf("%d",&pos);
dellast(&head,pos);
}
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 dellast(struct node **q, int pos)
{
int i;
struct node *next;
struct node *temp=*q;
if(*q==NULL)
{
return;
}

if(pos==0)
{
*q=temp->ref;
free(temp);
return;
}
for( i=1;temp!=NULL && i<pos-1;i++)
{
temp=temp->ref;
}
if(temp==NULL || temp->ref==NULL)
{
return;
}
next=temp->ref->ref;
free(temp->ref);
temp->ref=next;

}


Complete insertion Program for Singly Linked List (All cases)

In this program we gonna use all case of insertions in a singly linked list. In our previous programs , we do the same but separately here we gonna do all together.

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

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

/*Functions used throughout the program*/
void create(struct node **);
void display(struct node *);
void addfirst(struct node **);
void addlast(struct node **);
void addmid(struct node **);

void main()
{
struct node *head=NULL;
int i,num;
clrscr();
printf("\nEnter no. of elements for your node:- ");
scanf("%d",&num);
for(i=0;i<num;i++)
{
create(&head);
}
addfirst(&head);
addlast(&head);
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 *p)
{
while(p!=NULL)
{
printf("\nOUTPUT:- %d",p->data);
p=p->ref;
}
}

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

}

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

}

void addmid(struct node **q)
{
int i,position;
struct node *temp, *r;
r=(struct node *)malloc(sizeof(struct node));
if(r==NULL)
{
printf("\nUnable to allocate memory.");

}
else
{
printf("\nEnter the position:- ");
scanf("%d",&position);
printf("\n\nEnter value to be inserted at mid:- ");
scanf("%d",&r->data);
r->ref=NULL;
temp=*q;
for(i=2;i<=position-1;i++)
{
temp=temp->ref;
if(temp==NULL)
break;
}
if(temp!=NULL)
{
r->ref=temp->ref;
temp->ref=r;
printf("\nDATA INSERTED SUCCESSFULL");

}
else
{
printf("\nUNABLE TO INSERT DATA");
}
}
}


Thursday, 22 March 2018

Program to find greater no. out of 3

Program is simply created to find out the greatest  no.  out of three using C language.
so, here the code for the same.

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

void main()
{
        int a,b,c;
        clrscr();
        printf("\nEnter first element:- ");
        scanf("%d",&a);
   
        printf("\nEnter second element:- ");
        scanf("%d",&b);

        printf("\nEnter third element:- ");
        scanf("%d",&c);

        if(a>b&&a>c)
        {
                  printf("%d is greater among all of them",a);
         }
   
          if(b>c&&b>a)
          {
                  printf("%d is greater among all of them",b);
           }
     
           if(c>b&&c>a)
           {
                   printf("%d is greater among all of them",c);
            }
            getch();
}


Data Manipulation Language Short Note

                                                  DML(Data Manipulation Language)

DML deals with the record stored in the particular database.To know it in a better way some of the DDL Commands are below.

Commands:-

Insert
      Syntex:- Insert into <table_name> values(<all_attribute_records> );

Update
       Syntex:- Update <table_name> set <attribute_name>=<condition> where <attribute_name>=<>;

Delete
       Syntex:- Delete From <table_name> where <attribute_name>=<>;

Tuesday, 20 March 2018

Deletion of first node in Singly Linked List

In a simple linked list i.e is a part of a linear data structure we will know about how to delete the element in a linked list at a first node.
So, here the code for the deletion of first node in the linked list.


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

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

/*funtions to create a linked list , display , and delete the first node */
void create(struct node **);
void display(struct node *);
void delfirst(struct node **);

void main()
{
struct node *head=NULL;
int i,num, dlf;
clrscr();
printf("\n\tEnter no. of elements:- ");
scanf("%d",&num);
for(i=0;i<num;i++)
{
create(&head);
}
printf("\n\tDo you want any deletion at first (1--> YES And 0-->NO):- ");
scanf("%d",&dlf);
if(dlf==1)
{
delfirst(&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\n\tEnter value:- ");
scanf("%d",&temp->data);
temp->ref=NULL;
*q=temp;
}
else
{
r=(struct node *)malloc(sizeof(struct node));
printf("\n\tEnter 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 delfirst(struct node **q)
{
struct node *dlf;
int temp;
dlf=*q;
temp=dlf->data;
*q=dlf->ref;
free(dlf);
}


Array Program for Beginners

Array is a very important concept that we use in C . Basically it is a sequence of a data items of homogeneous values. Array as 1D is a base of whole array.
For sake of beginners here a simple program on array.

/* Pre- Processor used throughout the program*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 15   /* Length specified for MAX*/

void main()
{
int i, num, array[MAX];
clrscr();
printf("\nEnter no of element you want to insert in array: ");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\nEnter a value:- ");
scanf("%d",&array[i]);
}
for(i=0;i<num;i++)
{
printf("\nOutput:- %d",array[i]);
}
getch();
}


Saturday, 17 March 2018

Average of all the elements in Array

Array is a must known concept through array we can perform many task or program.
So , an another program that will help you to understand array more easily.

/* Pre-Processors*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 25 /* Length of MAX*/


void main()
{
float array[MAX];
float i, num, sum=0;
float avg=0;
clrscr();
printf("\nEnter no. of Element for your array:- ");
scanf("%f",&num);
for(i=0;i<num;i++)
{
printf("\nEnter value:- ");
scanf("%f",&array[i]);

}
for(i=0;i<num;i++)
{
sum=sum+array[i];
}
avg=sum/num;
printf("\nAverage of the given values is %f",avg);
getch();
}



Friday, 16 March 2018

Bubble Sort in C

Bubble sort is a concept that we use to sorting of elements in ascending order . In C programming , their are many more technique that will do the same but according to the complexity of the program it is better upto some extends

/* Pre-Processors*/
#include<stdio.h>
#include<conio.h>
#define MAX 15 /* Length of MAX*/

void main()
{
    int array[MAX];
    int i, j, n, temp;

    printf("Enter no. of elements for array: ");
    scanf("%d", &n);
    printf("\nEnter the elements of array: \n");
    for (i = 0; i < n; i++)
    {
        scanf("%d", &array[i]);
    }

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < (n - i - 1); j++)
        {
            if (array[j] > array[j + 1])
            {
                temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
        }
    }
    printf("Array in Ascending...\n");
    for (i = 0; i < n; i++)
    {
        printf("%d\n", array[i]);
    }
    getch();
}


Matrix Multiplication in C

In C programming matrix is created by using 2D and 3D array but today we are working on matrix multiplication in C language.
Here the program to do Matrix Multiplication in C.

/* Pre-Processors */
#include <stdio.h>
#include<conio.h>
#define MAX 15 /*Length of MAX*/
int main()
{
  int m, n,i,j,k, sum = 0;
  int first[MAX][MAX], second[MAX][MAX], multiply[MAX][MAX];
  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");
 for (i = 0; i < m; i++)
    for (j = 0; j < n; j++)
      scanf("%d", &first[i][j]);
  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d", &m, &n);
printf("Enter the elements of second matrix\n");
 for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
        scanf("%d",&second[i][j]);
  for (i = 0; i < m; i++) {
      for (j = 0; j < n; j++) {
        for (k = 0; k < n; k++) {
          sum = sum + first[i][k]*second[k][j];
        }
 multiply[i][j] = sum;
        sum = 0;
      }
    }

    printf("Product of entered matrices:-\n");
 for (i = 0; i < m; i++) {
      for (j = 0; j < n; j++)
        printf("%d\t", multiply[i][j]);
 printf("\n");
    }
 getch();
  return 0;
}

Addition of two matrix in C

In C Programming addition of matrix is all a very significant program while we are learning array in C.
So, here we gonna do Addition of matrix.

/*Pre-Processors */
#include<stdio.h>
#include<conio.h>
#define MAX 15 /* Length MAX*/
int main()
{
   int a[MAX][MAX], b[MAX][MAX], c[MAX][MAX];
   int i, j;
   int m,n;
   clrscr();
   printf("Enter no. of rows: ");
   scanf("%d",&m);
   printf("Enter no. of columns: ");
   scanf("%d",&n);

   printf("Enter elements of 1st matrix\n");
   for(i=0; i<m; ++i)
    for(j=0; j<n; ++j)
    {
       printf("Enter a%d%d: ", i+1, j+1);
       scanf("%d", &a[i][j]);
    }
   printf("Enter elements of 2nd matrix\n");
   for(i=0; i<m; ++i)
    for(j=0; j<n; ++j)
    {
       printf("Enter b%d%d: ", i+1, j+1);
       scanf("%d", &b[i][j]);
    }
   for(i=0; i<m; ++i)
    for(j=0; j<n; ++j)
    {
       c[i][j] = a[i][j] + b[i][j];
    }
   printf("\nSum Of Matrix:\n");

   for(i=0; i<m; ++i)
    for(j=0; j<n; ++j)
    {
       printf("%2d", c[i][j]);

       if(j==1)
          printf("\n");
    }
    getch();
return 0;
}

Matrix in C

In C language we can create a matrix by using 2D array. Array is a must known concept that we should know as it is used in many programs.

/* Pre- Processors*/
#include<stdio.h>
#include<conio.h>
#define MAX 15 /* Length of MAX*/

int main()
{
int j,i,m,n;
int a[MAX][MAX];
clrscr();
printf("\t\t\t\t----------------\n");
printf("\t\t\t\t--Array-Matrix--\n");
printf("\t\t\t\t----------------\n");
printf("Enter no. of elements for your Matrix:-");
printf("\n\nEnter no. of rows: ");
scanf("%d",&m);
printf("\nEnter no. of columns: ");
scanf("%d",&n);
printf("\nEnter values:-\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
getch();
return 0;
}