Monday, 17 August 2020

Write a C Program for Single Link List to perform Insert, Delete & Print data.

C Program for Single Link List to perform Insert, Delete & Print

 

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
int sd;
struct Node *next;
}
node;

void insert(node ** head, int sd)
{
node ** ptr2=head;
if(*ptr2==NULL){
(*ptr2)=(node*)malloc(sizeof(node));
(*ptr2)->sd=sd;
(*ptr2)->next=NULL;
}else{
while(*ptr2 != NULL){
ptr2 = &(*ptr2)->next;
}
(*ptr2)=(node*)malloc(sizeof(node));
(*ptr2)->sd=sd;
(*ptr2)->next=NULL;
}
}

void remove(node ** head,int sd){
node ** ptr2;
for(ptr2= head; *ptr2 != NULL; ptr2 = &(*ptr2)->next)
{
if((*ptr2)->sd == sd)
{
*ptr2 = (*ptr2)->next;
break;
}
}
}

void printLista (node * head) {
printf(“Lista: “);
node * ptr =head;
while(ptr!=NULL){
printf(“%d “, ptr->sd);
ptr=ptr->next;
}
printf(“\n”);
}
 

Write a C program for Creation of Binary Tree.

                              C program for Creation of Binary Tree

 

 

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

struct node

{

int data;

struct node *left,*right;

};

struct node *root;

void insert(int x)

{

   struct node *p,*previous,*current;

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

   if(p==NULL)

   {

     printf("\n Out of memory");

   }

   p->data=x;

   p->left=NULL;

   p->right=NULL;

   if(root=NULL)

   {

     root=p;

     return;

   }

   previous=NULL;

   current=root;

   while(current!=NULL)

   {

       previous=current;

       if(p->data<current->data)

            current=current->left;

       else

            current=current->right;

            }

     if(p->data<previous->data)

          previous->left=p;

     else

 previous->right=p;

}

void inorder(struct node *t)

{

  if (t!=NULL)

  {

  inorder(t->left);

  printf("\n %5d",t->data);

  inorder (t->right);

  }

}

void del(int x)

{

   int tright=0,tleft=0;

   struct node *ptr=root;

   struct node *parent=root;

   struct node *t1=root;

   struct node *temp=root;

   while(ptr!=NULL&& ptr->data!=x)

   {

      parent=ptr;

      if (x<ptr->data)

            ptr=ptr->left;

      else

           ptr=ptr->right;

   }

   if (ptr==NULL)

   {

     printf("\n Delete element not found");

     return ;

   }

   else if(t1->data==x && (t1->left ==NULL || t1->right==NULL))

          if(t1->left==NULL)

             t1=t1->right;

          else

             t1=t1->left;

   else if (ptr->left==NULL)

     if (x<parent->data)

       parent->left=ptr->right;

     else

       parent->right=ptr->right;

   else if (ptr->right==NULL)

     if (x<parent->data)

       parent->left=ptr->left;

     else

       parent->right=ptr->left;

   else

   {

   temp=ptr;

   parent=ptr;

   if((ptr->left)>=(ptr->right))

   {

      ptr=ptr->left;

      while(ptr->right!=NULL)

      {

           tright=1;

           parent=ptr;

           ptr=ptr->right;

      }

      temp->data=ptr->data;

      if(tright)

           parent->right=ptr->left;

      else

           parent->left=ptr->left;

    }

 else

    {

     ptr=ptr->right;

     while (ptr->left!=NULL)

     {

           tleft=1;

           parent=ptr;

           ptr=ptr->left;

      }

      temp->data=ptr->data;

      if(tleft)

           parent->left=ptr->right;

      else

           parent->right=ptr->right;

    }

    free(ptr);

  }

}

 

void main()

{

int op,n,srchno;

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

root->data=30;

root->right=root->left=NULL;

clrscr();

do

{

    printf("\n 1.Insertion");

    printf("\n 2.Deletion");

    printf("\n 3.Inorder");

    printf("\n 4.Quit");

    printf("\n Enter your choice\n");

    scanf("%d",&op);

 

  switch (op)

   {

    case 1: printf("\n Enter the element to insert\n");

              scanf("%d",&n);

              insert(n);

              break;

    case 2: printf("\n Enter the element to be deleted\n");

              scanf("%d",&srchno);

              del(srchno);

              break;

    case 3: printf("\n The inorder elements are\n");

              inorder(root);

              getch();

              break;

    default: exit(0);

   }

  }while(op<4);

  getch();

 

  }

Saturday, 21 April 2018

How to Show Snow in the Background

How to Show Snow in the Background ?

 

HTML FILE

<html>
<head>
<title>Snowy Background</title>
</head>

<body>

    <canvas id="skyfall"></canvas>
   
</body>
</html>

 

CSS FILE

