Thursday, September 16, 2010

C Program for insertion and deletion operations in a "Doubly Linked List"

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
    struct node *left;
    int data;
    struct node *right;
};

void d_append(struct node **s,int num)
{
   struct node *r,*q=*s;
   if(*s==NULL)
   {
     /* Create a New Node */
     (*s)=(struct node*)malloc(sizeof(struct node));
     (*s)->left=NULL;
     (*s)->data=num;
     (*s)->right=NULL;
   }
   else
   {
      /* Traverse the Linked List till the last node is reached */
      while(q->right != NULL)
      q=q->right;

      /* Add a new node at the end */
      r=(struct node*)malloc(sizeof(struct node));
      r->right=NULL;
      r->left=q;     //*s
      q->right=r;
      r->data=num;
   }
}
void d_display(struct node *q)
{
  printf("\n");
  /* Traverse the entire linked list */
  while(q != NULL)
  {
    printf(" %d\t",q->data);
    q=q->right;
  }
}

void d_addatbeg(struct node **s,int num)
{
    struct node *q;
    /* Create a new node */
    q=(struct node*)malloc(sizeof(struct node));

    /* Assign data and pointer to the new node */
    q->left=NULL;
    q->data=num;
    q->right=(*s);
    /* Make new node the head node */
    (*s)->left=q;
    (*s)=q;
}
void d_addafter(struct node *q,int loc,int num)
{
   struct node *temp;
   int i;

   /* Skip tp desired position */
   for(i=0;i<loc;i++)
   {
     q=q->right;
     /* If end of list is encountered   */
     if(q==NULL)
     {
 printf("\n There are less than %d elements ",loc);
 return;
     }
   }

   /* Insert new node */
     q=q->left;
     temp=(struct node*)malloc(sizeof(struct node));
     temp->data=num;
     temp->left=q;
     temp->right=q->right;
     temp->right->left=temp;
     q->right=temp;
}

void d_delete(struct node **s,int num)
{
   struct node *q=*s;

   /* Traverse the entire linked list */
   while(q != NULL)
   {
     /* If node to be deleted is found */
     if(q->data==num)
     {
 /* If node to be deleted is the first node */
 if(q==*s)
 {
    (*s)=(*s)->right;
    (*s)->left=NULL;
 }
 else
 {
  /* If node to be deleted is the last node */
  if(q->right==NULL)
    q->left->right=NULL;

    else
  /* If node to be deleted ia any intermediate node */
    {
 q->left->right=q->right;
 q->right->left=q->left;
    }
    free(q);
  }
  return;
  }
  q=q->right;   /* Go to next node */
  }
  printf("\n%d not found ",num);
}

void main()
{
    struct node *head;
    clrscr();
    head=NULL;
    d_append(&head,1);
    d_append(&head,2);
    d_append(&head,3);
    d_append(&head,4);
    d_append(&head,5);
    printf("\n\n The Linked List is :\n");
    d_display(head);
    printf("\n\n After insertion at the beginning ");
    d_addatbeg(&head,100);
    d_display(head);
    printf("\n\n Insertion after a specified node ");
    d_addafter(head,2,200);
    d_display(head);
    printf("\n\n After Deletion of an element");
    d_delete(&head,200);
    d_display(head);
    getch();
}

Sunday, September 12, 2010

C Program for various patterns of Pyramid

1)
#include<stdio.h>
#include<conio.h>
void main()
{
int c,k,i,j,n,v=1;
clrscr();
printf("enter the limit.....\n");
scanf("%d",&n);
c=n-1;
for(i=0;i<n;i++)
{
for(j=c;j>0;j--)
{
printf(" ");
}
c--;
for(k=0;k<=i;k++)
{
printf("%d ",v);
}
v++;
printf("\n");
}
getch();
}




2)



#include<stdio.h>
#include<conio.h>
void main()
{
    int n,i,j,c,k;
    printf(" Enter the limit ");
    scanf("%d",&n);
    c=n;
    for(j=0;j<n;j++)
    {
    for(i=c;i>0;i--)
    {
       printf("*");
    }


    printf("\n");
    c--;
    for(k=0;k<=j;k++)
    {
    printf(" ");
    }
    }
    getch();
    }


