Wednesday, July 27, 2011

Selection Sort in C

#include
int main()
{
int i,j,num,list[6],temp;


printf("Enter the Numbers:");
for(i=0;i<6;i++)
{
scanf("%d",&list[i]);
}
printf("Entered Numbers are:");
for(i=0;i<6;i++)
{
printf("%d ",list[i]);
}
for(i=0;i<6;i++)
{
for(j=0;j<5;j++)
{
if(list[j]>list[i])//if u put > as < u will get descending order
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}


}

}
printf("\nascending order\n");
for(j=0;j<6;j++)
{
printf("%d ",list[j]);
}
printf("\n");
}

Tuesday, July 26, 2011

Linear Search in C

#include
int main()
{
int i,item,list[5];
printf("Enter numbers;\n");
for(i=0;i<5;i++)
{
scanf("%d",&list[i]);
}
printf("Entered Numbers Are:\n");
for(i=0;i<5;i++)
{
printf("%d ",list[i]);
}
printf("Enter item to be selected:\n");
scanf("%d",&item);
for(i=0;i<5;i++)
{
if(item==list[i])
{
printf("item found at postion:%d\n",i+1);
printf("founded item is:%d\n",list[i]);
return;
}
}
printf("item does not exist\n");
}

bubble sort in c

#include
int main()
{
int i,j,num,list[6],temp;


printf("Enter the Numbers:");
for(i=0;i<6;i++)
{
scanf("%d",&list[i]);
}
printf("Entered Numbers are:");
for(i=0;i<6;i++)
{
printf("%d ",list[i]);
}
for(i=0;i<6;i++)
{
for(j=0;j<5;j++)
{
if(list[j]>list[j+1])
//if u change > as < you will get Descending order
{
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}


}

}
printf("\nascending order\n");
for(j=0;j<6;j++)
{
printf("%d ",list[j]);
}
printf("\n");
}

Binary Search in C

#include
void binary()
{
int i,item,mid;
int list[6];
printf("Enter the values:");
for(i=0;i<5;i++)
{
scanf("%d",&list[i]);
}
printf("Entered Items are:");
for(i=0;i<5;i++)
{
printf("%d ",list[i]);
}
printf("\n");
//int list[5]={1,2,3,4,5};
int lower,upper;
lower=0;
upper=4;
printf("item to be searched:");
scanf("%d",&item);
while(1)
{
mid=(lower+upper)/2;
if(list[mid]==item)
{
printf("position of item is:");
printf("%d",mid+1);
return;
}
else if(list[mid] {
lower=mid+1;
}
else
{
upper=mid-1;
}
if(lower>upper)
{
printf("item does not exist:");
return;
}


}



}
int main()
{
binary();
}

Stack in DataStructure

#include
#include
#include
int top=-1,count=0;
char stk[10][10];
void push();
void pop();
void display();
int main()
{
int ch;
do{
printf("\n1.Push\n2.pop\n3.Display\n4.Exit\n");
printf("Enter u r choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Enter Correct vale\n");

}
}while(ch!=4);

}
void push()
{
char name[10];
if(top<10)
{
printf("Enter any string:\n");
top++;
count++;
scanf("%s",name);
strcpy(stk[top],name);

}
else
{
printf("stack is full\n");
return;
}

}
void pop()
{
if(top==-1)
{
printf("stack is empty:\n");
}
printf("Elements deletd:%s ",stk[top]);
top--;
count--;


}
void display()
{
int i;
if(top==-1)
{
printf("stack is empty:\n");
}
printf("Items on the stack:\n");
for(i=0;i<=top;i++)
{
printf("%s ",stk[i]);
}
}

Single Linked List in DataStructure