.body {  background-color: black;    }  

JAVASCRIPT FILE

window.onload = function()

{
 
  // get the canvas and context and store in vars
  var canvas = document.getElementById('skyfall');
  var ctx = canvas.getContext('2d');
 
  // set canvas dimensions to window height and width
  var W = window.innerWidth;
  var H = window.innerHeight;
  canvas.width = W;
  canvas.height = H;
 
  // generate the snowflakes and apply attributes
  var mf = 170; // max number of flakes
  var flakes = [];
 
  // loop through the empty flakes and apply attributes
for(var i = 0; i < mf; i++){
        flakes.push({
            x: Math.random()*W, //set width of flake to random nr between 0 and 1 * width of screen
            y: Math.random()*H, //set height of flake to random nr between 0 and 1 * height of screen
            r: Math.random()*5+2, //set radius between 2 and 5
            d: Math.random() + 1
        })
    }
 
  // draw flakes onto canvas
  function drawFlakes() {
    ctx.clearRect(0, 0, W, H); // clear rectangle
    ctx.fillStyle = "white";
    ctx.beginPath();
    for(var i = 0; i < mf; i++) {
      var f = flakes[i];
      ctx.moveTo(f.x, f.y);
      ctx.arc(f.x, f.y, f.r, 0, Math.PI * 2, true);
    }
    ctx.fill();
    moveFlakes();
  }
 
  // animate the flakes
  var angle = 0;
 
  // move flakes
  function moveFlakes() {
    angle += 0.01;
    for(var i = 0; i < mf; i++) {
     
      // store current flake
      var f = flakes[i];
     
      // update X and Y coordinate of each snowflake
      f.y += Math.pow(f.d, 2) + 1;
      f.x += Math.sin(angle) * 2;
     
      // if the snowflake reaches the bottom, send a new one to the top
      if(f.y > H) {
        flakes[i] = {
          x: Math.random() * W,
          y: 0,
          r: f.r,
          d: f.d
        };
      }
    }
  }
  setInterval(drawFlakes, 35);
}


Sunday, 11 June 2017

Write a C program for Binary Search ?

C Program for Binary Search

#include<conio.h>
#include<stdio.h>

 main()
{
   int c, first, last, middle, n, search, array[100];
   printf("Enter number of elements\n");
   scanf("%d",&n);
   printf("Enter %d integers\n", n);
   for ( c = 0 ; c < n ; c++ )
      scanf("%d",&array[c]);
   printf("Enter value to find\n");
   scanf("%d",&search);
   first = 0;
   last = n - 1;
   middle = (first+last)/2;
   while( first <= last )
   {
      if ( array[middle] < search )
         first = middle + 1;   
      else if ( array[middle] == search )
      {
         printf("%d found at location %d.\n", search, middle+1);
         break;
      }
      else
         last = middle - 1;
      middle = (first + last)/2;
   }
   if ( first > last )
      printf("Not found! %d is not present in the list.\n", search);
   return 0;  
}

Write a C Program for Bubble Sort ?


/* Bubble sort code */

#include<stdio.h>

main()
{
   int array[100], n, c, d, swap;

   printf("Enter number of elements\n");
   scanf("%d", &n);

   printf("Enter %d integers\n", n);

   for ( c = 0 ; c < n ; c++ )
       scanf("%d", &array[c]);

   for ( c = 0 ; c < ( n - 1 ) ; c++ )
   {
      for ( d = 0 ; d < n - c - 1 ; d++ )
      {
          if ( array[d] > array[d+1] ) /* For decreasing order use < */
          {
             swap = array[d];
             array[d] = array[d+1];
             array[d+1] = swap;
          }
      }
   }

   printf("Sorted list in ascending order:\n");

   for ( c = 0 ; c < n ; c++ )
       printf("%d\n", array[c]);

   return 0;
}

Write a C Program for Finding a Leap Year.

C Program for Finding Leap Year.


#include <stdio.h>
#include <conio.h>

main()
{
   int year;

   printf("Enter a year to check if it is a leap year\n");
   scanf("%d", &year);

   if ( year%400 == 0)
      printf("%d is a leap year.\n", year);
   else if ( year%100 == 0)
      printf("%d is not a leap year.\n", year);
   else if ( year%4 == 0 )
      printf("%d is a leap year.\n", year);
   else
      printf("%d is not a leap year.\n", year);

   return 0;
}

Write a Program for compare Strings by using If in C ?

Compare Strings by using If

#include<stdio.h>
#include<string.h>

main()
{
   char a[100], b[100];

   printf("Enter the first string\n");
   gets(a);

   printf("Enter the second string\n");
   gets(b);

   if( strcmp(a,b) == 0 )
      printf("Entered strings are equal.\n");
   else
      printf("Entered strings are not equal.\n");

   return 0;
}