Friday, 29 June 2012

IMPLEMENTATION OF BRESENHAM’S LINE DRAWING

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
            int gd=DETECT,gm;
            int x0,y0,x1,y1,dx,dy,p0,s,t,color;
            clrscr();
            printf("Enter the starting coordinates(x0,y0):\n");
            scanf("%d%d",&x0,&y0);
            printf("Enter the ending coordinates(x1,y1):\n");
            scanf("%d%d",&x1,&y1);
            printf("Enter integer to select color:");
            scanf("%d",&color);

            initgraph(&gd,&gm,"C:/BC/BGI");
            if(x1<x0)
            {
             t=x0;x0=x1;x1=t;
             t=y0;y0=y1;y1=t;
            }         
            dx=x1-x0;
            dy=y1-y0;
            p0=(2*dy)-dx;

            putpixel(x0,y0,color);
            color=color+10;
            while(x0<x1)
            {
               if(p0<0)
              {
                x0=x0+1;
                p0=p0+(2*dy);
              }
              else
              {
               x0=x0+1;
               y0=y0+1;
              p0=p0+(2*dy)-(2*dx);
            }
             putpixel(x0,y0,color);
             color=color+10;
            }
            getch();
}

OUTPUT:
Enter the starting coordinates(x0,y0):0 0
Enter the ending coordinates(x1,y1):100 100
Enter integer to select color:6
           


Sunday, 17 June 2012

Singly linked list


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
};
void main()
{
struct node *head,*tp,*temp;
int ch,key,value;
clrscr();
head = NULL;
do
{
printf("1.Insert @ first\t 2.Insert @ last\t 3.Insert @ middle\n");
printf("4.Deletion\t 5.Display");
printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: {
            printf("\nenter a no to insert @ first:");
            temp=(struct node*)malloc(sizeof(struct node));
            scanf("%d",&value);
            temp->data=value;
            temp->next=NULL;
            if(head==NULL)
            {
            head=temp;
            }
            else
            {
            temp->next=head;
            head=temp;
            }}
            break;
case 2: {
            printf("\nenter a no to insert @ last:");
            temp=(struct node*)malloc(sizeof(struct node));
            scanf("%d",&value);
            temp->data=value;
            temp->next=NULL;
            if(head==NULL)
            {
             head=temp;
            }
            else
            {
             tp=head;
             while(tp->next!=NULL)
             {
             tp=tp->next;
             }
             tp->next=temp;
            }}
            break;
case 3: {
            printf("\nEnter a no to find:");
            scanf("%d",&key);
            printf("\nenter a no to insert @ middle:");
            temp=(struct node*)malloc(sizeof(struct node));
            scanf("%d",&value);
            temp->data=value;
            temp->next=NULL;
            if(head==NULL)
            head=temp;
            else
            {
             tp=head;
             while(tp!=NULL && tp->data!=key)
             tp=tp->next;
             if(tp!=NULL)
             {
             temp->next=tp->next;
             tp->next=temp;
             }
             else
             printf("\nElement not found...");
            }}
            break;
case 4: {
            printf("\nEnter a no to delete:");
            scanf("%d",&key);
            if(head==NULL)
            printf("\nlist is empty");
            else
            {
            tp=head;
            while(tp!=NULL && tp->data!=key)
            {
            temp=tp;
            tp=tp->next;
            }
            if(tp!=NULL)
            {
            if(tp==head)
            {
            head=head->next;
            free(tp);
            }
            else
            {
            temp->next=tp->next;
            free(tp);
            } }
            else
            printf("\nelement not found");
            }}
            break;
case 5: {
            if(head!=NULL)
            tp=head;
            while(tp!=NULL)
            {
            printf("%d\t",tp->data);
            tp=tp->next;
            }
            printf("\n");
            }break;
default:printf("\nenter correct choice");
            break;
}
}while(ch<=5);
getch();
}

Shell sort


