Pr1 Write a program for array Create, insertion, deletion,
display.
void main()
{
int a[10],e,p,ch,i,n=5;
clrscr();
printf("\n enter the array element:");
for(i=0;i<=4;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<=4;i++)
{
printf("\n %d",a[i]);
}
while(ch!=3)
{
printf("\n menu \n 1. insertion \n 2. deletion \n 3. exit");
printf("\n enter choise:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n enter the position:");
scanf("%d",&p);
printf("\n enter the element:");
scanf("%d",&e);
for(i=n-1;i>=p;i--)
{
a[i+1]=a[i];
}
a[p]=e;
for(i=0;i<n+1;i++)
{
printf("\n %d",a[i]);
}
break;
case 2:
printf("\n enter the position to be deleted:");
scanf("%d",&p);
for(i=p;i<n+1;i++)
{
a[i]=a[i+1];
}
for(i=0;i<4;i++)
{
printf("\n %d",a[i]);
}
break;
case 3:
break;
}
getch();
}
}
Pr2 & Pr 4 Write a program to search data from given
array of number using Linear Search and Binary Search.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,m,l=0,h=4,ch,e,flag=0;
clrscr();
printf("enter the element for array");
for(i=0;i<=4;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<=4;i++)
{
printf("\n %d",a[i]);
}
printf("1.linear search \n 2.binary search \n 3.exit");
printf("\n enter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter the key element");
scanf("%d",&e);
for(i=0;i<=4;i++)
{
if(a[i]==e)
{
printf("number is found%d",i);
flag=1;
}
}
if(flag==0)
{
printf("number is not found");
}
break;
case 2 :
flag=0;
printf("enter the key element");
scanf("%d",&e);
while(l<=h)
{
m=(l+h)/2;
if(a[m]==e)
{
printf("number is found%d",m);
flag=1;
break;
}
if(a[m]<e)
{
l=m+1;
}
else
{
h=m-1;
}
}
if(flag==0)
{
printf("number is not found");
}
break;
}
getch();
}
Pr3 Write a program to search data from given array of
String using Linear Search.
#include <stdio.h>
#include <string.h>
void main()
{
char a[100][100],ele[100]; // Array of strings
int n, i;
int found = 0;
clrscr();
// Input each string
printf("Enter %d strings:\n", n);
for (i = 0; i <99; i++) {
scanf("%s", a[i]);
}
// Input the string to search for
printf("\nEnter the string to search for: ");
scanf("%s", ele);
// Perform linear search
for (i = 0; i < n; i++)
{
if (strcmp(a[i],ele)==0)
{
printf("String '%s' found at position %d.\n", ele, i + 1);
found=1;
break; // Exit the loop if found
}
}
if (!found)
{
printf("String '%s' not found in the array.\n", ele);
}
getch();
}
Pr5 Write a program to search data from given array of
String using Binary Search.
#include<stdio.h>
#include<conio.h>
void main()
{
char a[5][5],ele[5];
int i,m,l=0,h=4,flag=0;
clrscr();
printf("\nEnter the elements");
for(i=0;i<5;i++)
{
scanf("\n%s",&a[i]);
}
printf("\nElements are");
for(i=0;i<5;i++)
{
printf("\n%s",a[i]);
}
printf("\nEnter the element for searching:");
scanf("%s",&ele);
while(l<=h)
{
m=(l+h)/2;
if(strcmp(a[m],ele)==0)
{
printf("\nElement found");
flag=1;
break;
}
if(a[m]<ele)
{
l=m+1;
}
else
{
h=m-1;
}
}
if(flag==0)
{
printf("\n Element not found");
}
getch();
}
Pr6 Write a program to Sort data from given array of
number using Bubble Sort.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,temp,n=5;
clrscr();
printf("enter the element for array");
for(i=0;i<=4;i++)
scanf("%d",&a[i]);
//for(i=0;i<=4;i++)
//printf("\n %d",a[i]);
for(i=0;i<=4;i++)
{
for(j=0;j<4-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
printf("\n sorted array is %d",a[i]);
getch();
}
Pr7 Write a program to Sort data from given array of
String using Bubble Sort.
#include <stdio.h>
#include <string.h>
void main()
{
char name[5][5], temp[5];
int n, i, j;
clrscr();
printf("Input string :\n");
for (i =0; i <=4; i++)
{
scanf("%s",name[i]);
}
for (i = 1; i <=4 ; i++)
{
for (j = 0; j <= 4 - i; j++)
{
if (strcmp(name[j], name[j + 1]) > 0)
{
strcpy(temp, name[j]);
strcpy(name[j], name[j + 1]);
strcpy(name[j + 1], temp);
}
}
}
printf("The strings appear after sorting:\n");
for (i = 0; i <=4; i++)
{
printf("%s\n", name[i]);
}
getch();
}
Pr8 Write a program to Sort data from given array of
number using Selection Sort.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,temp,n=5;
clrscr();
printf("enter the element for array");
for(i=0;i<=4;i++)
scanf("%d",&a[i]);
for(i=0;i<=4;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
printf("\n sorted array is %d",a[i]);
getch();
}
Pr9 Write a program to Sort data from given array of
String using Selection Sort.
#include <stdio.h>
#include <string.h>
void main()
{
char name[5][5], temp[5];
int n, i, j;
clrscr();
printf("Input string :\n");
for (i =0; i <=4; i++)
{
scanf("%s", name[i]);
}
for (i = 0; i <= 4; i++)
{
for (j = i+1; j <= 4; j++)
{
if (strcmp(name[i], name[j ]) > 0)
{
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
printf("The strings appear after sorting:\n");
for (i = 0; i <=4; i++)
{
printf("%s\n", name[i]);
}
getch();
}
Pr10 Write a program to Sort data from given array of
number using Insertion Sort.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,k,n=5,temp;
clrscr();
printf("\n enter the element");
for(i=0;i<=4;i++)
{
scanf("\n%d",&a[i]);
}
for(i=0;i<=4;i++)
{
printf("\n%d",a[i]);
}
for(j=1;j<n;j++)
{
for(k=0;k<n;k++)
{
if(a[j]<a[k])
{
temp=a[j];
a[j]=a[k];
a[k]=temp;
}
}
}
for(i=0;i<5;i++)
{
printf("\nsorted array is%d",a[i]);
}
getch();
}
Pr11 Write a program to Sort data from given array of
string using Insertion Sort.
#include <stdio.h>
#include <string.h>
void main()
{
char str[] ="DATA_STRUCTURE";
char temp;
int k, j, n = strlen(str);
clrscr();
printf("Original string: %s\n\n", str);
//insertion sort
for(j = 1; j < n; j++)
{
for(k = 0; k < n; k++)
{
if(str[j] < str[k])
{
temp = str[j];
str[j] = str[k];
str[k] = temp;
}
}
}
printf("\nUpdated string: %s", str);
getch();
}
OR
#include <stdio.h>
#include <string.h>
void main()
{
char str[20][20] = {"orange","mango","apple", "aaaaa"};
char temp[20];
int i, j, k;
clrscr();
printf("Inserted strings: ");
for(i = 0; i < 19; i++)
{
printf("%s ", str[i]);
}
printf("\n");
for(j = 1; j < 4; j++)
{
for(k = 0; k < 4; k++)
{
if(strcmp(str[k], str[j]) > 0)
{
strcpy(temp, str[j]);
strcpy(str[j], str[k]);
strcpy(str[k], temp);
}
}
}
printf("Updated strings:\n");
for(i = 0; i < 4; i++)
{
printf("%s\n", str[i]);
}
getch();
}
Pr16 Write a program to perform PUSH & POP
operations on Stack using Array
#include<stdio.h>
#include<conio.h>
struct stack
{
int top;
char opstack[20];
}s;
void creat()
{
s.top=-1;
}
int isfull()
{
if(s.top==19)
{
return (1);
}
else
return(0);
}
int isempty()
{
if(s.top==-1)
{
return (1);
}
else
return(0);
}
void push(int x)
{
if(isfull())
{
printf("stack is full");
exit(1);
}
else
{
s.top++;
s.opstack[s.top]=x;
}
}
void pop()
{
int c;
if(isempty())
{
printf("stack is empty");
exit(1);
}
else
{
c=s.opstack[s.top];
s.opstack[s.top]='\0';
s.top--;
printf("\n Deleted no. %d",c);
}
}
int display()
{
int i;
if(isempty())
{
printf("stack is empty");
exit(1);
}
else
{
for(i=1;i<=s.top;i++)
{
printf("\n %d",s.opstack[i]);
}
}
return(0);
}
void main()
{
int i,ch,n;
clrscr();
do
{
printf("\n1. push \n 2.pop \n 3.Display");
printf("\n Enter ur choice::");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter the element::");
scanf("%d",&n);
push(n);
break;
case 2: pop();
break;
case 3:
printf("\n Stack elements are::");
display();
break;
}
}while(ch!=3);
getch();
}
Pr21 Write a program to perform Insert &Deletion
operations on Queue using Array
#include<stdio.h>
#include<conio.h>
#define max[50]
struct queue
{
int r;
int f;
int queue[50];
}q;
void create()
{
q.f=q.r=-1;
}
int isfull()
{
if(q.r==49)
{
return(1);
}
else
{
return(0);
}
}
int isempty()
{
if(q.r==-1)
{
return(1);
}
else
{
return(0);
}
}
void insert(int item)
{
if(isfull())
{
printf("\n\n\t\tqueue is overflow");
exit(1);
}
else
{
q.r++;
q.queue[q.r]=item;
}
}
void del()
{
if(isempty())
{
printf("\n\n\t\tunderflow");
exit(1);
}
else
{
q.f++;
}
}
void display()
{
int i;
if(isempty())
{
printf("\nQueue is empty");
exit(1);
}
else
for(i=q.f+1;i<=q.r;i++)
{
printf("\n%d",q.queue[i]);
}
}
void main()
{
int ch;
int i,n;
clrscr();
printf("\n1.Insert\n2.Delete\n3.Display\n4.exit");
printf("\nenter your choice");
scanf("%d",&ch);
do
{
switch(ch)
{
case 1:
printf("\nEnter the element");
scanf("%d",&n);
insert(n);
break;
case 2:
del();
break;
case 3:
printf("\nstack contains:");
display();
break;
}
}while(ch!=4);
getch();
}