Saturday, 26 April 2014

Bubble Sort

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdio.h>
void bubble_sort(int [],int);
void main()
{
    int a[30],n,i;
    clrscr();
    printf("\n\n\n$$$  BUBBLE SORT  $$$\n\n\n");
    printf("\nEnter no of elements :");
    scanf("%d",&n);
    printf("\nEnter array elements :");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    bubble_sort(a,n);
    getch();
}
void bubble_sort(int a[],int n)
{
    int i,j,k,temp;
    printf("\nUnsorted Data:");
    for(k=0;k<n;k++)
    {
        printf("%5d",a[k]);
    }
    for(i=1;i<n;i++)
    {
        for(j=0;j<n-1;j++)
        {
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
        printf("\nAfter pass %d : ",i);
        for(k=0;k<n;k++)
        {
            printf("%5d",a[k]);
        }
    }
}


OUTPUT:

$$$  BUBBLE SORT  $$$

Enter no of elements :5

Enter array elements :10    4    55    21    6

Unsorted Data: 10    4    55    21    6

After pass 1 : 4    10    21    6    55

After pass 2 : 4    10    6    21    55

After pass 3 : 4    6    10    21    55

After pass 4 : 4    6    10    21    55

Sorting

 PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,n,t,j,a[20];
    clrscr();
    printf("\n\n\n $$$    Sorting     $$$ \n\n\n");
    printf("Enter the number of elements:\t");
    scanf("%d",&n);

    printf("Enter %d elements:\t",n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

    for(i=0;i<n-1;i++)
    {
        for(j=i;j<=n-1;j++)
        {
            if(a[i]>a[j])
            {
                t=a[i];
                a[i]=a[j];
                a[j]=t;
            }
        }
    }

    printf("Numbers in Ascending order:\t");
    for(i=0;i<n;i++)
    {
        printf("%d\t",a[i]);
    }

    printf("\nNumbers in Descending order:\t");
    for(i=n-1;i>=0;i--)
    {
        printf("%d\t",a[i]);
    }
    getch();
}


OUTPUT:


$$$     Sorting      $$$

Enter the number of elements:   6

Enter 6 elements:            8              2              6              1              4              9

Numbers in Ascending order:     1       2       4       6       8       9

Numbers in Descending order:    9       8       6       4       2       1

Binary Search

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
    int c, first, last, middle, n, f=0, search, array[100];
    clrscr();
    printf("\n\n\n Binary Search using Array \n\n\n");
    printf("Enter number of elements:\t");
    scanf("%d",&n);

    printf("Enter %d integers\n", n);
    for(c=0;c<n;c++)
    {
        scanf("%d",&array[c]);
    }
    printf("Enter value to find:\t");
    scanf("%d",&search);

    first = 0;
    last = n - 1;


    while( first <= last )
    {
        middle = (first+last)/2;
        if ( search < array[middle] )
        {
            last = middle - 1;
        }
        else if( search > array[middle] )
        {
            first= middle + 1;
        }
        else
        {
            printf("%d found at location %d.\n", search, middle+1);
            f=1;
            break;
        }
    }
    if ( f==0 )
    {
        printf("Not found! %d is not present in the list.\n", search);
    }
    getch();
}


OUTPUT:


Binary Search using Array

Enter number of elements:       5
Enter 5 integers
2
4
6
8
10
Enter value to find: 10
10 found at location 5.


 Binary Search using Array
 
Enter number of elements:       5
Enter 5 integers
1
3
5
7
9
Enter value to find: 8
Not found! 8 is not present in the list.

Linear Search


PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[10],i,n,key,f=0;
    clrscr();
    printf("\n\n LINEAR SEARCH \n\n");
    printf("Enter the array limit:\t");
    scanf("%d",&n);
    printf("Enter array elements:\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("Enter the element to be searched:\t");
    scanf("%d",&key);
    for(i=0;i<n;i++)
    {
        if(a[i]==key)
        {
            printf("Element found\n");
            f=1;
            break;
        }
    }
    if(f==0)
    {
        printf("Element not found\n");
    }
    getch();
}


OUTPUT:


LINEAR SEARCH

Enter the array limit:  5
Enter array elements:
1
3
5
7
9
Enter the element to be searched:       9
Element found



LINEAR SEARCH

Enter the array limit:  5
Enter array elements:
2
4
6
8
10
Enter the element to be searched:       9
Element not found

Sunday, 13 April 2014

