Feedback

Hi frns...
The programs posted here are all tested and workng..
So any prob regarding the programs can be posted here...
Also if u want me to post any particular program,  jus type in here..
I will try to post as quickly as possible..
Thank you!!

Tower of Hanoi


Program with recurrsion.. TOWER OF HANOI prob...


#include<stdio.h> 
#include<conio.h>
void towers(int,char,char,char);
int count=0;
main()
{
int n;
clrscr();
printf("\n Enter the no of disks:");
scanf("%d",&n); 
towers(n,'a','c','b');
printf("\nTotal no of moves required is %d",count);
getch();
return 0;
}
void towers(int n,char frompeg,char topeg,char temppeg)
{
if(n==1) 
{
printf("Move disk 1 from peg %c to peg %c\n",frompeg,topeg);
count++;
return;
}
towers(n-1,frompeg,temppeg,topeg);
count++;
printf("Move disk 1 from peg %c to peg %c\n",frompeg,topeg);
towers(n-1,temppeg,topeg,frompeg); 
}

Double linked list

A program on double linked list... To  perform various operations on DLL.

#include"stdio.h"      /*Note tags are to be used for pre-prcessor directives*/
#include"conio.h"
#include"stdlib.h"
struct node
 {
  int info;
  struct node *left;
  struct node *right;
 };
typedef struct node *nodeptr;
nodeptr getnode()
 {
  nodeptr p;
  p=(nodeptr)malloc(sizeof(struct node));
  p->left=NULL;
  p->right=NULL;
  return p;
  }
void freenode(nodeptr p)
 {
  free(p);
 }
nodeptr ins_f(nodeptr list,int ele)
 {
  nodeptr p;
  p=getnode();
  p->info=ele;
  if(list==NULL)
   {
    list=p;
   }
  else
   {
    p->right=list;
    list->left=p;
    list=p;
   }
  clrscr();
  printf("%d element inserted",ele);
  return list;
 }
nodeptr ins_end(nodeptr list,int ele)
 {
  nodeptr p,q;
  p=getnode();
  p->info=ele;
  if(list==NULL)
   {
    list=p;
   }
  else
   {
    for(q=list;q->right!=NULL;q=q->right);
    p->left=q;
    q->right=p;
   }
   clrscr();
   printf("%d element inserted",ele);
   return list;
  }
nodeptr ins_aftx(nodeptr list,int ele,int x)
 {
  nodeptr p,q;
  int flag=0;
  p=getnode();
  p->info=ele;
  if(list==NULL)
   {
    printf("\nNo element X found in the list...!");
    return 0;
   }
  else
   {
    for(q=list;q!=NULL;q=q->right)
      if(q->info==x)
{
flag=1;
break;
}
      if(flag)
       {
(q->right)->left=p;
p->right=q->right;
q->right=p;
p->left=q;
clrscr();
printf("%d inserted into the list after %d",ele,x);
       }
       else printf("%d not found in the list..!",x);
     }
   return list;
  }
void display(nodeptr list)
 {
  nodeptr p;
  if(list==NULL)
   {
    printf("List empty..!");
    return;
   }
  else
   {
   printf("\nLIST ELEMENTS\n-------------");
   for(p=list;p!=NULL;p=p->right)
    {
      printf("\n%d",p->info);
    }
   }
 }
nodeptr del_f(nodeptr list)
 {
  nodeptr p;
  int x;
  if(list==NULL)
   {
    printf("\nInvalid operation..!");
    return 0;
   }
  else
   {
    p=list;
    list=list->right;
    list->left=NULL;
    x=p->info;
    freenode(p);
    printf("%d deleted form list..!",x);
   }
  return list;
 }
nodeptr del_end(nodeptr list)
 {
  nodeptr p,q;
  int x;
  if(list==NULL)
   {
    printf("\nInvalid opertion...!");
    return 0;
   }
  else
   {
    q=NULL;
    for(p=list;p->right!=NULL;p=p->right)
     q=p;
    x=p->info;
    q->right=NULL;
    freenode(p);
   }
  printf("%d deleted from the list..!",x);
  return list;
 }
nodeptr del_b4x(nodeptr list,int x)
 {
  nodeptr p,q,r;
  int y,flag=0;
  if(list==NULL||list->info==x)
   {
    printf("Invalid opertaion..!");
    return 0;
   }
  else
   {
    q=NULL;
    for(p=list;p!=NULL;p=p->right)
     {
     if((list->right)->info==x)
       list=list->right;
     if(p->info==x)
      {
       flag=1;
       break;
      }
     q=p;
     }
     if(flag)
      {
       printf("\nElement %d found in the list!!",x);
r=q->left;
r->right=p;
p->left=r;
y=q->info;
freenode(q);
printf("\n%d deleted form the list..!",y);
      }
     else
      printf("\nElement not found in the list..!");
      return list;
     }
    }
main()
 {
  int ele,c1,c2,x;
  nodeptr list;
  clrscr();
  list=NULL;
  while(1)
   {
     printf("\n1 : Insert node\n2 : Delete node\n3 : Display list\n4 : Exit");
     printf("\nEnter choice : ");
     scanf("%d",&c1);
     switch(c1)
      {
       case 1:printf("\n1 : Insert from front\n2 : Insert from rear\n3 : Insert after element X\n");
     printf("Enter your option : ");
     scanf("%d",&c2);
     printf("Enter the element to be inserted : ");scanf("%d",&ele);
     switch(c2)
      {
case 1:list=ins_f(list,ele);
      break;
case 2:list=ins_end(list,ele);
      break;
case 3:printf("\nEnter the element X : ");scanf("%d",&x);
      list=ins_aftx(list,ele,x);
      break;
}
      break;
       case 2:printf("1 : Delete from front\n2 : Delete from rear\n3 : Delete before element X\n");
     printf("Enter your option : ");scanf("%d",&c2);
     switch(c2)
      {
case 1:list=del_f(list);
      break;
case 2:list=del_end(list);
      break;
case 3:printf("\nEnter the element x : ");
      scanf("%d",&x);
      list=del_b4x(list,x);
      break;
}
     break;
       case 3:display(list);
     break;
       default:exit(0);
      break;
      }
   }
    getch();
    return 0;
 }


Diamond string



This is a C program to print your entered string in DIAMOND shape..... Check it!
The above shown output is expected..


#include"stdio.h" /*Pre processor directives */
#include"conio.h"
main()
 {
  int i,j,l=0,k;
  char str[20];
  void fun(char *);
  clrscr();
  printf("Enter the string : ");
  gets(str);
  for(i=0;str[i]!='\0';i++)
   l++;
  for(i=0;i<l;i++)
   {
    k=l-i;
    for(j=0;j<k;j++)
     printf("  ");
    for(j=0;j<=i;j++)
     {
      fun(&str[j]);
     }
    printf("\n");
    }
   for(i=l-1;i>0;i--)
    {
     k=l-i;
     for(j=0;j<=k;j++)
      printf("  ");
     for(j=i;j>0;j--)
      fun(&str[l-j]);
     printf("\n");
     }
   getch();
   return 0;
  }
void fun(char *p)
 {
   printf("  %c ",*p);
 }