Tuesday, November 29, 2022

Calculate time for a Program

 #include<conio.h>

#include<stdio.h>

#include<time.h>

#include<dos.h>

void main()

{


int a[10][10],b[10][10],c[10][10],i,j,r,l;

time_t start, end;

double tot_time;

clrscr();

start=clock();

printf("enter no. of rows=");

scanf("%d",&r);

printf("enter no. of columns=");

scanf("%d",&l);

for(i=0;i<r;i++)

for(j=0;j<l;j++)

scanf("%d",&a[i][j]);

for(i=0;i<r;i++)

for(j=0;j<l;j++)

scanf("%d",&b[i][j]);

for(i=0;i<r;i++)

for(j=0;j<l;j++)

c[i][j]=a[i][j]+b[i][j];

printf("%d", c[i][j]);

delay(50):

printf("\n");

end=clock();

tot_time=((double) (end-start))/CLOCKS_PER_SEC;

printf("'In time=%f",tot_time);

getch();

}


Insertion Sort C Program

 /* Insertion Sort *

#include<stdio.h>

#include<conio.h>


void insertion(int x[],int n)

{

int i,j,temp;

for(i=0;i<n;i++)

{

temp=x[i];


for(j=i-1;j>=0;j--)

{

if(temp<x[j])

x[j+1]=x[j];

else

break;

}


x[j+1]=temp;

}}

void main()

{

int x[10],n,i;

clrscr();

printf("Enter the number of elements: ");

scanf("%d",&n);

printf("\nEnter the elements: \n");

for(i=0;i<n;i++)

scanf("%d",&x[i]);

insertion(x,n);

printf("\nThe sorted output:\n");

for(i=0;i<n;i++)

printf("\n%d",x[i]);


getch();

}


Tuesday, November 22, 2022

Program to implement queue using array

 /* Program to implement queue using array */

#include <stdio.h>

#include <conio.h>

#define MAX 100


int q[MAX + 1], front = 0, rear = 0;


void main ( )

{

clrscr();

void create(),traverse(),insert(),delet();

create ( );

traverse ();

insert ( );

printf("\n After insert an element");

traverse();

delet ( );

printf("\nAfter deletion");

traverse ( );

getch ( );

}


void create ( )

{

char ch;

front=1;

do

{

rear++;

printf ("\nInput element in queue:\n");

scanf ("%d", & q[rear]);

printf ("Press <Y/N> for more element");

ch = getch ( );

}

while (ch=='Y');

}


void traverse ( )

{

int i;

printf ("\nelements in the Queue are:\n");

for (i=front; i<=rear;++i)

printf ("%d\n", q[i]);

}


void insert ( )

{

int m;

if (rear == MAX)

{

printf ("Queue is overflow \n");

return;

}

printf ("\nInput new element to insert\n");

scanf ("%d", &m);

rear++;

q[rear]=m;

}


void delet( )

{

if (front==0)

{

printf ("Queue is underflow\n");

return;

}

if (front==rear)

{

q[front] = '\0';

front = rear = 0;

}

else

{

q[front] = '\0';

front++;

}

}


Implementation of stack by Array

 /* Implementation of the stack by Array */

#include <stdio.h>

#include <conio.h>

#define MAX 50

int stack [MAX+1], top = 0;

void main ( )

{

clrscr();

void create ( ), traverse ( ), push ( ), pop ( );

create ( );

printf("\n Stack is :\n");

traverse ( );

push ( );

printf("After Push an element the stack is:\n");

traverse ( );

pop ( );

printf("After pop the element the stack is:\n");

traverse ( );

getch ( );

}

void create ( )

{

char ch;

do

{

top ++;

printf ("Input Element");

scanf ("%d", &stack[top]);

printf ("Press <Y> for more element \n");

ch = getch ( );

}

while (ch=='Y');

}

void traverse ( )

{

int i;

for (i=top; i>0; --i)

printf ("%d\n", stack[i]);

}

void push ( )

{

int m;

if (top==MAX)

{

printf ("Stack is overflow");

return;

}

printf ("Input New Element to Insert");

scanf ("%d", &m);

top++;

stack[top]=m;

}

void pop ( )

{

if (top==0)

{

printf ("Stack is underflow\n");

return;

}

stack[top]='\0';

top--;

}


Tuesday, November 1, 2022

'C' Program for insertion & deletion in Doubly Linked List

#include <stdio.h>

#include<stdlib.h>

struct node 

{

    struct node *lpt;

    struct node *rpt;

    int info;

};

struct node *first;

int main()