#include
#include
#include
struct list
{

int data;
struct list *next;
}*root = NULL;
int count = 0;
void create()
{
int item;
struct list *temp,*node;
temp=(struct list *)malloc(sizeof(struct list));
printf("Enter Value :\n");
scanf("%d",&item);
temp->data = item;
if(root == NULL)
{
root = temp;
root->next = NULL;
count++;
return;
}
node = root;
while(node->next != NULL)
{
node = node->next;
}
node->next = temp;
temp->next = NULL;
count++;

}
void display()
{
struct list *temp;
temp = root;
if(root == NULL)
{
printf("list is empty\n");
return;
}
while(temp!=NULL)
{
printf("\t%d ",temp->data);
temp= temp->next;
}
printf("\n");

}
void insertatbeg()
{
struct list *temp;
temp = (struct list *)malloc(sizeof(struct list));
printf("Enter Value : \n");
scanf("%d",&temp->data);
if(root == NULL)
{
root = temp;
temp->next =NULL;
return;
}
temp->next = root;

root = temp;
count++;
}
void insertatmid()
{
struct list *temp,*node;
int pos,i=0;
temp= (struct list *)malloc(sizeof(struct list));
printf("Enter value: \n");
scanf("%d",&temp->data);
if(root==NULL)
{
printf("list is Empty");
return;
}
node=root;
printf("Enter the position to enter node:");
scanf("%d",&pos);
if(pos>count || pos<0)

{
printf("Enter correct position:\n");

}
while(i<=count)
{
if(i==pos-1)
{
temp->next=node->next;

node->next=temp;

count++;
return;

}
node=node->next;
i++;
}
}
void insertlast()
{
struct list *temp,*node;
temp=(struct list *)malloc(sizeof(struct list));
printf("Enter Value:\n");
scanf("%d",&temp->data);
if(root==NULL)
{
printf("list is empty:\n");
}
node=root;
while(root!=NULL)
{
if(node->next==NULL)
{
node->next=temp;
temp->next=NULL;
count++;
return;
}
else
{
node=node->next;
}
}



}
void deleteatbeg()
{
if(root==NULL)
{
printf("List is Empty");
}
printf("deleted value:%d",root->data);
root=root->next;

count--;
}
void deleteatmid()
{
struct list *temp,*node;
int pos,i=0;

if(root==NULL)
{
printf("list is Empty");
return;
}
node=root;
printf("Enter the position to delete node:");
scanf("%d",&pos);
if(pos>count || pos<0)
{
printf("Enter correct position:\n");

}
while(i<=count)
{
if(i==pos-1)
{

temp=node->next->next;
node->next=temp;

count--;
return;

}
node=node->next;
i++;
}

}
void deletelast()
{
struct list *temp,*node;

if(root==NULL)
{
printf("List is empty:");
return;
}
temp=root;
if(root->next==NULL)
{
printf("deleted item is:%d",node->data);
root=NULL;
return;
}

while(temp->next->next!=NULL)
{
temp=temp->next;

}
printf("deleted data is:%d",temp->next->data);
temp->next=NULL;




}
int main()
{
int ch;
do{
printf("\n1.insert\t2.delete\t3.Display\t4.Exit\t5.insertatbeg\t6.deleteatbeg\t7.insertatmid\t8.deleteatmid\t9.insertlast\t10.deletelast\n");
printf("Enter u r choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
//delete();
break;
case 3:
display();
break;
case 4:
exit(0);
case 5:
insertatbeg();
break;
case 6:
deleteatbeg();
break;
case 7:
insertatmid();
break;
case 8:
deleteatmid();
break;
case 9:
insertlast();
break;
case 10:
deletelast();
break;

default:
printf("Enter Correct vale\n");

}
}while(ch!=4);



}

Sunday, July 10, 2011

write a program to convert decimal to binary,octal and decimal?


Answer:

#include
void main()
{
int d;
int i=0,n,j,b[100];
cout<<"\n Press 1 for Decimal to Binary converstion";
cout<<"\n press 2 for Decimal to Octal converstion ";
cout<<"\n press 3 for Decimal to Hexadecimal converstion";
cout<<"\n\nEnter your choice: ";
cin>>d;
switch(d)
{
case 1:
cout<<"\nEnter decimal number: ";
cin>>n;
while (n>0)
{
b[i]=n%2;
n=n/2;
i++;
}
cout<<"\nBinary is: ";
j=i-1;
for (i=j;j>=0;j--)
{
cout<}
break;
case 2:
cout<<"\nEnter decimal number: ";
cin>>n;
while (n>0)
{
b[i]=n%8;
n=n/8;
i++;
}
cout<<"\nOctal is: ";
j=i-1;
for (i=j;j>=0;j--)
{
cout<}

break;
case 3:
cout<<"\nEnter decimal number: ";
cin>>n;
while (n>0)
{
b[i]=n%16;
n=n/16;
i++;
}
cout<<"\nHexadecimal is:";
j=i-1;
for (i=j;j>=0;j--)
{
cout<if(b[j]<10)
{
cout<}
else
{
switch(b[j])
{
case 10:
cout<<"A";
break;
case 11:
cout<<"B";
break;
case 12:
cout<<"C";
break;
case 13:
cout<<"D";
break;
case 14:
cout<<"E";
break;
case 15:
cout<<"F";
break;
}
}
}

}

Search This Blog