3)



#include<stdio.h>
#include<conio.h>
void main()
{
  int row,col,n;
  printf(" Enter the height ");
  scanf("%d",&n);
  for(row=0;row<n;row++)
  {
  for(col=0;col<row+1;col++)
  {
  printf("*");
  }
  printf("\n");
  }
  getch();
}


4)

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m,n;
printf(" Enter the limit ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
printf(" ");
for(j=i+1;j<2*i+2;j++)
printf("*");
for(m=2*i;m>i;m--)
printf("*");
printf("\n");
}
}


5)

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m,n;
printf(" Enter the limit ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
printf(" ");
for(j=i+1;j<2*i+2;j++)
printf("%d",j%10);
for(m=2*i;m>i;m--)
printf("%d",m%10);
printf("\n");
}
}

C Program to Check whether a number is an Armstrong Number or not

#include<stdio.h> 
#include<conio.h> 
#include<math.h> 
void main() 

int n,sum=0,rem=0,cube=0,n1;
clrscr();
printf("\n\tEnter a number :" );
scanf("%d",&n);
n1=n;
while(n!=0)
{
rem=n%10;
cube=pow(rem,3);
sum=sum+cube;
n=n/10;
}
if(sum==n1)
printf("\n\tIt is an Armstrong's Number ");
else
printf("\n\tIt is not an Armstrong's Number ");
getch(); 

C Program to check whether a number is a Perfect Number or not

#include<stdio.h>
int main(){
  int n,i=1,sum=0;
  printf("\nEnter a number:-");
  scanf("%d",&n);
  while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
  }
  if(sum==n)
printf("\nThe nnumber %d is a perfect number",i);
  else
      printf("\nThe number  %d is not a perfect number",i);
  return 0;
}

C Program to print Krishnamurthy Number

#include<stdio.h>
#include<conio.h>
void main()
{
int temp,n,fact,num,sum=0,i;
clrscr();
printf("Enter the Number: ");
scanf("%d",&num);
temp=num;
sum=0;
while(temp>0)
{
n=temp%10;
fact=1;
for(i=1;i<=n;i++)
fact=fact*i;
sum=sum+fact;
temp=temp/10;
}
if(sum==num)
printf("\nKrishnamurthy's Number :");
else
      printf("\n Not a Krishnanurthy's Number ");
getch();
}















C Program to print Towers Of Hanoi

#include<stdio.h>
#include<conio.h>
long step;
void Hanoi(int n,char from,char to,char spare)
{
   if(n>1)
      Hanoi(n-1,from,spare,to);
      printf(" Step %ld : move #%d %c-->%c\n",++step,n,from,to);

   if(n>1)
      Hanoi(n-1,spare,to,from);
}
void main()
{
   int n;
   printf(" Enter the number of blocks in the tower ");
   scanf("%d",&n);
   step=0;
   Hanoi(n,'A','B','C');
   getch();
}

Saturday, September 4, 2010

C Program to count the number of nodes in the singly linked list

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
      int data;
      struct node *link;
};
void append(struct node**,int);
void display(struct node*);
int count(struct node *);
void main()
{
     struct node *head;


     clrscr();
     head=NULL;
     append(&head,5);
     append(&head,6);
     append(&head,7);
     append(&head,8);
     display(head);

     printf("\n No of nodes in the list is %d",count(head));

     getch();
}
void append(struct node **q,int num)
{
     struct node *temp,*r;
     temp=*q;

     if(temp==NULL)  /* If list is empty , create first node */
     {
            temp=(struct node*)malloc(sizeof(struct node));
            temp->data=num;
            temp->link=NULL;
            *q=temp;
     }
     else
     {
          temp=*q;
          while(temp->link !=NULL)
          temp=temp->link;

          r=(struct node*)malloc(sizeof(struct node));
          r->data=num;
          r->link=NULL;
          temp->link=r;
     }
}
void display(struct node *start)
{
     printf("\n");
     printf(" The Linked List is :\n");
     /* Traverse the entire linked list */
     while(start != NULL)
     {
          printf("   %d",start->data);
          start=start->link;
     }

}

