Array Sorting and Searching Algorithms
Array Sorting and Searching Algorithms
OF CSE
Program Statement:
Program:
#include<stdio.h>
#include<conio.h>
main(
)
{ int a[20],n,i;
clrscr( );
printf("\n enter how many elements you want to sort(max.20):");
for(i=0;i<n;i++)
{
printf("\n enter the value for a[%d]:",i);
scanf("%d",&a[i]); /* read the values */
}
for(i=0;i<n;i++)
{
printf("%d\t",a[i]); /* print unsorted list */
}
for(i=0;i<n;i++)
{
printf("%d\t",a[i]); /* print sorted list */
}
getch( );
}
Input:
Output:
Program Statement:
Program:
#include<stdio.h>
#include<conio.h>
main(
)
{ int a[20],n,i;
clrscr( );
printf("\n enter how many elements you want to sort(max.20):");
for(i=0;i<n;i++)
{
printf("\n enter the value for a[%d]:",i);
scanf("%d",&a[i]); /* read the values */
}
for(i=0;i<n;i++)
{
printf("%d\t",a[i]); /* print unsorted list */
}
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch( );
}
}
temp=a[min];
a[min]=a[i];
a[i]=temp;
}
}
Input:
Output:
Program Statement:
Program:
#include<stdio.h>
#include<conio.h>
int i,j,k,flag;
void main( )
{
int a[20],n;
clrscr( );
printf("\n Enter how many elements you want to sort(max.20):");
scanf("%d",&n); /* read number of elements in the list*/
printf("\n Enter the %d elements",n);
for(i=0;i<n;i++)
{
printf("\n enter the value for a[%d]:",i);
scanf("%d",&a[i]); /* read the values */
}
printf("\n before sorting, the elements are:");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]); /* print unsorted list */
}
heapsort(a,n);
Input:
Output:
Program Statement:
Program:
#include<stdio.h>
#include<conio.h>
int a[100],i,n,key,flag=0;
main(
)
{ clrscr( );
printf("\n enter how many elements you want to search(max.100):");
for(i=0;i<n;i++)
{
printf("\n enter the value for a[%d]:",i);
scanf("%d",&a[i]); /* read the values */
}
getch( );
}
if(flag>0)
{
printf("\n search is successful");
printf("\n element %d is found at %d position in list",key,i+1);
}
else
{
printf("\n search is unsuccessfull");
printf("\n element %d is not found in list",key);
}
}
Output:
Input:
Output:
Program Statement:
Program:
#include<stdio.h>
#include<conio.h>
main(
)
{ int a[100],l,u,mid,i,n,key,flag=0;
clrscr( );
for(i=0;i<n;i++)
{
printf("\n enter the value for a[%d]:",i);
scanf("%d",&a[i]); /* read the values */
}
l=0;
u=n-1;
mid=(l+u)/2;
while(l<= u)
{
if(key = = a[mid])
{
flag=1;
break;
}
if(key > a[mid])
{
l=mid+1;
}
else
{
u=mid-1;
}
mid=(l+u)/2;
}
if(flag > 0)
{
printf("\n search is successful");
printf("\n element %d is found at %d position in list",key,mid+1);
}
else
{
printf("\n search is unsuccessfull");
printf("\n element %d is not found in list",key);
}
getch( );
}
Input:
Output:
Input:
Output:
Program Statement:
5 b)Write a program to perform Binary Search on the elements of a given array using recursion.
Program:
#include<stdio.h>
#include<conio.h>
main(
)
{ int a[100],l,u,mid,i,n,key,flag;
clrscr( );
printf("\n enter how many elements you want to search(max.100):");
for(i=0;i<n;i++)
{
printf("\n enter the value for a[%d]:",i);
scanf("%d",&a[i]); /* read the values */
}
if(flag>0)
{
printf("\n search is successful");
printf("\n element %d is found in list",key);
}
else
{
printf("\n search is unsuccessfull");
printf("\n element %d is not found in list",key);
}
getch( );
}
if(key == a[mid])
{
return(mid);
}
if(key < a[mid])
{
return(bsearch(a,n,l,mid-1,key));
}
else
{
return(bsearch(a,n,mid+1,u,key));
}
}
Run-1:
Input:
Output:
Run-2:
Input:
Output:
Program Statement:
Program:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define MAX 20
char infix[MAX],post[MAX],s[MAX],ch,t,x,ele;
int i=0,j=0,top= -1;
int main( )
{
clrscr( );
printf("\n enter the infix expression:");
scanf("%s",infix);
push('#');
while(infix[i]!='\0')
{
t=infix[i];
if(isalpha(t))
{
post[j++]=t;
}
else
{
if(t=='+' || t=='-' || t=='*' || t=='/' || t=='(' || t==')' || t=='^')
switch(t)
{
case '(': push(t);
break;
case '-':
case '+': check( );
push(t);
break;
case '*':
case '/': check( );
push(t);
break;
case '^': check( );
push(t);
break;
case ')': do
{
x=pop( );
post[j++]=x;
}while(x!= '(' );
j=j-1;
break;
}
}
i=i+1;
}
while(s[top]!='#')
{
post[j++]=pop();
}
post[j]='\0';
printf("\n the postfix notation of given infix %s is %s",infix,post);
getch( );
return 0;
}
Program Statement:
Program:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<math.h>
#include<stdlib.h>
#define MAX 50
char postfix[MAX],ch;
int i=0,top= -1;
float s[MAX],op1,op2,temp,val,res;
float pop( )
{
return (s[top--]);
}
float operate(float op1,float op2,char ch)
{
switch(ch)
{
case '+':temp=op1+op2;
break;
case '-':temp=op1-op2;
break;
case '*':temp=op1*op2;
break;
case '/':temp=op1/op2;
break;
case '^':temp=pow(op1,(int)op2);
break;
}
return(temp);
}
void push(float val)
{
top++;
s[top]=val;
}
int main( )
{
clrscr( );
printf("\n enter the postfix expression:");
scanf("%s",postfix); while(postfix[i]!='\
0')
{
ch=postfix[i];
if(isalpha(ch))
{
printf("\n enter value for %c:",ch);
scanf("%f",&val);
push(val);
}
else
{
if(ch= ='*' || ch= ='/' || ch= ='+' || ch= ='-' || ch= ='^' )
{
op2=pop( );
op1=pop( );
res=operate(op1,op2,ch);
push(res);
}
}
i=i+1;
}
temp=pop( );
printf("\n the simplified answer for %s is %f",postfix,temp);
getch( );
return 0;
}
Input:
Output:
Input:
Output:
Program Statement:
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 5
int a[MAX];
/* function prototypes */
void push( );
void pop( );
void display( );
void push( )
{
int ele;
if(top= =MAX-1)
{
printf("stack is overflow\n");
return;
}
top++;
printf("enter the element:");
scanf("%d",&ele);
a[top]=ele;
}
void pop( )
{
int ele;
if(top= = -1)
{
printf("stack is underflow\n");
return;
}
ele=a[top];
Annamacharya Institute of Technology and Sciences Page
,Kadapa 23
DATA STRUCTURES DEPT. OF CSE
top- -;
printf("the deleted element is:%d",ele);
}
void display( )
{
int i;
if(top= = -1)
{
printf("stack is underflow\n");
return;
}
printf("the elements are:");
for(i=0;i<=top;i++)
{
printf("%d\t",a[i]);
}
}
main(
)
{ int ch;
clrscr( );
while(1)
{
printf("\n Menu \n");
printf(" 1. push \n");
printf(" 2. pop \n");
printf(" 3. display \n");
printf(" 4. exit \n");
switch(ch)
{
case 1:push( );
break;
case 2:pop( );
break;
case 3:display( );
break;
case 4:exit(0);
default:printf("Invalid option\n");
}
}
}
Output:
Program Statement:
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 5
int a[MAX];
/* function prototypes */
void insert( );
void delete( );
void display( );
void insert( )
{
int ele;
if(rear= =MAX-1)
{
printf("queue is overflow\n");
return;
}
if(front= = -1)
{
rear=0;
front=0;
}
else
{
rear++;
}
a[rear]=ele;
void delete( )
{
int ele;
if(rear= = -1)
{
printf("queue is underflow\n");
return;
}
ele=a[front];
if(rear= =front)
{
rear = -1;
front = -1;
}
else
{
front++;
}
printf("the deleted element is:%d",ele);
}
void display( )
{
int i;
if(rear = = -1)
{
printf("queue is underflow\n");
return;
}
printf("the elements are:");
for(i=front;i<= rear;i++)
{
printf("%d\t",a[i]);
}
}
main(
)
{ int ch;
clrscr( );
while(1)
{
printf("\n Menu \n");
printf(" 1. insert \n");
printf(" 2. delete \n");
switch(ch)
{
case 1:insert( );
break;
case 2:delete( );
break;
case 3:display( );
break;
case 4:exit(0);
default:printf("Invalid option\n");
}
}
}
Output:
Program Statement:
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 5
int a[MAX];
/* function prototypes */
void insert( );
void delete( );
void display( );
void insert( )
{
int ele;
if(front= =(rear+1) % MAX)
{
printf(" circular queue is overflow\n");
return;
}
if(front= = -1)
{
rear=0;
front=0;
}
else
{
rear=(rear+1) % MAX;
}
a[rear]=ele;
void delete( )
{
int ele;
if(rear= = -1)
{
printf(" circular queue is underflow\n");
return;
}
ele=a[front];
if(rear= =front)
{
rear = -1;
front= -1;
}
else
{
front=(front+1) % MAX;
}
printf("the deleted element is:%d",ele);
}
void display( )
{
int i;
if(rear= = -1)
{
printf("circular queue is underflow\n");
return;
}
printf("the elements are:");
if(front<= rear)
{
for(i=front;i<=rear;i++)
{
printf("%d\t",a[i]);
}
}
else
{
for(i=front;i<MAX;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<= rear;i++)
{
printf("%d\t",a[i]);
Annamacharya institute of technology and sciences,kadapa Page 33
DATA STRUCTURES DEPT. OF CSE
}
}
}
main(
)
{ int ch;
clrscr( );
while(1)
{
printf("\n Menu \n");
printf(" 1. insert \n");
printf(" 2. delete \n");
printf(" 3. display \n");
printf(" 4. exit \n");
switch(ch)
{
case 1:insert( );
break;
case 2:delete( );
break;
case 3:display( );
break;
case 4:exit(0);
default:printf("Invalid option\n");
}
}
}
Output:
Program Statement:
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
#define NULL 0
struct node
{
int data;
struct node *link;
}*temp,*head,*temp1,*top;
/* function prototypes */
void push();
void pop();
void display();
void push()
{
int ele;
if(temp==NULL)
{
printf("\n memory allocation error");
return;
}
if(head->link==NULL)
{
temp->data=ele;
head->link=temp;
temp->link=NULL;
top=temp;
}
else
{
temp->link=head->link;
head->link=temp;
temp->data=ele;
top=temp;
}
}
void pop()
{
int ele;
if(top==NULL)
{
printf("\n linked stack is empty");
return;
}
temp=top;
temp1=temp->link;
head->link=temp1;
ele=temp->data;
free(temp);
top=temp1;
void display()
{
if(head->link==NULL)
{
printf("\n linked stack is empty");
return;
}
printf("\n the elements are:");
temp=head->link; while(temp!
=NULL)
{
printf("%d\t",temp->data);
temp=temp->link;
}
}
main()
{
int ch;
top=NULL;
clrscr();
while(1)
{
printf("\n Menu");
printf("\n [Link]
")
;
printf("\n [Link] ” );
printf("\n [Link] ");
printf("\n [Link] ");
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);
Program Statement:
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
#define NULL 0
struct node
{
int data;
struct node *link;
}*temp,*head,*front,*rear;
/* function prototypes
*/ void insert();
void delete();
void display();
void insert()
{
int ele;
if(temp==NULL)
{
printf("\n memory allocation
error"); return;
}
if(head->link==NULL)
{
temp->data=ele;
head->link=temp;
temp->link=NULL;
front=rear=temp;
}
else
{
temp->link=NULL;
rear->link=temp;
temp->data=ele;
rear=temp;
}
}
void delete()
{
int ele;
if(front==NULL)
{
printf("\n linked queue is
empty"); return;
}
if(front==rear)
{
ele=front->data;
front=rear=NULL;
}
else
{
ele=front->data;
temp=front;
front=front->link;
free(temp);
}
void display()
{
if(rear==NULL)
{
printf("\n linked queue is
empty"); return;
}
printf("\n the elements are:");
temp=front;
while(temp!=rear-
>link)
{
printf("%d\t",temp->data);
temp=temp->link;
}
}
main()
{
int
ch;
front=rear=NULL;
clrscr();
while(1)
{
printf("\n Menu\n");
printf("\n [Link] \n");
printf("\n [Link] \n");
printf("\n [Link] \n");
printf("\n [Link] \n");
switch(ch)
{
case 1:insert();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:exit(0);
Program Statement:
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
#define NULL 0
struct Node
{
int data;
struct Node *link;
}*head,*temp,*front,*rear,*temp1;
void enqueue( );
void dequeue( );
void display( );
void enqueue( )
{
int ele;
printf("\n\t Enter the element:");
scanf("%d",&ele);
temp=(struct Node*)malloc(sizeof(struct Node));
if(temp= =NULL)
{
printf("\n Memory allocation error");
return;
}
if(front= =NULL)
{
temp->data=ele;
head->link=temp;
front=rear=temp;
rear->link=head;
}
else
{
temp1=head->link;
while(temp1->link!=head)
{
temp1=temp1->link;
}
temp1->link=temp;
temp->data=ele;
rear=temp;
rear->link=head;
}
}
void dequeue( )
{
int ele;
if(front= =NULL)
{
printf("\n\t linked circular queue is empty");
return;
}
if(front= =rear)
{
temp=head->link;
ele=front->data;
head->link=NULL;
front=rear=NULL;
free(temp);
}
else
{
temp=head->link;
head->link=temp->link;
ele=front->data;
front=temp->link;
rear->link=head;
free(temp);
}
printf("\n\t The deleted element is %d",ele);
}
void display()
{
if(rear= =NULL)
{
printf("\n\t linked circular queue is empty");
return;
}
printf("\n\t The elements are:");
temp=front;
while(temp!=rear)
{
printf("%d\t",temp->data);
temp=temp->link;
}
printf("%d\t",temp->data);
}
void main( )
{
int ch;
front=rear=NULL;
clrscr( );
while(1)
{
printf("\n Menu ");
printf("\n [Link]");
printf("\n [Link]");
printf("\n [Link]");
printf("\n [Link]");
printf("\n\t Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: enqueue( );
display( );
break;
case 2: dequeue( );
display( );
break;
case 3: display( );
break;
case 4: exit(0);
default:printf("\n invalid option");
}
}
}
Output:
Program Statement:
8 .Write a program to perform the operations creation, insertion, deletion, and traversing a singly
linked list.
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
#define NULL 0
struct node
{
int data;
struct node *link;
}*temp,*head,*temp1,*next;
/* function prototype */
void insertf()
{
int ele;
if(temp==NULL)
{
printf("\n memory allocation error");
return;
}
if(head->link==NULL)
{
temp->data=ele;
head->link=temp;
temp->link=NULL;
}
else
{
temp->link=head->link;
head->link=temp;
temp->data=ele;
}
}
void insertl()
{
int ele;
if(temp==NULL)
{
printf("\n memory allocation error");
return;
}
if(head->link==NULL)
{
temp->data=ele;
head->link=temp;
temp->link=NULL;
}
else
{
temp1=head->link;
while(temp1->link!=NULL)
{
temp1=temp1->link;
}
temp1->link=temp;
temp->data=ele;
temp->link=NULL;
}
}
void insertsp()
{
int ele;
int pos,i;
if(temp==NULL)
{
printf("\n memory allocation error");
return;
}
if(head->link==NULL)
{
temp->data=ele;
head->link=temp;
temp->link=NULL;
}
else
{
temp1=head; i=1;
while(i<pos)
{
temp1=temp1->link; i+
+;
}
temp->link=temp1->link;
temp1->link=temp;
temp->data=ele;
}
}
void deletef()
{
int ele;
if(head->link==NULL)
{
printf("\n singly linked list is
empty"); return;
}
temp=head->link;
temp1=temp->link;
head->link=temp1;
ele=temp->data;
free(temp);
printf("\n the deleted element is:%d",ele);
}
void deletel()
{
int ele;
if(head->link==NULL)
{
printf("\n singly linked list is
empty"); return;
}
temp1=head; while(temp1-
>link!=NULL)
{
temp=temp1;
temp1=temp1->link;
}
temp->link=NULL;
ele=temp1->data;
free(temp1);
void deletesp()
{
int ele;
int pos,i;
if(head->link==NULL)
{
printf("\n singly linked list is
empty"); return;
}
temp1=head;
i=0;
while(i<pos)
{
temp=temp1;
i++;
temp1=temp1->link;
}
temp->link=temp1->link;
ele=temp1->data;
printf("\n the deleted element is:%d",ele);
free(temp1);
void display()
{
if(head->link==NULL)
{
printf("\n singly linked list is
empty"); return;
}
printf("\n the elements are:");
temp=head->link; while(temp!
=NULL)
{
printf("%d\t",temp->data);
temp=temp->link;
}
}
main()
{
int ch;
head->link=NULL; clrscr();
while(1)
{
printf("\n Menu\n");
printf("\n [Link] at front \n");
printf("\n [Link] at last \n");
printf("\n [Link] at any specified position \n");
printf("\n [Link] at front \n");
printf("\n [Link] at last \n");
printf("\n [Link] at any specified position \n");
printf("\n [Link] \n");
printf("\n [Link] \n");
switch(ch)
{
case 1:insertf();
break;
case 2:insertl();
break;
case 3:insertsp();
break;
case 4:deletef();
break;
case 5:deletel();
break;
case 6:deletesp();
break;
case 7:display();
break;
case 8:exit(0);
default:printf("\n invalid option");
}
}
}
Run:
Program Statement:
9 Write a program to perform the operations creation, insertion, deletion, and traversing a
doubly linked list.
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
#define NULL 0
struct node
{
int data;
struct node *flink;
struct node *blink; /* flink->forward link & blink->backword link */
}*next,*head,*temp,*temp1;
void insertF( )
{
int ele;
printf("\n\t Enter the element:");
scanf("%d",&ele);
temp=(struct node*)malloc(sizeof(struct node));
if(temp= =NULL)
{
printf("\n Memory allocation error");
return;
}
if(head->flink= =NULL)
{
head->flink=temp;
temp->data=ele;
temp->blink=head;
temp->flink=NULL;
}
else
{
temp->flink=head->flink;
temp->blink=head;
head->flink=temp;
temp->data=ele;
temp1=temp->flink;
temp1->blink=temp;
}
}
void insertE( )
{
int ele;
printf("\n\t Enter the element:");
scanf("%d",&ele);
temp=(struct node*)malloc(sizeof(struct node));
if(temp= =NULL)
{
printf("\n Memory allocation error");
return;
}
if(head->flink= =NULL)
{
temp->data=ele;
temp->blink=head;
head->flink=temp;
temp->flink=NULL;
}
else
{
temp1=head->flink;
while(temp1->flink!=NULL)
{
temp1=temp1->flink;
}
temp1->flink=temp;
temp->data=ele;
temp->flink=NULL;
temp->blink=temp1;
}
}
void insertA( )
{
int ele;
int pos,i;
printf("\n\t Enter the element:");
scanf("%d",&ele);
printf("\n\t Enter the position:");
scanf("%d",&pos);
temp=(struct node*)malloc(sizeof(struct node));
if(temp= =NULL)
{
printf("\n Memory allocation error");
return;
}
if(head->flink= =NULL)
{
temp->data=ele;
head->flink=temp;
temp->blink=head;
temp->flink=NULL;
}
else
{
temp1=head;
i=1;
while(i<pos)
{
temp1=temp1->flink; i+
+;
}
temp->flink=temp1->flink;
temp1->flink=temp;
temp->data=ele;
temp->blink=temp1;
temp=temp->flink;
temp->blink=temp;
}
}
void deleteF( )
{
int ele;
if(head->flink= =NULL)
{
printf("\n\t Doubly linked list is empty");
return;
}
temp=head->flink;
temp1=temp->flink;
head->flink=temp1;
temp1->blink=head;
ele=temp->data;
free(temp);
printf("\n\t The deleted element is %d",ele);
}
void deleteE( )
{
int ele;
if(head->flink= =NULL)
{
printf("\n\t Doubly linked list is empty");
return;
}
temp1=head;
while(temp1->flink!=NULL)
{
temp=temp1;
temp1=temp1->flink;
}
temp->flink=NULL;
ele=temp1->data;
free(temp1);
printf("\n\t The deleted element is %d",ele);
}
void deleteA( )
{
int ele;
int pos,i;
if(head->flink= =NULL)
{
printf("\n\t Doubly linked list is empty");
return;
}
printf("\n\t Enter the position:");
scanf("%d",&pos);
temp1=head;
i=0;
while(i<pos)
{
temp=temp1;
i++;
temp1=temp1->flink;
}
temp->flink=temp->flink;
temp=temp->flink;
temp->blink=temp1->blink;
ele=temp1->data;
printf("\n\t The deleted element is %d",ele);
free(temp1);
}
void fDisplay( )
{
if(head->flink= =NULL)
{
printf("\n\t Doubly linked list is empty");
return;
}
printf("\n\t the elements are:");
temp=head->flink;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->flink;
}
}
void bDisplay( )
{
if(head->flink= =NULL)
{
printf("\n\t Doubly linked list is empty");
return;
}
temp=head;
while(temp->flink!=NULL)
{
temp=temp->flink;
}
printf("\n\t The elements are:");
temp1=temp;
while(temp1->blink!=NULL)
{
printf("%d\t",temp1->data);
temp1=temp1->blink;
}
}
void main( )
{
int ch;
head->flink=NULL;
head->blink=NULL;
clrscr( );
while(1)
{
printf("\n Menu \n");
printf(" [Link] at front \n");
printf(" [Link] at end \n");
printf(" [Link] at any position \n");
printf(" [Link] at front \n");
printf(" [Link] at end \n");
printf(" [Link] at any position \n");
switch(ch)
{
case 1: insertF( );
break;
case 2: insertE( );
break;
case 3: insertA( );
break;
case 4: deleteF( );
break;
case 5: deleteE( );
break;
case 6: deleteA( );
break;
case 7: fDisplay( );
break;
case 8: bDisplay( );
break;
case 9: exit(0); default:printf("\
nInalid option");
}
}
}
Output:
Program Statement:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a[20],n,i,j;
clrscr();
printf("\n Enter number of elements in
array:"); scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter value for
a[%d]:",i); scanf("%d",&a[i]);
}
printf("\n The array elements are:");
for(i=0;i<n;i++)
{
printf(" %d\t",a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]==a[i])
{
while(j<n)
{
a[j]=a[j+1];
j++;
}
n=n-1;
}
}
}
printf("\n\n\n After removing duplicate array elements array elements are:");
for(i=0;i<n;i++)
{
printf(" %d\t",a[i]);
}
}
Input:
Output:
Program Statement:
Program:
#include<stdio.
h>
#include<conio.
h> void main()
{
int
a[20],n,i,j,k;
clrscr();
printf("\n Enter number of elements in
array:"); scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter value for a[%d]:",i);
scanf("%d",&a[i]);
}
printf("\n The array elements are:");
for(i=0;i<n;i++)
{
printf(" %d\t",a[i]);
}
for(i=0; i < n; i++)
{
for(j=i+1; j < n; )
{
if(a[j] == a[i])
{
for(k=j; k < n;k++)
{
a[k] = a[k+1];
}
n--;
}
else
{
j+
} +;
}
}
printf("\n\n\n After removing duplicate array elements array
elements are:"); for(i=0;i<n;i++)
{
printf(" %d\t",a[i]);
}
Input:
Output:
Program Statement:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a[20],n,i;
clrscr();
printf("\n enter how many elements you want to sort(max.20):");
for(i=0;i<n;i++)
{
printf("\n enter the value for a[%d]:",i);
scanf("%d",&a[i]); /* read the values */
}
for(i=0;i<n;i++)
{
printf("%d\t",a[i]); /* print unsorted list */
}
for(i=0;i<n;i++)
{
printf("%d\t",a[i]); /* print sorted list */
}
getch();
}
Input:
Output:
Program Statement:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a[20],n,i;
clrscr();
printf("\n enter how many elements you want to sort(max.20):");
for(i=0;i<n;i++)
{
printf("\n enter the value for a[%d]:",i);
scanf("%d",&a[i]); /* read the values */
}
for(i=0;i<n;i++)
{
printf("%d\t",a[i]); /* print unsorted list */
}
quicksort(a,0,n-1);
for(i=0;i<n;i++)
{
printf("%d\t",a[i]); /* print sorted list */
}
getch();
}
Run:
Input:
Output:
Program statement:
13. Write a program for tic tac toe game
#include<bits/stdc++.h>
using namespace std;
#define COMPUTER 1
#define HUMAN 2
return;
}
printf("\t\t\t 1 | 2 | 3 \n");
printf("\t\t\t--------------\n");
printf("\t\t\t 4 | 5 | 6 \n");
printf("\t\t\t--------------\n");
printf("\t\t\t 7 | 8 | 9 \n\n");
printf("-\t-\t-\t-\t-\t-\t-\t-\t-\t-\n\n");
return;
}
return;
}
return(false);
}
int moves[SIDE*SIDE];
initialise(board, moves);
int moveIndex = 0, x, y;
declareWinner(whoseTurn);
}
return;
}
// Driver program
int main()
{
// Let us play the game with COMPUTER starting first
playTicTacToe(COMPUTER);
return (0);
}
Run on IDE
Output:
Tic-Tac-Toe
1|2 |3
--------------
4|5 |6
--------------
7|8 |9
- - - - - - - - - -
| |
--------------
| |O
--------------
| |
| |
--------------
| |O
--------------
X| |
| |
--------------
|O |O
--------------
X| |
X| |
--------------
|O |O
--------------
X| |
X| |
--------------
|O |O
--------------
X| |O
X| |
--------------
|O |O
--------------
X|X |O
X| |
--------------
O|O |O
--------------
X|X |O
Program Statement:
14. Write a program to search a word in a given file and display all the positions.
#include <stdio.h>
#include <stdlib.h>
if (in_file == NULL)
{
printf("Error file missing\n");
exit(-1);
}
Printf(“Enter the word to be searched\n”);
scanf("%s",word);
printf("%s\n", word);
while(!feof(in_file))
{
fscanf(in_file,"%s",string);
if(!strcmp(string,word))
num++;
}
printf(" the word %s found in the file %d times\n",word,num );
return 0;
}
Output:
[Link] file
Program Statement:
15. Write a program to perform operations creation, insertion, deletion and traversing on a
binary search tree.
Program:
#include<stdio.
h>
#include<conio.
h>
#include<stdlib.
h>
#define NULL 0
/* class is created for the implementation of
BST */ struct node
{
int info;
struct node
*lchild; struct
node *rchild;
};
typedef struct node *NODE;
*loc)
{
NODE ptr,ptrsave;
Annamacharya Institute of Technology and Page
Sciences ,Kadapa 81
DATA STRUCTURES DEPT. OF CSE
{
*loc=root;
*par=NUL
L; return;
}
if(item<root->info) /* initialize ptr and ptrsave */
{
ptr=root->lchild;
}
else
{
ptr=root->rchild;
}
ptrsave=root;
while(ptr!=NULL)
{
if(item==ptr->info)
{
*loc=ptr;
*par=ptrsav
e; return;
}
ptrsave=ptr;
if(item<ptr-
>info)
{
ptr=ptr->lchild;
}
else
{
ptr=ptr->rchild;
}
} /* end of while */
*par=ptrsave;
} /* end of find() */
else
{
par->rchild=NULL;
}
}
} /* end of cse_a() */
/* initialize child */
else
{
if(loc==par->lchild) /* item is lchild of its parent */
{
par->lchild=child;
}
else /* item is rchild of its parent */
{
par->rchild=child;
}
}
} /* end of case_b() */
find(item,&parent,&location);
if(location!=NULL)
{
printf("\n\t Item already
present"); return;
}
/* crearting new node to insert */
tmp=(NODE)new(struct
node); tmp->info=item;
tmp-
>lchild=NULL;
tmp-
>rchild=NULL;
if(parent==NUL
L)
{
root=tmp;
}
else
{
if(item<parent->info)
{
parent->lchild=tmp;
}
else
{
parent->rchild=tmp;
}
}
} /* end of insert() */
}
} /* end of postorder() */
if(ptr!=NULL)
{
display(ptr-
>rchild,level+1);
printf("\n");
for(i=0;i<level;i++)
{
printf(" ");
}
printf("%d\t",ptr->info);
display(ptr-
>lchild,level+1);
}
} /* end of display() */
void main()
{
int ch,n;
root=NUL
L; clrscr();
while(1)
{
printf("\n Menu");
printf("\n
[Link]");
printf("\n
[Link]");
printf("\n [Link] traversal");
printf("\n [Link] traversal");
printf("\n [Link]
traversal"); printf("\n
[Link]");
printf("\n [Link]");
switch(ch)
{
case 1:
printf("\n Enter the number to be
Annamacharya Institute of Technology and Page
Sciences ,Kadapa 90
DATA STRUCTURES DEPT. OF CSE
inserted:"); scanf("%d",&n);
insert(n);
break;
case 2:
case 3:
preorder(root);
break;
case 4:
inorder(root);
break;
case 5:
postorder(root);
break;
case 6:
display(root,1);
break;
case 7:
exit(0)
; default:
printf("\n wrong choice");
}
}
Output:
Program Statement:
Program:
#include<stdio.h>
#include<conio.h>
char delet();
void
insert(char);
void bfs();
char queue[20],g[20],x;
int a[20][20],m,n,i,j,state[20],front=0,rear=-1;
void main()
{
clrscr();
printf("\n\t Enter number of nodes of
graph:"); scanf("%d",&n);
printf("\n\t Enter the nodes of graph (In alphabets):");
for(i=1;i<=n;i++)
{
scanf("%c",&g[i]);
}
printf("\n Instructions \n press 1 if edge exists otherwise press 0 \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%c to %c",g[i],g[j]);
scanf("%d",&a[i][j]);
}
}
printf("\n the order of visiting the nodes:");
for(i=1;i<=n;i++)
{
state[i]=1;
}
state[1]=2;
insert(g[1]);
bfs();
getch();
}
void bfs()
{
x=delet();
Annamacharya Institute of Technology and Page
Sciences ,Kadapa 94
DATA STRUCTURES DEPT. OF CSE
for(i=1;i<=n;i++)
{
if(g[i]==x)
{
break;
}
}
state[i]=3;
printf("%c",g[i
]);
for(j=1;j<=n;j++)
{
if(a[i][j]== 1 && state[j] ==1)
{
state[j]=2
;
insert(g[j]
);
}
}
if(front<=rear)
{
bfs();
}
}
void insert(char x)
{
rear++;
queue[rear]
=x;
}
char delet()
{
char x;
x=queue[front]
; front++;
return(x);
}
Output:
Program statement
#include<stdio.h>
void DFS(int);
int G[10][10],visited[10],n;
void main()
{
int i,j;
printf("Enter number of vertices:");
scanf("%d",&n);
printf("\nEnter adjecency matrix of the graph:");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
for(i=0;i<n;i++)
visited[i]=0;
printf(dfs order is :”);
DFS(0);
}
void DFS(int i)
{
int j;
printf("\n%d",i);
visited[i]=1;
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
}
Output:
Dfs order is : 0 13 2
Program statement:
Program:
#include<stdio.h>
#include<conio.h>
/* function prototypes */
main()
{
int a[20],n,i;
clrscr();
printf("\n enter how many elements you want to sort(max.20):");
for(i=0;i<n;i++)
{
printf("\n enter the value for a[%d]:",i);
scanf("%d",&a[i]); /* read the values */
}
for(i=0;i<n;i++)
{
printf("%d\t",a[i]); /* print unsorted list */
}
for(i=0;i<n;i++)
{
printf("%d\t",a[i]); /* print sorted list */
}
getch();
}
}
}
Input:
Output:
program Statement:
#include <stdio.h>
#include <stdlib.h>
#define MAX 4
#define MIN 2
struct btreeNode {
int val[MAX + 1], count;
struct btreeNode *link[MAX + 1];
};
median = MIN + 1;
else
median = MIN;
int pos;
if (!node) {
*pval = val;
*child = NULL;
return 1;
}
return 1;
}
}
return 0;
}
/* removes the value from the given node and rearrange values */
void removeVal(struct btreeNode *myNode, int pos) {
int i = pos + 1;
while (i <= myNode->count) {
myNode->val[i - 1] = myNode->val[i];
myNode->link[i - 1] = myNode->link[i];
i++;
}
myNode->count--;
}
while (j > 0) {
x->val[j + 1] = x->val[j];
x->link[j + 1] = x->link[j];
}
x->val[1] = myNode->val[pos];
x->link[1] = x->link[0];
x->count++;
x = myNode->link[pos - 1];
myNode->val[pos] = x->val[x->count];
myNode->link[pos] = x->link[x->count];
x->count--;
return;
}
x->count++;
x->val[x->count] = myNode->val[pos];
x->link[x->count] = myNode->link[pos]->link[0];
x = myNode->link[pos];
myNode->val[pos] = x->val[1];
x->link[0] = x->link[1];
x->count--;
/* merge nodes */
void mergeNodes(struct btreeNode *myNode, int pos) {
int j = 1;
struct btreeNode *x1 = myNode->link[pos], *x2 = myNode->link[pos - 1];
x2->count++;
x2->val[x2->count] = myNode->val[pos];
x2->link[x2->count] = myNode->link[0];
j = pos;
while (j < myNode->count) {
myNode->val[j] = myNode->val[j + 1];
myNode->link[j] = myNode->link[j + 1];
j++;
}
myNode->count--;
free(x1);
}
if (flag) {
if (myNode->link[pos - 1]) {
copySuccessor(myNode, pos);
flag = delValFromNode(myNode->val[pos], myNode->link[pos]);
if (flag == 0) {
printf("Given data is not present in B-Tree\n");
}
} else {
removeVal(myNode, pos);
}
} else {
flag = delValFromNode(val, myNode->link[pos]);
}
if (myNode->link[pos]) {
if (myNode->link[pos]->count < MIN)
adjustNode(myNode, pos);
}
}
return flag;
}
/* B-Tree Traversal */
void traversal(struct btreeNode *myNode) {
int i;
if (myNode) {
for (i = 0; i < myNode->count; i++) {
traversal(myNode->link[i]);
printf("%d ", myNode->val[i + 1]);
}
traversal(myNode->link[i]);
}
}
int main() {
int val, ch;
while (1) {
printf("1. Insertion\t2. Deletion\n");
printf("3. Searching\t4. Traversal\n");
printf("5. Exit\nEnter your choice:");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter your input:");
scanf("%d", &val);
insertion(val);
break;
case 2:
printf("Enter the element to delete:");
scanf("%d", &val);
deletion(val, root);
break;
case 3:
printf("Enter the element to search:");
scanf("%d", &val);
searching(val, &ch, root);
break;
case 4:
traversal(root);
break;
case 5:
exit(0);
default:
printf("U have entered wrong option!!\n");
break;
}
printf("\n");
}
}
Output:
1. Insertion 2. Deletion
3. Searching 4. Traversal
5. Exit
Enter your choice:1
Enter your input:70
1. Insertion 2. Deletion
3. Searching 4. Traversal
5. Exit
Enter your choice:1
Enter your input:17
1. Insertion 2. Deletion
3. Searching 4. Traversal
5. Exit
Enter your choice:1
Enter your input:67
1. Insertion 2. Deletion
3. Searching 4. Traversal
5. Exit
Enter your choice:1
Enter your input:89
1. Insertion 2. Deletion
3. Searching 4. Traversal
5. Exit
Enter your choice:4
17 67 70 89
1. Insertion 2. Deletion
3. Searching 4. Traversal
5. Exit
Enter your choice:3
Enter the element to search:70
Given data 70 is present in B-Tree
1. Insertion 2. Deletion
3. Searching 4. Traversal
5. Exit
Enter your choice:2
1. Insertion 2. Deletion
3. Searching 4. Traversal
5. Exit
Enter your choice:4
67 70 89
1. Insertion 2. Deletion
3. Searching 4. Traversal
5. Exit
Enter your choice:5