5/01/2009

C Program Tower Of Hanoi


/*C Program Tower Of Hanoi*/
#include<stdio.h>
#include<conio.h>
void TOH(int,char,char,char);
void main()
{
    int n;

    printf("Enter number of disks:\n");
    scanf("%d",&n);
    printf("Tower of Hanoi for %d disk:\n",n);
    TOH(n,'a','b','c');

}
void TOH(int n,char a,char b,char c)
{
    if(n<=0)
        printf(" wrong i/p \n");
    else if(n==1)
        printf(" move disk from tower %c to %c \n",a,c);
    else
    {
        TOH(n-1,a,c,b);
        TOH(1,a,b,c);
        TOH(n-1,b,a,c);
    }
}
Out Put:
Enter number of disks:
3
Tower of Hanoi for 3 disk:
 move disk from tower a to c
 move disk from tower a to b
 move disk from tower c to b
 move disk from tower a to c
 move disk from tower b to a
 move disk from tower b to c
 move disk from tower a to c

C Program Stack Operation


/*C Program Stack Operation*/
#include<stdio.h>
#include<conio.h>
#define SIZE 5
int top=-1,S[SIZE];
void push()
{
    int item;
    if(top==SIZE-1)
        printf("stack overflow \n");
    else
    {
        printf(" enter the element \n");
        scanf("%d",&item);
        top=top++;
        S[top]=item;
    }
}
void pop()
{
    int x;
    if(top==-1)
    {
        printf("stack is underflow!\n");
        return;
    }
    x=S[top];
    printf("poped element=%d \n",x);
    top--;
}
void display()
{
    int i;
    if(top==-1)
    {
        printf(" stack is underflow!\n");
        return;
    }
    printf(" stack contents are:\n");
    for(i=0; i<=top; i++)
    {
        printf("%d \n",S[i]);
    }
}
void main()
{
    int ch;

    for(;;)
    {
        printf("1.PUSH\t2.POP\t3.DISPLAY\t4.EXIT\n");
        printf("enter the choice: \n");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:
            push();
            break;
        case 2:
            pop();
            break;
        case 3:
            display();
            break;
        default:
            exit(0);
        }

    }
}
Out Put:
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
1
 enter the element
1
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
1
 enter the element
2
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
1
 enter the element
3
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
1
 enter the element
4
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
1
 enter the element
5
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
1
stack overflow
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
3
 stack contents are:
1
2
3
4
5
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
2
poped element=5
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
2
poped element=4
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
2
poped element=3
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
2
poped element=2
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
2
poped element=1
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
2
stack is underflow!
1.PUSH  2.POP   3.DISPLAY       4.EXIT
enter the choice:
4

C Program Selection Sort


/*C Program Selection Sort*/
#include<stdio.h>
#include<conio.h>
void main()
{
    int a[20],i,j,n,small,position;

    printf("Enter the number of elements:\n");
    scanf("%d",&n);
    printf("Enter the elements: \n");
    for(i=0; i<n; i++)
        scanf("%d",&a[i]);
    for(i=0; i<n-1; i++)
    {
        small=a[i];
        position=i;
        for(j=i+1; j<n; j++)
            if(small>a[j])
            {
                small=a[j];
                position=j;
            }
        a[position]=a[i];
        a[i]=small;
    }
    printf("Soretd list is: \n");
    for(i=0; i<n; i++)
        printf("%d \n",a[i]);

}
Out Put:
Enter the number of elements:
9
Enter the elements:
54
78
95
15
32
25
14
68
123
Soretd list is:
14
15
25
32
54
68
78
95
123

C Program Quick Sort


/*C Program Quick Sort*/
#include<stdio.h>
#include<conio.h>
int partition(int a[],int low,int high)
{
    int i,j,temp,key;
    key=a[low];
    i=low+1;
    j=high;
    while(1)
    {
        while(i<high&&key>=a[i])
            i++;
        while(key<a[j])
            j--;
        if(i<j)
        {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
        else
        {
            temp=a[low];
            a[low]=a[j];
            a[j]=temp;
            return j;
        }
    }
}
void quick_sort(int a[],int low,int high)
{
    int j;
    if(low<high)
    {
        j=partition(a,low,high);
        quick_sort(a,low,j-1);
        quick_sort(a,j+1,high);
    }
}
void main()
{
    int i,n,a[20];

    printf(" enter no. of ele \n");
    scanf("%d",&n);
    printf("enter the ele \n");
    for(i=0; i<n; i++)
        scanf("%d",&a[i]);
    quick_sort(a,0,n-1);
    printf("sorted list is \n");
    for(i=0; i<n; i++)
        printf(" %d \n",a[i]);

}
Out Put:
Enter number of elements:
8
enter the ele
32
45
8
15
78
122
35
47
sorted list is
 8
 15
 32
 35
 45
 47
 78
 122

C Program Queue Operation


/*C Program Queue Operation*/
#include<stdio.h>
#include<conio.h>
#define max 5
int r,f,x,item,q[max];
void insert()
{
    if(r==max-1)
    {
        printf("queue is overflow! \n");
        return;
    }
    else
    {
        printf(" enter the element: \n");
        scanf("%d",&item);
        r=r+1;
        q[r]=item;
    }
}
void delete()
{
    if(f>r)
    {
        printf(" queue is underflow! \n");
        return;
    }
    x=q[f];
    printf(" deleted element = %d\n",x);
    f=f+1;
}
void display()
{
    int i;
    if(f>r)
    {
        printf(" queue is underflow \n");
    }
    printf(" contents of the queue: \n");
    for(i=f; i<=r; i++)
    {
        printf(" %d \t",q[i]);
    }
    printf("\n");
}

void main()
{
    int ch;
    f=0;
    r=-1;

    while(1)
    {
        printf("1.insert\t2.delete\t3.display\t4.exit \n");
        printf(" enter the choice: \n");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:
            insert();
            break;
        case 2:
            delete();
            break;
        case 3:
            display();
            break;
        default:
            exit(0);
        }

    }
}
Out Put:
1.insert        2.delete        3.display       4.exit
 enter the choice:
1
 enter the element:
1
1.insert        2.delete        3.display       4.exit
 enter the choice:
1
 enter the element:
2
1.insert        2.delete        3.display       4.exit
 enter the choice:
1
 enter the element:
3
1.insert        2.delete        3.display       4.exit
 enter the choice:
1
 enter the element:
4
1.insert        2.delete        3.display       4.exit
 enter the choice:
1
 enter the element:
5
1.insert        2.delete        3.display       4.exit
 enter the choice:
1
queue is overflow!
1.insert        2.delete        3.display       4.exit
 enter the choice:
3
 contents of the queue:
 1       2       3       4       5
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 deleted element = 1
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 deleted element = 2
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 deleted element = 3
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 deleted element = 4
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 deleted element = 5
1.insert        2.delete        3.display       4.exit
 enter the choice:
2
 queue is underflow!
1.insert        2.delete        3.display       4.exit
 enter the choice:
4

C Program Merge Sort


/*C Program Merge Sort*/
#include<stdio.h>
#include<conio.h>
void merge(int a[10],int low,int mid,int high)
{
    int i=low;
    int j=mid+1;
    int k=low;
    int c[100];
    while(i<=mid&&j<=high)
    {
        if(a[i]<a[j])
        {
            c[k]=a[i];
            i=i+1;
            k=k+1;
        }
        else
        {
            c[k]=a[j];
            j=j+1;
            k=k+1;
        }
    }
    while(i<=mid)
    {
        c[k]=a[i];
        k=k+1;
        i=i+1;
    }
    while(j<=high)
    {
        c[k]=a[j];
        k=k+1;
        j=j+1;
    }
    for(i=low; i<=high; i++)
    {
        a[i]=c[i];
    }
}
void merge_sort(int a[],int low,int high)
{
    int mid;
    if(low<high)
    {
        mid=(low+high)/2;
        merge_sort(a,low,mid);
        merge_sort(a,mid+1,high);
        merge(a,low,mid,high);
    }
}
void main()
{
    int a[100],n,i;

    printf("Enter number of elements: \n");
    scanf("%d",&n);
    printf("enter the elements:\n");
    for(i=0; i<n; i++)
        scanf("%d",&a[i]);
    merge_sort(a,0,n-1);
    printf("sorted elements are:\n");
    for(i=0; i<n; i++)
        printf("%d\n",a[i]);

}
Out Put:
Enter number of elements:
9
enter the elements:
12
56
4
8749
32
48
2
15
98
sorted elements are:
2
4
12
15
32
48
56
98
8749

C Program Linear Search


/*C Program Linear Search*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
    int i,n,ele,a[20];

    printf("enter number of elements:\n");
    scanf("%d",&n);
    printf("enter %d elements\n",n);
    for(i=0; i<=n-1; i++)
        scanf("%d",&a[i]);
    printf("enter the element to be searched:\n");
    scanf("%d",&ele);
    for(i=0; i<=n-1; i++)
    {
        if(a[i]==ele)
        {
            printf("element found at %d position",i+1);
            exit(0);
        }
    }
    printf("element %d not found",ele);

}
Out Put:
enter number of elements:
9
enter 9 elements
45
65
9
78
15
24
35
67
123
enter the element to be searched:
78
element found at 4 position

C Program Insertion Sort


/*C Program Insertion Sort*/
#include<stdio.h>
#include<conio.h>
void main()
{
    int a[20],n,temp,i,j;

    printf("Enter the no of elements:\n");
    scanf("%d",&n);
    printf("Enter %d elements:\n",n);
    for(i=0; i<n; i++)
        scanf("%d",&a[i]);
    for(i=1; i<n; i++)
    {
        temp=a[i];
        j=i-1;
        while(temp<a[j]&&j>=0)
        {
            a[j+1]=a[j];
            j=j-1;
        }
        a[j+1]=temp;
    }
    printf("Sorted elements are:\n");
    for(i=0; i<n; i++)
        printf(" %d \n",a[i]);

}
Out Put:
Enter the no of elements:
8
Enter 8 elements:
21
23
48
95
64
51
79
321
Sorted elements are:
 21
 23
 48
 51
 64
 79
 95
 321