int count(struct node *q)
{
     int c=0;
     /* traverse the entire linked list */
     while(q!=NULL)
     {
        q=q->link;
        c++;
     }
     return c;
}

C Program to delete an element from a singly linked list

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
      int data;
      struct node *link;
};
void append(struct node**,int);
void display(struct node*);
void del(struct node **,int);
void main()
{
     struct node *head;

     int n;
     clrscr();
     head=NULL;
     append(&head,5);
     append(&head,6);
     append(&head,7);
     append(&head,8);
     display(head);

     printf(" \n Enter the number you want to delete ");

     scanf("%d",&n);

     del(&head,n);
     display(head);
     getch();
}
void append(struct node **q,int num)
{
     struct node *temp,*r;
     temp=*q;

     if(temp==NULL)  /* If list is empty , create first node */
     {
            temp=(struct node*)malloc(sizeof(struct node));
            temp->data=num;
            temp->link=NULL;
            *q=temp;
     }
     else
     {
          temp=*q;
          while(temp->link !=NULL)
          temp=temp->link;

          r=(struct node*)malloc(sizeof(struct node));
          r->data=num;
          r->link=NULL;
          temp->link=r;
     }
}
void display(struct node *start)
{
     printf("\n");
     printf(" The Linked List is :\n");
     /* Traverse the entire linked list */
     while(start != NULL)
     {
          printf("   %d",start->data);
          start=start->link;
     }

}

void del(struct node **q,int no)
{
     struct node *old,*temp;
     temp=*q;

     while(temp != NULL)
     {
         if(temp->data==no)
        { /* If number to be selected is the first node in the list */
         if(temp==*q)
            *q=temp->link;
         /* Deletes the intermediate nodes from the linked list */
         old->link=temp->link;
         /* Free the memory occupied by the node */
         free(temp);
         return;
      }
        /* Traverse the linked list till the last node is reached */
        else
        {
          old=temp;       /* old points to previous node */

          temp=temp->link;  /* go to the next node */

        }
        }
      printf(" \n Element %d not found ",no);
}

C Program to insert a node at the beginning of a singly linked list

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
      int data;
      struct node *link;
};
void append(struct node**,int);
void display(struct node*);
void add_at_beg(struct node**,int );
void main()
{
     struct node *head;

     int n;
     clrscr();
     head=NULL;
     append(&head,5);
     append(&head,6);
     append(&head,7);
     append(&head,8);
     display(head);

     printf(" \n Enter the number you want to insert ");

     scanf("%d",&n);

     add_at_beg(&head,n);
     display(head);
     getch();
}
void append(struct node **q,int num)
{
     struct node *temp,*r;
     temp=*q;

     if(temp==NULL)  /* If list is empty , create first node */
     {
            temp=(struct node*)malloc(sizeof(struct node));
            temp->data=num;
            temp->link=NULL;
            *q=temp;
     }
     else
     {
          temp=*q;
          while(temp->link !=NULL)
          temp=temp->link;

          r=(struct node*)malloc(sizeof(struct node));
          r->data=num;
          r->link=NULL;
          temp->link=r;
     }
}
void display(struct node *start)
{
     printf("\n");
     printf(" The Linked List is :\n");
     /* Traverse the entire linked list */
     while(start != NULL)
     {
          printf("   %d",start->data);
          start=start->link;
     }

}

void add_at_beg(struct node**q,int no)
{
  struct node *temp;
  temp=(struct node*)malloc(sizeof(struct node));
  temp->data=no;
  temp->link=*q;
  *q=temp;
}

Friday, September 3, 2010