{

    int s;

    void create();

    void inbeg();

    void inend();

    void inloc();

    void delbeg();

    void delend();

    void delgiv();

    while(1)

    {

        printf("\n1. Create");

        printf("\n2. Insertion Beginning");

        printf("\n3. Insertion at end");

        printf("\n4. Insertion at given location");

        printf("\n5. Deletion at the beginning");

        printf("\n6. Deletion at the end");

        printf("\n7. Deletion at given location");

        printf("\n8. Exit");

        printf("\nEnter Your Choice:");

        scanf("%d",&s);

        

        switch(s)

        {

            case 1: create();

                    break;

            case 2: inbeg();

                    break;

            case 3: inend();

                    break;

            case 4: inloc();

                    break;

            case 5: delbeg();

                    break;

            case 6: delend();

                    break;

            case 7: delgiv();

                    break;

            case 8: exit(0);

            default: printf("\nEnter correct value");

        }

    }

    return 0;

}

void create()

{

    char ch;

    struct node *ptr,*cpt;

    ptr=(struct node *)malloc(sizeof(struct node));

    printf("\nEnter the node information:");

    scanf("%d",&ptr->info);

    ptr->lpt=NULL;

    first=ptr;

    

    do{

        cpt=(struct node *)malloc(sizeof(struct node));

    printf("\nEnter the node information:");

    scanf("%d",&cpt->info);

    ptr->rpt=cpt;

    cpt->lpt=ptr;

    ptr=cpt;

    printf("\nPress 'y' for more nodes");

    scanf("%s",&ch);

    }

    while(ch=='y');

    ptr->rpt=NULL;

    

    ptr=first;

    while(ptr!=NULL)

    {

        printf("\t%d",ptr->info);

        ptr=ptr->rpt;

    }

}

void inbeg()

{

    struct node *ptr;

    ptr=(struct node *)malloc(sizeof(struct node));

    printf("\nEnter the node information:");

    scanf("%d",&ptr->info);

    ptr->rpt=first;

    first->lpt=ptr;

    first=ptr;

    

    ptr=first;

    while(ptr!=NULL)

    {

        printf("\t%d",ptr->info);

        ptr=ptr->rpt;

    }

    

}

void inend()

{

    struct node *ptr,*cpt;

    ptr=(struct node *)malloc(sizeof(struct node));

    printf("\nEnter the node information:");

    scanf("%d",&ptr->info);

    cpt=first;

    while(cpt->rpt!=NULL)

    {

        cpt=cpt->rpt;

    }

    cpt->rpt=ptr;

    ptr->lpt=cpt;

    ptr->rpt=NULL;

    

    ptr=first;

    while(ptr!=NULL)

    {

        printf("\t%d",ptr->info);

        ptr=ptr->rpt;

    }

}

void inloc()

{

    int loc;

    struct node *ptr,*cpt,*tpt;

    ptr=(struct node *)malloc(sizeof(struct node));

    printf("\nEnter the node information:");

    scanf("%d",&ptr->info);

    cpt=first;

    printf("/n Enter Location:");

    scanf("%d",&loc);

    

    while(cpt->info!=loc)

    {

        cpt=cpt->rpt;

    }

    tpt=cpt->rpt;

    cpt->rpt=ptr;

    ptr->lpt=cpt;

    ptr->rpt=tpt;

    tpt->lpt=ptr;

    

    ptr=first;

    while(ptr!=NULL)

    {

        printf("\t%d",ptr->info);

        ptr=ptr->rpt;

    }

}

void delbeg()

{

    struct node *ptr;

    ptr=first;

    first=ptr->rpt;

    ptr->lpt=NULL;

    free(ptr);

    

    ptr=first;

    while(ptr!=NULL)

    {

        printf("\t%d",ptr->info);

        ptr=ptr->rpt;

    }

}

void delend()

{

    struct node *ptr,*cpt;

    ptr=first;

    while(ptr->rpt!=NULL)

    {

        cpt=ptr;

        ptr=ptr->rpt;

    }

    cpt->rpt=NULL;

    ptr->lpt=NULL;

    free(ptr);

    

    ptr=first;

    while(ptr!=NULL)

    {

        printf("\t%d",ptr->info);

        ptr=ptr->rpt;

    }

}

void delgiv()

{

    int loc;

    struct node *ptr,*cpt,*tpt;

    cpt=first;

    printf("\n Enter Location:");

    scanf("%d",&loc);

     while(cpt->info!=loc)

    {

        cpt=cpt->rpt;

    }

    ptr=cpt->lpt;

    tpt=cpt->rpt;

    ptr->rpt=tpt;

    tpt->lpt=ptr;

    cpt->lpt=NULL;

    cpt->rpt=NULL;

    free(cpt); 

    ptr=first;

    while(ptr!=NULL)

    {

        printf("\t%d",ptr->info);

        ptr=ptr->rpt;

    }

}