#include<stdio.h>
#include<conio.h>
void main()
{
 int a[20],n,i; void shell(int a[],int n);
 clrscr();
 printf("\n Enter the size of the array ");
 scanf("%d",&n);
 printf("\n Enter the elements: ");
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 shell(a,n);
 printf("\n Elements after sorting:");
 for(i=0;i<n;i++)
  printf("%d\t",a[i]);
 getch();
}
void shell(int a[],int n)
{
 int j,p,k,temp;
 for(k=n/2;k>0;k=k/2)
 {
  for(p=k;p<n;p++)
  {
   temp=a[p];
   for(j=p; j>=k && a[j-k]>temp;j=j-k)
    a[j]=a[j-k];
    a[j]=temp;
  }
 }
}

Sequential search


#include<stdio.h>
#include<conio.h>
int seqsearch(int k[],int n,int target)
{
 int i=0;
 while(i<n)
 {
  if(target==k[i])
   return i;
  i++;
 }
 return (-1);
}
void main()
{
 int k[10],n,target,i;
 clrscr();
 printf("\n Enter the size of the array ");
 scanf("%d",&n);
 printf("\n Enter the array values ");
 for(i=0;i<n;i++)
  scanf("%d",&k[i]);
 printf("\n Enter the element to find ");
 scanf("%d",&target);
 printf("\n The element %d is found in %d position",target,seqsearch(k,n,target));
 getch();
}

Implementation of queue


#include<stdio.h>
#include<conio.h>
struct queue
{
  int a[10],front,rear,size;
};
void main()
{
 struct queue *q;
 int ch,i;
 q->front=0;
 q->rear=-1;
 clrscr();
 printf("\n\nenter the size:");;
 scanf("%d",&q->size);
 printf("\nQueue operations:");
 //printf("\n1.enqueue \n2.dequeue \n3.display");
 do
 {
 printf("\n1.enqueue \n2.dequeue \n3.display");
 printf("\n\nenter choice:");
 scanf("%d",&ch) ;
 switch(ch)
 {
 case 1: {
             printf("\nenter the element:");
             if(q->rear<q->size-1)
             {
              q->rear++;
              scanf("%d",&q->a[q->rear]);
             }
             else
             {
             printf("\n\nqueue overflow");
             }
             }break;
  case 2: {
               if(q->front<=q->rear)
               {
                printf("\n\ndequeued element:");
                printf("%d",q->a[q->front]);
                q->front++;
               }
               else
               {
               printf("\n\nqueue underflow:");
               }}break;
  case 3:   {
               printf("\n\nelements are:");
               for(i=q->front;i<=q->rear;i++)
               {
               printf("%d\t",q->a[i]);
               }}break;
   default:printf("\n\nenter correct choice");
               break;
  }
  }while(ch<4);
  }




Queue using linked list


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
};
void main()
{
struct node *front,*rear,*tp,*temp;
int ch,key,value;
clrscr();
front = rear = NULL;
do
{
printf("1.Insert @ last\t ");
printf("2.Delete @ first\t 3.Display");
printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:  {
             printf("\nenter a no to insert @ last:");
             temp=(struct node*)malloc(sizeof(struct node));
             scanf("%d",&value);
             temp->data=value;
             temp->next=NULL;
             if(rear==NULL&& front==NULL)
             {
             rear=temp;
             front=temp;
             }
             else
             {
              tp=rear;
              while(tp->next!=NULL)
              {
              tp=tp->next;
              }
              tp->next=temp;
              }}
             break;
 case 2: {
              if(front==NULL)
              printf("\n list is empty");
              else
              {
              tp=front;
              front=front->next;
              free(tp);
              }
             }break;
 case 3: {
             if(rear==NULL)
             printf("\nlist is empty");
             else
             {
             tp=front;
             printf("\nvalues are:\n");
             while(tp!=NULL)
             {
             printf("%d\t",tp->data);
             tp=tp->next;
             }
             }
             break;
default: printf("\nenter correct choice");
             break;
}}
}while(ch<4);
getch();
}