Professional Documents
Culture Documents
Data Structure File1
Data Structure File1
A
PRACTICAL
FILE
ON
DATA STRUCTURE
Data Structure
Department Of Computer Applications
INDEX
Sr. No. PROGRAMS Page No. Remarks
1. Program for array Traversal
2. Program for array Insertion
3. Program for array Deletion
4. Program for matrix Transpose
5. Program for matrix Addition
6. Program for matrix Multiplication
7. Program for single linked list Traversal
8. Program for single linked list Insertion
9. Program for single linked list Deletion
10. Program for doubly linked list Insertion
& Deletion
11. Program for Push & Pop into stack
12. Program for Infix to Postfix conversion
13. Program for Linear queue
14. Program for Circular queue
15. Program for implementation of Sparse
Array
16. Program for Polynomial Addition &
Multiplication
17. Program for Tree Traversal
18. Program for BST(Binary Search Tree)
19. Program for Bubble Sort
20. Program for quick Sort
21. Program for Selection Sort
22. Program for Binary search
23. Program for Linear search
Data Structure
Department Of Computer Applications
Output:
Data Structure
Department Of Computer Applications
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
Output:
Data Structure
Department Of Computer Applications
B[i][j]=A[j][i];
}
}
printf(“After transpose:\n”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“%d\t”,B[i][j]);
}
printf(“\n”);
}
getch();
}
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
s[i][j] = a[i][j]+b[i][j];
}
}
printf(“The addition of matrix is: \n”);
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
printf(“%d \n”, s[i][j]);
}
}
getch();
}
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
{
s[i][j] = s[i][j]+a[i][k]*b[k][j];
}
}
}
printf(“Matrix multiplication is: \n”);
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
printf(“%d \n”, s[i][j]);
}
}
getch();
}
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
}
this1=first;
while(this1 != NULL)
{
printf(“\n %d”,this1->info);
this1=this1->link;
}
getch();
return 0;
}
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
}
void insert_at_end(int item)
{
NODE *ptr,*loc;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
ptr->next=NULL;
if(start==NULL)
{
start=ptr;
}
else
{
loc=start;
while(loc->next!=NULL)
loc=loc->next;
loc->next=ptr;
}
}
void insert_spe(NODE *start,int item)
{
NODE *ptr,*loc;
int temp,k;
loc=start;
for(k=0;k<temp;k++)
{
loc=loc->next;
if(loc==NULL)
{
printf("node in the list at less than one\n");
return;
}
}
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
ptr->next=loc->next;
loc->next=ptr;
}
void main()
{
int choice,item,after,ch;
clrscr();
createmptylist(start);
while(1)
{
printf(" 1. Insert element at begin:\n");
printf(" 2. insert element at end:\n");
printf(" 3. Insert at specific location:\n");
printf(" 4. Traverse the list inorder:\n");
Data Structure
Department Of Computer Applications
printf(" 5. Exit.\n");
printf(" Enter your choice\n");
scanf(" %d",&choice);
switch(choice)
{
case 1: printf(" Enter the item\n");
scanf(" %d",&item);
insert_at_begin(item);
break;
case 2: printf(" Enter the item\n");
scanf(" %d",&item);
insert_at_end(item);
break;
case 3: printf(" Enter the value of item\n");
scanf(" %d",&item);
insert_spe(start,item);
break;
case 4: printf(" Traverse the list:\n");
traversinorder(start);
break;
case 5: return;
}
}
getch();
}
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
}
printf(“\n\nDo you want to continue? (1 / 0): “);
scanf(“%d”, &cont);
}
getch();
}
void insert_end()
{
int data_value;
printf(“\nEnter data of the node: “);
scanf(“%d”, &data_value);
temp = (struct node *) malloc(sizeof(struct node));
ptr = header;
while(ptr->link != NULL)
{
ptr = ptr->link;
}
temp->data = data_value;
temp->link = ptr->link;
ptr->link = temp;
}
void delete_front()
{
if(header->link == NULL)
{
printf(“\nEmpty Linked List. Deletion not possible.\n”);
}
else
{
ptr = header->link;
header->link= ptr->link;
free(ptr);
printf(“\nNode deleted from the front.\n”);
}
}
void delete_end()
{
if(header->link == NULL)
{
printf(“\nEmpty Linked List. Deletion not possible.\n”);
}
else
{
ptr = header;
while(ptr->link != NULL)
{
ptr1 = ptr;
ptr = ptr->link;
}
Data Structure
Department Of Computer Applications
ptr1->link = ptr->link;
free(ptr);
printf(“\nNode deleted from the end.\n”);
}
}
void delete_any()
{
int key;
if(header->link == NULL)
{
printf(“\nEmpty Linked List. Deletion not possible.\n”);
}
else
{
printf(“\nEnter the data of the node to be deleted: “);
scanf(“%d”, &key);
ptr = header;
while((ptr->link != NULL) && (ptr->data != key))
{
ptr1 = ptr;
ptr = ptr->link;
}
if(ptr->data == key)
{
ptr1->link = ptr->link;
free(ptr);
printf(“\nNode with data %d deleted.\n”, key);
}
else
{
printf(“\nValue %d not found. Deletion not possible.\n”, key);
}
}
}
void display()
{
printf(“\nContents of the linked list are: \n”);
ptr = header;
while(ptr->link != NULL)
{
ptr = ptr->link;
printf(“%d “, ptr->data);
}
}
Data Structure
Department Of Computer Applications
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
break;
case 4:
delete();
break;
case 5:
traversebeg();
break;
case 6:
temp2 = h;
if (temp2 == NULL)
printf(“\n Error : List empty to display “);
else
{
printf(“\n Reverse order of linked list is : “);
traverseend(temp2->n);
}
break;
case 7:
search();
break;
case 8:
sort();
break;
case 9:
update();
break;
case 10:
exit(0);
default:
printf(“\n Wrong choice menu”);
}
}
}
void create()
{
int data;
temp =(struct node *)malloc(1*sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf(“\n Enter value to node : “);
scanf(“%d”, &data);
temp->n = data;
count++;
}
void insert1()
{
if (h == NULL)
{
create();
Data Structure
Department Of Computer Applications
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}
void insert2()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
void insert3()
{
int pos, i = 2;
printf(“\n Enter position to be inserted : “);
scanf(“%d”, &pos);
temp2 = h;
if ((pos < 1) || (pos >= count + 1))
{
printf(“\n Position out of range to insert”);
return;
}
if ((h == NULL) && (pos != 1))
{
printf(“\n Empty list cannot insert other than 1 st position”);
return;
}
if ((h == NULL) && (pos == 1))
{
create();
h = temp;
temp1 = h;
return;
Data Structure
Department Of Computer Applications
}
else
{
while (i < pos)
{
temp2 = temp2->next;
i++;
}
create();
temp->prev = temp2;
temp->next = temp2->next;
temp2->next->prev = temp;
temp2->next = temp;
}
}
void delete()
{
int i = 1, pos;
printf(“\n Enter position to be deleted : “);
scanf(“%d”, &pos);
temp2 = h;
if ((pos < 1) || (pos >= count + 1))
{
printf(“\n Error : Position out of range to delete”);
return;
}
if (h == NULL)
{
printf(“\n Error : Empty list no elements to delete”);
return;
}
else
{
while (i < pos)
{
temp2 = temp2->next;
i++;
}
if (i == 1)
{
if (temp2->next == NULL)
{
printf(“Node deleted from list”);
free(temp2);
temp2 = h = NULL;
return;
}
}
if (temp2->next == NULL)
Data Structure
Department Of Computer Applications
{
temp2->prev->next = NULL;
free(temp2);
printf(“Node deleted from list”);
return;
}
temp2->next->prev = temp2->prev;
if (i != 1)
temp2->prev->next = temp2->next; /* Might not need this statement if i == 1 check */
if (i == 1)
h = temp2->next;
printf(“\n Node deleted”);
free(temp2);
}
count--;
}
void traversebeg()
{
temp2 = h;
if (temp2 == NULL)
{
printf(“List empty to display \n”);
return;
}
printf(“\n Linked list elements from beginning : “);
while (temp2->next != NULL)
{
printf(“ %d “, temp2->n);
temp2 = temp2->next;
}
printf(“ %d “, temp2->n);
}
void traverseend(int i)
{
if (temp2 != NULL)
{
i = temp2->n;
temp2 = temp2->next;
traverseend(i);
printf(“ %d “, i);
}
}
void search()
{
int data, count = 0;
temp2 = h;
if (temp2 == NULL)
{
printf(“\n Error : List empty to search for data”);
Data Structure
Department Of Computer Applications
return;
}
printf(“\n Enter value to search : “);
scanf(“%d”, &data);
while (temp2 != NULL)
{
if (temp2->n == data)
{
printf(“\n Data found in %d position”,count + 1);
return;
}
else
temp2 = temp2->next;
count++;
}
printf(“\n Error : %d not found in list”, data);
}
void update()
{
int data, data1;
printf(“\n Enter node data to be updated : “);
scanf(“%d”, &data);
printf(“\n Enter new data : “);
scanf(“%d”, &data1);
temp2 = h;
if (temp2 == NULL)
{
printf(“\n Error : List empty no node to update”);
return;
}
while (temp2 != NULL)
{
if (temp2->n == data)
{
temp2->n = data1;
traversebeg();
return;
}
else
temp2 = temp2->next;
}
printf(“\n Error : %d not found in list to update”, data);
}
void sort()
{
int i, j, x;
temp2 = h;
temp4 = h;
if (temp2 == NULL)
Data Structure
Department Of Computer Applications
{
printf(“\n List empty to sort”);
return;
}
for (temp2 = h; temp2 != NULL; temp2 = temp2->next)
{
for (temp4 = temp2->next; temp4 != NULL; temp4 = temp4->next)
{
if (temp2->n > temp4->n)
{
x = temp2->n;
temp2->n = temp4->n;
temp4->n = x;
}
}
}
traversebeg();
}
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
int ch;
clrscr();
top=-1;
do
{
do
{
printf(“\n MAIN MENU”);
printf(“\n1. PUSH(INSERT) in the stack”);
printf(“\n2. POP (DELETE) from the stack”);
printf(“\n3. EXIT (END THE EXECUTION)”);
printf(“\nEnter your choice:”);
scanf(“%d”,&ch);
if(ch<1||ch>3)
printf(“\nInvalid choice,try again”);
}
while(ch<1||ch>3);
switch(ch)
{
case 1:
printf(“\nEnter the element to be pushed:”);
scanf(“%d”,&item);
printf(“%d”,item);
push(stack,item);
if (status)
{
printf(“\n After pushing”);
display(stack);
if(top==(MAX-1))
printf(“\nThe stack is full”);
}
else
printf(“\nStack overflow on push”);
break;
case 2:
item=pop(stack);
if(status)
{
printf(“\nThe poped item if %d. After poping:”);
display(stack);
}
else
printf(“\nStack underflow on pop”);
break;
default:
printf(“\nEND OF EXECUTION”);
}
}
while(ch!=3);
Data Structure
Department Of Computer Applications
}
getch();
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
pos++;
}
push (symbol);
break;
default:postfix[pos++]=symbol;
break;
}
index++;
}
while(top>0)
{
temp=pop();
postfix[pos++]=temp;
}
postfix[pos++]='\0';
puts(postfix);
return;
}
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
}
}
delete()
{
if(front==-1||front>rear)
{
printf("Queue underflow\n");
return;
}
else
{
printf("Element deleted from queue is:%d\n");
front=front+1;
}
}
display()
{
int i;
if(front==-1)
printf("Queue is empty\n");
else
{
printf("Queue is:\n");
for(i=front;i<=rear;i++)
printf("%d",queue_array[i]);
printf("\n");
}
}
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
#include<stdio.h>
#include<conio.h>
#define MAX 3
int q[10],front=0,rear=-1;
main()
{
int ch;
void insert();
void delet();
void display();
void exit();
clrscr();
printf("\nCircular queue operations\n");
printf("1.Isert\n2.delete\n3.display\n4.exit\n");
while(1)
{
printf("Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit();
default:printf("Invalid option\n");
}
}
}
void insert()
{
int x;
if((front==0&&rear==MAX-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("Enter element to be inserted:");
scanf("%d",&x);
if(rear==MAX-1&&front>0)
Data Structure
Department Of Computer Applications
{
rear=0;
q[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
}
}
}
void delet()
{
int a;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
getch();
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else
if(front==MAX-1)
{
a=q[front];
front=0;
}
else
a=q[front++];
printf("Dleted element is:%d\n",a);
}
void display()
{
int i,j;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
getch();
}
if(front>rear)
{
for(i=0;i<=rear;i++)
printf("\t%d",q[i]);
for(j=front;j<=MAX-1;j++)
printf("\t%d",q[j]);
Data Structure
Department Of Computer Applications
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
printf("\n");
}
getch();
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
ptr=ptr->link;
if(ptr!=NULL)
printf("+");
else
printf("\n");
}
}
void poly_add(struct node*p1, struct node*p2)
{
struct node* start3;
start3=NULL;
while(p1!=NULL&&p2!=NULL)
{
if(p1->expo>p2->expo)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
}
else if(p2->expo>p1->expo)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
else if(p1->expo==p2->expo)
{
start3=insert(start3,p1->coef+p2->coef,p1->expo);
p1=p1->link;
p2=p2->link;
}
}
while(p1!=NULL)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
}
while(p2!=NULL)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
printf("Added polynomial is:");
display(start3);
}
void poly_multi(struct node*p1,struct node*p2)
{
struct node*start3;
struct node*p2_beg=p2;
start3=NULL;
if(p1==NULL||p2==NULL)
Data Structure
Department Of Computer Applications
{
printf("Multiplied polynomial is zero polynomial\n");
return;
}
while(p1!=NULL)
{
p2=p2_beg;
while(p2!=NULL)
{
start3=insert_s(start3,p1->coef*p2->coef,p1->expo+p2->expo);
p2=p2->link;
}
p1=p1->link;
}
printf("Multiplied polynomial is:");
display(start3);
}
#include<stdio.h>
struct rec
{
long num;
struct rec *left;
struct rec *right;
};
struct rec *tree=NULL;
struct rec *insert(struct rec *tree,long num);
void preorder(struct rec *tree);
void inorder(struct rec *tree);
void postorder(struct rec *tree);
int count=1;
main()
{
int choice;
long digit;
do
{
choice=select();
switch(choice)
{
case 1: puts("Enter integer: To quit enter 0");
scanf("%ld",&digit);
while(digit!=0)
{
tree=insert(tree,digit);
scanf("%ld",&digit);
}continue;
case 2: puts("\npreorder traversing TREE");
preorder(tree);continue;
case 3: puts("\ninorder traversing TREEE");
inorder(tree);continue;
case 4: puts("\npostorder traversing TREE");
postorder(tree);continue;
case 5: puts("END");exit(0);
}
}while(choice!=5);
}
int select()
{
int selection;
do
{
puts("Enter 1: Insert a node in the BT");
puts("Enter 2: Display(preorder)the BT");
puts("Enter 3: Display(inorder)the BT");
puts("Enter 4: Display(postorder)the BT");
puts("Enter 5: END");
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
}
}
int select()
{
int selection;
do
{
puts("Enter 1: Insert a node in the BT");
puts("Enter 2: Display(preorder)the BT");
puts("Enter 3: Display(inorder)the BT");
puts("Enter 4: Display(postorder)the BT");
puts("Enter 5: END");
puts("Enter your choice");
scanf("%d",&selection);
if((selection<1)||(selection>5))
{
puts("wrong choice:Try again");
getch();
}
}
while((selection<1)||(selection>5));
return (selection);
}
struct rec *insert(struct rec *tree,long digit)
{
if(tree==NULL)
{
tree=(struct rec *)malloc(sizeof(struct rec));
tree->left=tree->right=NULL;
tree->num=digit;count++;
}
else
if(count%2==0)
tree->left=insert(tree->left,digit);
else
tree->right=insert(tree->right,digit);
return(tree);
}
void preorder(struct rec *tree)
{
if(tree!=NULL)
{
printf("%12ld\n",tree->num);
preorder(tree->left);
preorder(tree->right);
}
}
void inorder(struct rec *tree)
{
Data Structure
Department Of Computer Applications
if(tree!=NULL)
{
inorder(tree->left);
printf("%12ld\n",tree->num);
inorder(tree->right);
}
}
void postorder(struct rec *tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
printf("%12ld\n",tree->num);
}}
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
#include<conio.h>
#include<stdlib.h>
struct treenode
{
int data;
struct treenode *left, *right;
};
struct treenode *root=NULL;
struct treenode *createnode(int data)
{
struct treenode *newnode;
newnode=(struct treenode *)malloc(sizeof(struct treenode));
newnode->data=data;
newnode->left=NULL;
newnode->right=NULL;
return(newnode);
}
void insertion(struct treenode**node,int data)
{
if(*node==NULL)
{
*node= createnode(data);
}
else if(data<(*node)->data)
{
insertion(&(*node)->left,data);
}
else if(data>(*node)->data)
{
insertion(&(*node)->right,data);
}
}
void deletion(struct treenode **node,struct treenode **parent,int data)
{
struct treenode *tmpnode,*tmpparent;
if(*node==NULL)
return;
if((*node)->data==data)
{
if(!(*node)->left&&!(*node)->right)
{
if(parent)
{
if((*parent)->left==*node)
(*parent)->left=NULL;
else
(*parent)->right=NULL;
free(*node);
}
Data Structure
Department Of Computer Applications
}
else if(!(*node)->right&&(*node)->left)
{
tmpnode=*node;
(*parent)->right=(*node)->left;
free(tmpnode);
*node=(*parent)->right;
}
else if((*node)->right&&!(*node)->left)
{
tmpnode= *node;
(*parent)->left=(*node)->right;
free(tmpnode);
(*node)=(*parent)->left;
}
else if(!(*node)->right->left)
{
tmpnode= *node;
(*node)->right->left=(*node)->left;
(*parent)->left=(*node)->right;
free(tmpnode);
*node=(*parent)->left;
}
else
{
tmpnode=(*node)->right;
while (tmpnode->left)
{
tmpparent=tmpnode;
tmpnode=tmpnode->left;
}
tmpparent->left= tmpnode->right;
tmpnode->left=(*node)->left;
tmpnode->right=(*node)->right;
free(*node);
*node=tmpnode;
}
}
else if(data<(*node)->data)
{
deletion(&(*node)->left,node,data);
}
else if(data>(*node)->data)
{
deletion(&(*node)->right,node,data);
}
}
void findElement(struct treenode *node,int data)
{
Data Structure
Department Of Computer Applications
if(!node)
return;
else if(data<node->data)
{
findElement(node->left,data);
}
else if(data>node->data)
{
findElement(node->right,data);
}
else
printf("data found:%d\n",node->data);
return;
}
int main()
{
int data,ch;
clrscr();
while(1)
{
printf("\n1.Insertion in binary search tree");
printf("\n2.deletion in binary search tree");
printf("\n3.search element in binary search tree");
printf("\n4.exit");
printf("\n Enter ur choice:");
scanf("\t%d",&ch);
switch (ch)
{
case 1:
while(1)
{
printf("\n Enter ur data:");
scanf("\n%d",&data);
insertion(&root, data);
printf("continue insertion(0/1):");
scanf("%d",&ch);
if(!ch)
break;
}
break;
case 2:
printf("enter ur data:");
scanf("%d",&data);
deletion(&root, NULL,data);
break;
case 3:
printf("enter value for data");
scanf("%d",&data);
Data Structure
Department Of Computer Applications
findElement(root,data);
break;
case 4:
exit(0);
default:
printf("You have entered wrong opertaion\n");
break;
}
}
return(0);
}
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
#include<conio.h>
void main()
{
int a[100] ,n,i,j,temp;
clrscr();
printf("How many elements?\n");
scanf("%d",&n);
printf("Enter the element of array:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("Elelments of array after the sorting are:\n");
for(i=1;i<=n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
void main()
{
int a[100],n,i,j,temp,loc,min;
clrscr();
printf("How many elements?\n");
scanf("%d",&n);
printf("Enter the elements of array:\n");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
min=a[0];
for(i=0;i<=n-1;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<=n-1;j++)
{
if(a[j]<min)
{
min=a[j];
loc=j;
}
}
if(loc!=1)
{
temp=a[i];
a[i]=a[loc] ;
a[loc]=temp;
}
}
printf("The number after selection sorting are:\n");
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
getch();
}
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
int a[100],i,loc,mid,beg,end,n,flag=0,item;
clrscr();
printf("How many elements:");
scanf("%d",&n);
printf("Enter the elements of the array:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be search:");
scanf("%d",&item);
loc=0;
beg=0;
end=n-1;
while((beg<=end)&&(item!=a[mid]))
{
mid=((beg+end)/2);
if(item==a[mid])
{
printf("Search is successfull");
loc=mid;
printf("\nPosition of the item is: %d",loc+1);
flag=flag+1;
}
if(item<a[mid])
end=mid-1;
else
beg=mid+1;
}
if(flag==0)
{
printf("Search is not successfull");
}
getch();
}
Output:
Data Structure
Department Of Computer Applications
Data Structure
Department Of Computer Applications
void main()
{
void input(void);
clrscr();
input();
getch();
}
void input(void)
{
void output(int a[],int n);
void quick_sort(int a[],int l,int h);
printf("How many elements?\n");
scanf("%d",&n);
printf("Enter the elements:\n");
for(i=0;i<n-1;i++)
{
scanf("%d",&a[i]);
}
l=0;
h=n-1;
quick_sort(a,l,h);
printf("Sorted array:\n");
output(a,n);
}
void quick_sort(int a[],int l,int h)
{
int temp,key,low,high;
low=1;
high=h;
key=a[(low+high)/2];
do
{
while(key>a[low])
{
low++;
}
while(key<a[high])
{
high--;
}
if(low<=high)
{
temp=a[low];
a[low++]=a[high];
a[high--]=temp;
}
}
while(low<=high);
if(1<high)
Data Structure
Department Of Computer Applications
quick_sort(a,l,high);
if(low<h)
quick_sort(a,low,h);
}
void output(int a[],int n)
{
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
getch();
}
Data Structure
Department Of Computer Applications
int a[100],n,i,item,loc=-1;
clrscr();
printf("Enter the number of element:");
scanf("%d",&n);
printf("Enter the number:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the no. to be search:");
scanf("%d",&item);
for(i=0;i<=n;i++)
{
if(item==a[i])
{
loc=i;
break;
}
}
if(loc>=0)
printf("%d is found in position %d",item,loc+1);
else
printf("Item does not exist");
getch();
}
Output:
Data Structure