C Program Create a Linked List


/*C Program Create a Linked List*/
#include<stdio.h>
#include<malloc.h>
struct node
{
    int info;
    struct node *link;
};
struct node *getnode()
{
    return (struct node*)malloc(sizeof(struct node));
}
struct node *insert (int item,struct node *first)
{
    struct node *temp;
    temp=getnode();
    temp->info=item;
    temp->link=first;
    return temp;
}
void display(struct node *temp)
{
    printf("contents of Linked List\n");
    while(temp!=NULL)
    {
        printf("%d\t", temp->info);
        temp=temp->link;
    }
    printf("\n");
}
void main()
{
    struct node*first=NULL;
    int ch,item;

    while(1)
    {
        printf("1.insert\t2.display\n");
        printf("enter the choice:\n");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:
            printf("enter the item to be inserted:\n");
            scanf("%d",&item);
            first=insert(item,first);
            break;
        case 2:
            display(first);
            break;
        default:
            exit(0);
        }

    }
}
Out Put:
1.insert        2.display
enter the choice:
1
enter the item to be inserted:
1
1.insert        2.display
enter the choice:
1
enter the item to be inserted:
2
1.insert        2.display
enter the choice:
1
enter the item to be inserted:
3
1.insert        2.display
enter the choice:
1
enter the item to be inserted:
4
1.insert        2.display
enter the choice:
1
enter the item to be inserted:
5
1.insert        2.display
enter the choice:
2
contents of Linked List
5       4       3       2       1
1.insert        2.display
enter the choice:
3

C Program Circular Queue


/*C Program Circular Queue*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define size 3
int item,x,f,r,connt,i,cQ[size];
void insert()
{
    if(connt==size-1)
    {
        printf("overflow\n");
        return;
    }
    printf("enter elements to be inserted:\n");
    scanf("%d",&item);
    r=r+1;
    cQ[r]=item;
    r=r%size;
    connt++;
}
void delete()
{
    if(connt==-1)
    {
        printf("empty\n");
        return;
    }
    x=cQ[f];
    printf("deleted element: %d\n",x);
    f=f+1;
    f=f%size;
    connt--;
}
void display()
{
    if(connt==-1)
    {
        printf("empty\n");
        return;
    }
    printf("elements in the queue:\n");
    for(i=0; i<=connt; i++)
    {
        printf("%d\t",cQ[f++]);
        f=f%size;
    }
    printf("\n");
}
void main()
{
    int ch;

    f=0,r=-1,connt=-1;
    while(1)
    {
        printf("1.insert\t2.delete\t3.display\n");
        printf("enter the choice:\n");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:
            insert();
            break;
        case 2:
            delete();
            break;
        case 3:
            display();
            break;
        default:
            exit (0);
        }

    }
}
Out Put:
1.insert        2.delete        3.display
enter the choice:
1
enter elements to be inserted:
1
1.insert        2.delete        3.display
enter the choice:
1
enter elements to be inserted:
2
1.insert        2.delete        3.display
enter the choice:
1
enter elements to be inserted:
3
1.insert        2.delete        3.display
enter the choice:
1
overflow
1.insert        2.delete        3.display
enter the choice:
3
elements in the queue:
1       2       3
1.insert        2.delete        3.display
enter the choice:
2
deleted element: 1
1.insert        2.delete        3.display
enter the choice:
2
deleted element: 2
1.insert        2.delete        3.display
enter the choice:
2
deleted element: 3
1.insert        2.delete        3.display
enter the choice:
2
empty
1.insert        2.delete        3.display
enter the choice:
4

C program Binary Search


/*C program Binary Search*/
#include<stdio.h>
#include<conio.h>
void main()
{
    int array[10];
    int i, j, n, temp, num;
    int low,mid,high;

    printf("Enter the value of the array\t");
    scanf("%d",&n);
    printf("Enter the elements one by one:\n");
    for(i=0; i<n; i++)
    {
        scanf("%d",&array[i]);
    }
    printf("Input array elements\n");
    for(i=0; i<n; i++)
    {
        printf("%d\n",array[i]);
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<(n-i-1); j++)
        {
            if(array[j]>array[j+1])
            {
                temp=array[j];
                array[j]=array[j+1];
                array[j+1]=temp;
            }
        }
    }
    printf("Sorted array is...\n");
    for(i=0; i<n; i++)
    {
        printf("%d\n",array[i]);
    }
    printf("Enter the element to be searched\n");
    scanf("%d",&num);
    low=1;
    high=n;
    do
    {
        mid=(low+high)/2;
        if(num<array[mid])
            high=mid-1;
        else if(num>array[mid])
            low=mid+1;
    }
    while(num!=array[mid] && low<=high);
    if(num==array[mid])
    {
        printf("\n\t%d is present at position %d",num,mid+1);
    }
    else
    {
        printf("Search is FAILED\n");
    }

}
Output:
Enter the value of the array    8
Enter the elements one by one:
4
6
1
9
7
2
3
8
Input array elements
4
6
1
9
7
2
3
8
Sorted array is...
1
2
3
4
6
7
8
9
Enter the element to be searched
5
Search is FAILED