C Program to insert an element after a specified node

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
     int data;
     struct node *link;
};
void append(struct node**,int);
void display(struct node*);
void add_after(struct node*,int,int);
void main()
{
    struct node *head;

    int n,lo;
    clrscr();
    head=NULL;
    append(&head,5);
    append(&head,6);
    append(&head,7);
    append(&head,8);
    display(head);

    printf(" \n Enter the number you want to insert ");

    scanf("%d",&n);

    printf(" \n Enter the location after which you want to insert ");

    scanf("%d",&lo);
    add_after(head,lo,n);
    display(head);
    getch();
}
void append(struct node **q,int num)
{
    struct node *temp,*r;
    temp=*q;

    if(temp==NULL)  /* If list is empty , create first node */
    {
         temp=(struct node*)malloc(sizeof(struct node));
         temp->data=num;
         temp->link=NULL;
         *q=temp;
    }
    else
    {
        temp=*q;
        while(temp->link !=NULL)
        temp=temp->link;

        r=(struct node*)malloc(sizeof(struct node));
        r->data=num;
        r->link=NULL;
        temp->link=r;
    }
}
void display(struct node *start)
{
    printf("\n");
    printf(" The Linked List is :\n");
    /* Traverse the entire linked list */
    while(start != NULL)
    {
        printf("   %d",start->data);
        start=start->link;
    }

}
void add_after(struct node*q,int loc,int num)
{
struct node *temp,*r;
int i;

temp=q;
for(i=1;i<=loc-1;i++)
{
temp=temp->link;
if(temp==NULL)
{

printf("\n There are less than %d elements in the list",loc);
return;
}
}
r=(struct node*)malloc(sizeof(struct node));
r->data=num;
r->link=temp->link;
temp->link=r;
}


C Program to find the largest number between three numbers

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


void main()
{
     int a,b,c;
     clrscr();
     printf("\n\n Enter the values of three numbers \n\n");
     scanf("%d%d%d",&amp;a,&amp;b,&amp;c);
     printf("\n The values of three numbers are \n");
     printf(" a = %d\n",a);
     printf(" b = %d\n",b);
     printf(" c = %d\n",c);
     if(a&gt;b)
     {
        if(a&gt;c)
          printf("\ta = %d is larger ",a);
        else
          printf("\tc = %d is larger ",c);
     }
     else
     {
         if(c&gt;b)
            printf("\tc = %d is larger",c);
         else
            printf("\tb = %d is larger",b);
     }
     getch();
}

Thursday, September 2, 2010

C Program to find fibonacci series using "RECURSION"

#include<stdio.h>
#include<conio.h>
long fibonacci(int);
void main()
{
    int i,n;
    clrscr();
    printf(" Enter the number of terms required in the series ");
    scanf("%d",&n);
    printf("\n Series is :\n");

    for(i=1;i<=n;i++)
    {
    printf(" %ld",fibonacci(i));
    }

    getch();
}
long fibonacci(int n)
{
  if(n==1 || n==2)
  return 1;
  else
  return (fibonacci(n-1)+fibonacci(n-2));
}

C Program to reverse a " Linked List "

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


struct node
{
    int data;
    struct node *link;
};
void append(struct node**,int);
void display(struct node*);
void reverse(struct node **);
void main()
{
   struct node *head;
   clrscr();
   head=NULL;
   append(&head,5);
   append(&head,6);
   append(&head,7);
   append(&head,8);
   display(head);
   reverse(&head);
   display(head);
   getch();
}

void append(struct node **q,int num)
{
   struct node *temp,*r;
   temp=*q;

   if(temp==NULL)  /* If list is empty , create first node */
   {
       temp=(struct node*)malloc(sizeof(struct node));
       temp->data=num;
       temp->link=NULL;
       *q=temp;
   }
   else
   {
      temp=*q;
      while(temp->link !=NULL)
      temp=temp->link;

      r=(struct node*)malloc(sizeof(struct node));
      r->data=num;
      r->link=NULL;
      temp->link=r;
   }
}
void display(struct node *start)
{
   printf("\n");
   printf(" The Linked List is :\n");
   /* Traverse the entire linked list */
   while(start != NULL)
   {
      printf("   %d",start->data);
      start=start->link;
   }
}
void reverse(struct node **x)
{
  struct node *q,*r,*s;
  q=*x;
  r=NULL;

  /* Traverse the enitre linked list */
  while(q != NULL)
  {
      s=r;
      r=q;
      q=q->link;
      r->link=s;
  }
  *x=r;
}