DATA STRUCTURES IN C LANGUAGE
COMPARING OF TWO STRINGS PROGRAM
/* Comparing Two Strings */
#include
#include <conio.h>
#include <stdlib.h>
void main ( )
{
char *c1, *c2, str1[10], str2[10];
clrscr ( );
printf ("Enter the string no 1:\n");
scanf("%s",str1);
printf ("Enter the string no 2:\n");
scanf("%s",str2);
c1=str1;
c2=str2;
while(*c1==*c2 && *c1!='\0' && *c2!='\0')
{
*c1=*c1+1;
*c2=*c2+1;
}
if (*c1=='\0' && *c2=='\0')
printf ("strings are equal\n");
else
printf ("strings are not equal\n");
getch ( );
}
#include
#include <conio.h>
#include <stdlib.h>
void main ( )
{
char *c1, *c2, str1[10], str2[10];
clrscr ( );
printf ("Enter the string no 1:\n");
scanf("%s",str1);
printf ("Enter the string no 2:\n");
scanf("%s",str2);
c1=str1;
c2=str2;
while(*c1==*c2 && *c1!='\0' && *c2!='\0')
{
*c1=*c1+1;
*c2=*c2+1;
}
if (*c1=='\0' && *c2=='\0')
printf ("strings are equal\n");
else
printf ("strings are not equal\n");
getch ( );
}
BINARY SEARCH PROGRAM
/* Implementation of Binary Search */
/* Data Structures Using C by UDIT AGARWAL */
#include
#include<conio.h>
void main()
{
char ch;
int arr[20],start,end,mid,n,i,data;
clrscr();
printf("\nHow many elements you want to enter in the array:");
scanf("%d",&n);
for(i=0;i
{
printf("\nEnter element %d:",i+1);
scanf("%d",&arr[i]);
}
printf("\n\nPress any key to continue...");
getch();
do
{
clrscr();
printf("\nEnter the element to be searched:");
scanf("%d",&data);
start=0;
end=n-1;
mid=(start + end)/2;
while(data!=arr[mid] && start <=end)
{
if(data > arr[mid])
start=mid+1;
else
end=mid-1;
mid=(start+end)/2;
}
if(data==arr[mid])
printf("\n%d found at position %d\n",data,mid + 1);
if(start>end)
printf("\n%d not found in array\n",data);
printf("\n\n Press to continue:");
fflush(stdin);
scanf("%c",&ch);
}while(ch == 'Y' || ch == 'y');
}
LINEAR SEARCH PROGRAM
/* Implementation of Linear Search*/
#include
#include<conio.h>
void main()
{
char ch;
int arr[50],n,i,item;
clrscr();
printf("\nHow many elements you want to enter in the array:");
scanf("%d",&n);
for(i=0;i
{
printf("\nEnter element %d:",i+1);
scanf("%d",&arr[i]);
}
printf("\n\nPress any key to continue.....");
getch();
do
{
clrscr();
printf("\nEnter the element to be searched:");
scanf("%d",&item);
for(i=0;i < n;i++)
{
if(item == arr[i])
{
printf("\n%d found at position %d\n",item,i+1);
break;
}
}
if (i == n)
printf("\nItem %d not found in array\n",item);
printf("\n\nPress (Y/y) to continue: ");
fflush(stdin);
scanf("%c",&ch);
}while(ch == 'Y'|| ch == 'y');
}
CREATING A SIMPLE TREE PROGRAM
/*Create the simple binary tree and recursive traversal*/
#include
#include<conio.h>
#include<alloc.h>
typedef struct bin
{
int data;
struct bin *left;
struct bin *right;
}node;
void insert (node *, node*);
void inorder (node *);
void preorder (node *);
void postorder (node *);
node *get_node();
void main( )
{
int choice;
char ans = 'n';
node *newn,*root;
root = NULL;
clrscr();
do
{
printf("\n Program for implementing simple binary tree\n");
printf("\n**********************************************\n");
printf("\n 1.create");
printf("\n 2.inorder");
printf("\n 3.preorder");
printf("\n 4.postorder");
printf("\n 5.exit");
printf("\n \t Enter Your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
root = NULL;
do
{
newn=get_node();
printf("\n enter the element\n");
scanf("%d",& newn->data);
if(root == NULL)
root= newn;
else
insert(root, newn);
printf("\n Do you want to enter more elements?(y/n)\n");
ans = getch();
}
while(ans== 'Y'|| ans=='y');
clrscr();
break;
case 2:
if(root == NULL)
printf("Tree is not created!");
else
inorder(root);
break;
case 3:
if(root ==NULL)
printf("Tree is not created!");
preorder (root);
break;
case 4:
if(root == NULL)
printf("Tree is not created!");
postorder(root);
break;
}
}while(choice!=5);
}
node *get_node()
{
node *temp;
temp = (node *) malloc(sizeof(node));
temp->left = NULL;
temp->right = NULL;
return temp;
}
void insert(node *root, node *newn)
{
char ch;
printf("\n Where to insert left/right of %d:\n", root->data);
ch=getche();
if((ch=='R') || (ch=='r'))
{
if(root->right == NULL)
{
root->right=newn;
}
else
insert(root->right,newn);
}
else
{
if(root->left== NULL)
{
root->left=newn;
}
else
insert(root->left,newn);
}
}
void inorder(node *temp)
{
if(temp!= NULL)
{
inorder(temp->left);
printf("%d", temp->data);
inorder(temp ->right);
}
}
void preorder(node *temp)
{
if(temp != NULL)
{
printf("%d",temp->data);
preorder(temp->left);
preorder(temp->right);
}
}
void postorder(node *temp)
{
if(temp!= NULL)
{
postorder(temp-> left);
postorder(temp->right);
printf("%d",temp->data);
}
}
THANK YOU!!!@!
No comments:
Post a Comment