Tuesday 28 February 2012

C program to search an element in an array using Binary search



#include<stdio.h>
main()
{
int a[20],i,j,d,t,x,l=0,low,mid,high;
printf("\nEnter the number of elements\n");
scanf("%d",&d);
printf("Enter the numbers\n");
for(i=0;i<d;i++)
scanf("%d",&a[i]);
for(i=0;i<d;i++)
{
for(j=i+1;j<d;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("\nThe sorted list :");
for(i=0;i<d;i++)
printf("%d ",a[i]);
printf("\nEnter the number to be searched\n");
scanf("%d",&x);
low=0;
high=d-1;
while(low<=high)
{
mid=(low+high)/2;
if(x<a[mid])
high=mid-1;
else if(x>a[mid])
low=mid+1;
else
{
if(x==a[mid])
{
l++;
printf("The item %d is found at location %d\n",x,mid+1);
exit(0);
}
}
}
if(l==0)
printf("Item not found\n");
}




Saturday 18 February 2012

C program to implement a queue using linked list



#include<stdio.h>
void enq();
void deq();
void display();
main()
{
int n;
printf("\tMENU\n1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.EXIT\n");
do
{
printf("\nEnter your choice\n");
scanf("%d",&n);
switch(n)
{
case 1:
enq();
break;
case 2:
deq();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Invalid choice\n");
break;
}
}
while(n!=4);
}

typedef struct node
{
int data;
struct node *link;
}n;
n *front=NULL;
n *rear=NULL;

void enq()
{
int item;
n *temp;
printf("Enter the item\n");
scanf("%d",&item);
temp=(n*)malloc(sizeof(n));
temp->data=item;
temp->link=NULL;
if(rear==NULL)
{
front=temp;
rear=temp;
}
else
{
rear->link=temp;
rear=temp;
}
}

void deq()
{
int item;
if(front==NULL)
printf("Queue is empty\n");
else
{
item=front->data;
printf("The element deleted = %d\n",item);
}
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
front=front->link;
}

void display()
{
n *ptr;
if(front==NULL)
printf("Queue is empty\n");
else
{
ptr=front;
printf("The elements of the queue are :");
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->link;
}
}
}



C program to implement a stack using linked list



#include<stdio.h>
void push();
void pop();
void display();
main()
{
int n;
printf("\tMENU\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
do
{
printf("\nEnter your choice\n");
scanf("%d",&n);
switch(n)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Invalid choice\n");
break;
}
}
while(n!=4);
}

typedef struct node
{
int data;
struct node *link;
}n;
n *top=NULL;

void push()
{
int item;
n *temp;
printf("Enter the item\n");
scanf("%d",&item);
temp=(n*)malloc(sizeof(n));
temp->data=item;
temp->link=top;
top=temp;
}

void pop()
{
n *temp;
if(top==NULL)
printf("Stack is empty\n");
else
{
temp=top;
printf("The element deleted = %d\n",temp->data);
free(temp);
top=top->link;
}
}

void display()
{
n *save;
if(top==NULL)
printf("Stack is empty\n");
else
{
save=top;
printf("The elements of the stack are :");
while(save!=NULL)
{
printf("%d\t",save->data);
save=save->link;
}
printf("\nTopmost element = %d\n",top->data);
}
}



Saturday 4 February 2012

C program to implement a queue using array



#include<stdio.h>
main()
{
int q[10]={0},i,front=-1,rear=-1,max=10,n,item;
printf("\n\tMENU\n1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.EXIT\n");
do
{
printf("\nEnter your choice\n");
scanf("%d",&n);
switch(n)
{
case 1:
if(rear<max-1)
{
printf("Enter the element\n");
scanf("%d",&item);
if(rear==-1)
{
front=0;
rear=0;
q[rear]=item;
}
else
q[++rear]=item;
}
else
printf("Overflow\n");
break;

case 2:
if(front>=0)
{
printf("The deleted item =%d",q[front]);
if(front==rear)
{
front=-1;
rear=-1;
}
else
front++;
}
else
printf("Underflow\n");
break;

case 3:
if((front==-1)&&(rear==-1))
printf("The queue is empty\n");
else
{
printf("The elements of the queue are :");
for(i=front;i<=rear;i++)
printf("%d\t",q[i]);
}
break;

case 4:
break;

default:
printf("Invalid choice\n");
break;
}
}
while(n!=4);
}



C program to implement a stack using array



#include<stdio.h>
main()
{
int a[10]={0},i,top=-1,max=10,n,x;
printf("\n\tMENU\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
do
{
printf("\nEnter your choice\n");
scanf("%d",&n);
switch(n)
{
case 1:
if(top==max-1)
printf("Overflow\n");
else
{
printf("Enter the element\n");
scanf("%d",&x);
a[++top]=x;
}
break;

case 2:
if(top<0)
printf("Underflow\n");
else
printf("The deleted item =%d",a[top--]);
break;

case 3:
if(top<0)
printf("The stack is empty\n");
else
{
printf("The elements of the stack are :");
for(i=0;i<=top;i++)
printf("%d\n",a[i]);
}
break;

case 4:
break;

default:
printf("Invalid choice\n");
break;
}
}
while(n!=4);
}