Saturday, October 23, 2010

/*Program to Create a Binary Search Tree and Traverse it*/

/*Program to Create a Binary Search Tree and Traverse it*/
#include
struct tree
{
int info;
struct tree *left;
struct tree *right;
};
main()
{
struct tree *root;
int ch,ele;
root=NULL;
clrscr();
while(1)
{
printf("\nTREE OPERATIONS\n");
printf("1.INSERT\n");
printf("2.IN ORDER TRAVERSAL\n");
printf("3.PRE ORDER TRAVERSAL\n");
printf("4.POST ORDER TRAVERSAL\n");
printf("5.EXIT\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the element to insert");
scanf("%d",&ele);
insert(&root,ele);
printf("\nSuccessfully Inserted\n");
break;
case 2: inorder(root);
break;
case 3: preorder(root);
break;
case 4: postorder(root);
break;
case 5: exit(1);
}
}
}
inorder(struct tree *root)
{
if(root!=NULL)
{
inorder(root->left);
printf(" %d ",root->info);
inorder(root->right);
}
}
preorder(struct tree *root)
{
if(root!=NULL)
{
preorder(root->left);
preorder(root->right);
printf(" %d ",root->info);
}
}
postorder(struct tree *root)
{
if(root!=NULL)
{
printf(" %d ",root->info);
postorder(root->left);
postorder(root->right);
}
}
insert(struct tree **root,int ele)
{
struct tree *x,*y,*temp;
temp=(struct tree *)malloc(sizeof(struct tree));
temp->info=ele;
temp->left=temp->right=NULL;
x=y=*root;
if(*root==NULL)
*root=temp;
else
{
while(x!=NULL)
{
y=x;
if(eleinfo)
x=x->left;
else
x=x->right;
}
if(eleinfo)
y->left=temp;
else
y->right=temp;
}
}

Thursday, October 21, 2010

/*Bubble Sort and Binary Search*/

/*Bubble Sort and Binary Search*/
main()
{
int a[10],n,i,ch,ele,pos;
clrscr();
printf("Enter the size of array");
scanf("%d",&n);
printf("Enter %d elements",n);
for(i=0;i
scanf("%d",&a[i]);
printf("Elements before Sort\n");
for(i=0;i
printf(" %d ",a[i]);
bubble(a,n);
printf("\nElements after Sort\n");
for(i=0;i
printf(" %d ",a[i]);
while(1)
{
printf("\n1.SEARCH\n2.EXIT\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the element you want to search");
scanf("%d",&ele);
pos=search(a,n,ele);
if(pos==0)
printf("Element not found in the list");
else
printf("Element is at position %d",pos);
break;
case 2: exit();
}
}
}
bubble(int a[],int n)
{
int i,j;
for(i=0;i
for(j=0;j
if(a[j]>a[j+1])
{
a[j]=a[j]+a[j+1];
a[j+1]=a[j]-a[j+1];
a[j]=a[j]-a[j+1];
}
}
search(int a[],int n,int ele)
{
int low,high,mid;
low=0;high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==ele)
return mid+1;
else if(a[mid]
low=mid+1;
else
high=mid-1;
}
return 0;
}

/*Circular Queue implementation using arrays*/

/*Circular Queue implementation using arrays*/
struct queue
{
int a[10];
int front,rear;
int count;
}
main()
{
struct queue q;
int ele,ch;
q.front=q.rear=-1;
q.count=0;
clrscr();
while(1)
{
printf("\nCircular Queue Operations\n");
printf("1.INSERT\n");
printf("2.DELETE\n");
printf("3.DISPLAY\n");
printf("4.COUNT\n");
printf("5.EXIT\n");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the element you want to insert");
scanf("%d",&ele);
insert(&q,ele);
break;
case 2: ele=delete(&q);
if(ele==-1)
printf("Queue Empty. No elements to delete.");
else
printf("The deleted element is %d",ele);
break;
case 3: display(q);
break;
case 4: printf("The total no. of elements in the queue are %d",q.count);
break;
case 5: exit();
}
}
}
insert(struct queue *q,int ele)
{
if(q->count==10)
printf("Queue full. Insert not possible.");
else
{
if(q->rear==9)
q->rear=-1;
q->a[++q->rear]=ele;
q->count++;
}
}
delete(struct queue *q)
{
if(q->count==0)
return -1;
else
{
if(q->front==9)
q->front=-1;
q->count--;
return q->a[++q->front];
}
}
display(struct queue q)
{
if(q.count==0)
printf("No elements to display.");
else
while(q.count--!=0)
{
if(q.front==9)
q.front=-1;
printf(" %d ",q.a[++q.front]);
}
}


/*Stack Operations using Arrays.*/

/*Stack Operations using Arrays.*/
struct stack
{
int s[10];
int top;
};
main()
{
int ch,ele;
struct stack s1;
s1.top=-1;
clrscr();
while(1)
{
printf("\nSTACK OPERATIONS\n");
printf("1.PUSH\n");
printf("2.POP\n");
printf("3.DISPLAY\n");
printf("4.EXIT\n");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the element you want to push : ");
scanf("%d",&ele);
push(&s1,ele);
break;
case 2: ele=pop(&s1);
if(ele==-1)
printf("Stack Underflow. No elements to pop");
else
printf("The popped element is %d",ele);
break;
case 3: display(s1);
break;
case 4: exit();
}
}
}
push(struct stack *s1,int ele)
{
if(s1->top==9)
printf("Stack Overflow. Cannot push");
else
s1->s[++(s1->top)]=ele;
}
pop(struct stack *s1)
{
if(s1->top==-1)
return -1;
else
return s1->s[(s1->top)--];
}
display(struct stack s1)
{
if(s1.top==-1)
printf("Stack Empty. Nothing to display.");
else
while(s1.top!=-1)
printf(" %d ",s1.s[s1.top--]);
}