Circular Singly Linked List

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *head=NULL,*newn,*temp,*t;
void insert();
void del();
void display();
void main()
{
int ch;
clrscr();
printf("\n\n$$$ CIRCULARLY LINKED LIST $$$\n\n");
while(1)
{
printf("\n\nCIRCULARLY LINKED LIST OPERATIONS ARE\n\n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter Ur Choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();break;
case 2:del();break;
case 3:display();break;
case 4:exit(0);
default:printf("Enter the correct option\n");
break;
}
}
}
void insert()
{
int no,op;
newn=(node *)malloc(sizeof(node));
printf("Enter the element to be inserted:\t");
scanf("%d",&newn->data);
printf("1.Insert at First\n2.Insert at Middle\n3.Insert at Last\n");
printf("Enter the option:\t");
scanf("%d",&op);
switch(op)
{
case 1:
if(head==NULL)
{
head=newn;
newn->next=newn;
}
else
{
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=newn;
newn->next=head;
head=newn;
}
break;

case 2:
if(head->next==NULL)
{
printf("Single node entry so insertion is not allowed in the middle\n");
}
else
{
printf("Enter the data after which the element to be inserted:\t");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
temp=temp->next;
}
newn->next=temp->next;
temp->next=newn;
}
break;

case 3:
if(head==NULL)
{
head=newn;
newn->next=newn;
}
else
{
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=newn;
newn->next=head;
}
break;
}
}
void del()
{
int op,no;
if(head==NULL)
{
printf("List is empty\n");
}
else
{
printf("1.Deletion at first\n2.Deletion at middle\n3.Deletion at last\n");
printf("Enter your choice:\t");
scanf("%d",&op);
switch(op)
{
case 1:
if(head->next==head)
{
printf("The deleted element is: %d",head->data);
head=NULL;
}
else
{
temp=head;
printf("The deleted element is: %d",temp->data);
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=head->next;
head=head->next;
free(temp);
}
break;

case 2:
if(head->next==head)
{
printf("Single node entry cannot perform deletion in the middle\n");
}
else
{
printf("Enter the element to be deleted:\t");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
t=temp;
temp=temp->next;
}
printf("The deleted element is: %d",temp->data);
t->next=temp->next;
free(temp);
}
break;

case 3:
if(head->next==head)
{
printf("The deleted element is: %d",head->data);
head=NULL;
}
else
{
temp=head;
while(temp->next!=head)
{
t=temp;
temp=temp->next;
}
printf("The deleted element is: %d",temp->data);
t->next=head;
free(temp);
}
break;
}
}
}
void display()
{
if(head==NULL)
{
printf("List is empty\n");
}
else
{
temp=head;
printf("The elements in a list are:\n");
do
{
printf("%d\n",temp->data);
temp=temp->next;
}while(temp!=head);
}

}


OUTPUT:

$$$ CIRCULARLY LINKED LIST $$$

CIRCULARLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        1
Enter the element to be inserted:       11
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option:       1

CIRCULARLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        1
Enter the element to be inserted:       33
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option:       3

CIRCULARLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        1
Enter the element to be inserted:       22
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option:       2
Enter the data after which the element to be inserted:  11

CIRCULARLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        3
The elements in a list are:
11
22
33

CIRCULARLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        2
1.Deletion at first
2.Deletion at middle
3.Deletion at last
Enter your choice:      2
Enter the element to be deleted:        22
The deleted element is: 22

CIRCULARLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        3
The elements in a list are:
11
33

CIRCULARLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        4

Doubly Linked List

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *next,*prev;
}node;
node *head=NULL,*newn,*temp,*t;
void insert();
void del();
void display();
void main()
{
int ch;
clrscr();
printf("\n\n$$$ DOUBLY LINKED LIST $$$\n\n");
while(1)
{
printf("\n\nDOUBLY LINKED LIST OPERATIONS ARE\n\n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter Ur Choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();break;
case 2:del();break;
case 3:display();break;
case 4:exit(0);
default:printf("Enter the correct option\n");
break;
}
}
}
void insert()
{
int no,op;
newn=(node *)malloc(sizeof(node));
printf("Enter the element to be inserted:\t");
scanf("%d",&newn->data);
printf("1.Insert at First\n2.Insert at Middle\n3.Insert at Last\n");
printf("Enter the option:\t");
scanf("%d",&op);
switch(op)
{
case 1:
if(head==NULL)
{
newn->next=NULL;
newn->prev=NULL;
head=newn;
}
else
{
newn->next=head;
head->prev=newn;
head=newn;
newn->prev=NULL;
}
break;

case 2:
if(head->next==NULL)
{
printf("Single node entry so insertion is not allowed in the middle\n");
}
else
{
printf("Enter the data after which the element to be inserted:\t");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
temp=temp->next;
}
newn->next=temp->next;
temp->next->prev=newn;
temp->next=newn;
newn->prev=temp;
}
break;

case 3:
if(head==NULL)
{
newn->next=NULL;
newn->prev=NULL;
head=newn;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newn;
newn->prev=temp;
newn->next=NULL;
}
break;
}
}
void del()
{
int op,no;
if(head==NULL)
{
printf("List is empty\n");
}
else
{
printf("1.Deletion at first\n2.Deletion at middle\n3.Deletion at last\n");
printf("Enter your choice:\t");
scanf("%d",&op);
switch(op)
{
case 1:
if(head->next==NULL)
{
printf("The deleted element is: %d",head->data);
head=NULL;
}
else
{
temp=head;
head=head->next;
printf("The deleted element is: %d",temp->data);
free(temp);
}
break;

case 2:
if(head->next==NULL)
{
printf("Single node entry cannot perform deletion in the middle\n");
}
else
{
printf("Enter the element to be deleted:\t");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
t=temp;
temp=temp->next;
}
temp->prev=t;
printf("The deleted element is: %d",temp->data);
t->next=temp->next;
temp->next->prev=t;
free(temp);
}
break;

case 3:
if(head->next==NULL)
{
printf("The deleted element is: %d",head->data);
head=NULL;
}
else
{
temp=head;
while(temp->next!=NULL)
{
t=temp;
temp=temp->next;
}
t->next=NULL;
printf("The deleted element is: %d",temp->data);
free(temp);
}
break;
}
}
}
void display()
{
if(head==NULL)
{
printf("List is empty\n");
}
else
{
temp=head;
printf("The elements in a list are:\n");
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}

}


OUTPUT:

$$$ DOUBLY LINKED LIST $$$

DOUBLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        1
Enter the element to be inserted:       11
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option:       1

DOUBLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        1
Enter the element to be inserted:       33
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option:       3

DOUBLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        1
Enter the element to be inserted:       22
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option:       2
Enter the data after which the element to be inserted:  11

DOUBLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        3
The elements in a list are:
11
22
33

DOUBLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        2
1.Deletion at first
2.Deletion at middle
3.Deletion at last
Enter your choice:      2
Enter the element to be deleted:        22
The deleted element is: 22

DOUBLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        3
The elements in a list are:
11
33

DOUBLY LINKED LIST OPERATIONS ARE

1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice:        4