1. INSERTION AND DELETION
PROGRAM:
#include<stdio.h>
void main()
{
int n,c,a,value,position,array[50];
clrscr();
printf("Enter the number of elements in arrayn");
scanf("%d",&n);
printf("Enter the %d elements n",n);
for(c=0;c<n;c++)
scanf("%d",&array[c]);
printf("1.Insertiont2.Deletionn");
scanf("%d",&a);
if(a==1)
{
printf("Enter the location where you wish to insert elementn");
scanf("%d",&position);
printf("Enter the value to insertn");
scanf("%d",&value);
for(c=n-1;c>=position-1;c--)
array[c+1]=array[c];
array[position-1]=value;
printf("Array is");
for(c=0;c<=n;c++)
printf("%dt",array[c]);
getch();
}
else
{
printf("Enter the location where you wish to delete elementn");
scanf("%d",&position);
if(position>=n+1)
printf("Deletion not posiblen");
for(c=position-1;c<n;c++)
array[c]=array[c+1];
printf("Array after deletion isn");
for(c=0;c<n-1;c++)
printf("%dt",array[c]);
getch();
}
}
OUTPUT:
Enter the number of elements in array
5
Enter the 5 elements
2
4
6
8
10
1.Insertion 2.Deletion
1
Enter the location where you wish to insert element
4
Enter the value to insert
7
Array is 2 4 6 7 8 10
Enter the number of elements in array
5
Enter the 5 elements
2
4
6
8
10
1.Insertion 2.Deletion
2
Enter the location where you wish to delete element
4
Array after deletion is
2 4 6 10
2. MERGING TWO SORTED ARRAYS
PROGRAM:
#include<stdio.h>
voidmain()
{
inta[50],b[50],c[100],m,n,i,j,k=0;
clrscr();
printf("nEntersize of array A:n");
scanf("%d",&m);
printf("nEntersortedelementsof arrayA:n");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
printf("nEntersize of arrayB:n");
scanf("%d",&n);
printf("nEntersortedelementsof arrayB:n");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
i=0;
j=0;
while(i<m&&j<n)
{
if(a[i]<b[j])
{
c[k]=a[i];
i++;
}
else
{
c[k]=b[j];
j++;
}
k++;
}
if(i>=m)
{
while(j<n)
{
c[k]=b[j];
j++;
k++;
}
}
if(j>=n)
{
while(i<m)
{
c[k]=a[i];
i++;
k++;
}
}
printf("nAftermerging:n");
for(i=0;i<m+n;i++)
{
printf("n%d",c[i]);
}
getch();
}
OUTPUT:
Enter sorted elements of array A:
2
4
6
8
Enter size of array B:
4
Enter sorted elements of array B:
1
3
5
7
After merging:
1
2
3
4
5
6
7
8
3. SEARCHING AN ELEMENT IN 2-D ARRAY
PROGRAM:
#include<stdio.h>
#include<conio.h>
voidmain()
{
inta[20][20],i,j,m,n,pos,item;
char f;
int*p=&a[0][0];
clrscr();
printf("Enterthe numberof row n");
scanf("%d",&m);
printf("Enterthe numberof column n");
scanf("%d",&n);
printf("Enterthe dataone byone in row wise n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("Enterthe searchitem n");
scanf("%d",&item);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]==item)
{
p=p+(i*n+j)*16;
f='y';
printf("The searchitem%disinposition[%d][%d]",item,i,j);
printf("nTheaddressof the searchitem%dis%u",item,p);
break;
}
}
}
if(f!='y')
printf("nThesearchitem%disnotpresentinthe array",item);
getch();
}
OUTPUT:
Enter the numberof row
4
Enter the numberof column
3
Enter the data one by one inrow wise
2
4
6
8
10
12
14
16
18
20
22
24
Enter the searchitem
12
The search item12 isin position[1][2]
The addressof the searchitem12 is 64876
4. STACK OPERATION USING ARRAY
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define n 10
struct stack
{
int top;
int data[n];
};
void main()
{
struct stack s;
int i,opt,item;
clrscr();
s.top=-1;
do
{
printf("n1.Pushn");
printf("2.Popn");
printf("3.Listn");
printf("4.Exitn");
printf("Enter any one optionn");
scanf("%d",&opt);
switch(opt)
{
case 1:if(s.top==n-1)
printf("Stack Fulln");
else
{
printf("Enter the data to push into the stackn");
scanf("%d",&item);
s.top=s.top+1;
s.data[s.top]=item;
}
break;
case 2:if(s.top==-1)
printf("Stack emptyn");
else
{
printf("The deleted element from stack is %d",s.data[s.top]);
s.top--;
}
break;
case 3:if(s.top==-1)
printf("n stack empty");
else
{
printf("The elements in the stack aren");
for(i=s.top;i>=0;i--)
printf("n%dn",s.data[i]);
}
break;
}
}
while(opt!=4);
getch();
}
OUTPUT:
1.Push
2.Pop
3.List
4.Exit
Enter any one option
1
Enter the data to push into the stack
10
1.Push
2.Pop
3.List
4.Exit
Enter any one option
1
Enter the data to push into the stack
20
1.Push
2.Pop
3.List
4.Exit
Enter any one option
1
Enter the data to push into the stack
30
1.Push
2.Pop
3.List
4.Exit
Enter any one option
3
The elements in the stack are
30
20
10
1.Push
2.Pop
3.List
4.Exit
Enter any one option
2
The deleted element from stack is 30
1.Push
2.Pop
3.List
4.Exit
Enter any one option
2
The deleted element from stack is 30
1.Push
2.Pop
3.List
4.Exit
Enter any one option
3
The elements in the stack are
20
10
1.Push
2.Pop
3.List
4.Exit
Enter any one option
4
5. CONVERTING INFIX EXPRESSION TO POSTFIX EXPRESSION
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct stack
{
intdata[50];
inttop;
};
struct stack s;
char infix[50],post[50];
voidpostfix();
voidpush(int);
char pop();
voidmain()
{
clrscr();
printf("Enterthe infix expressionn");
scanf("%s",infix);
postfix();
getch();
}
voidpostfix()
{
inti,j=0;
for(i=0;infix[i]!='0';i++)
{
switch(infix[i])
{
case'+':
while(s.data[s.top]>=i)
post[j++]=pop();
push(1);
break;
case'-':
while(s.data[s.top]>=1)
post[j++]=pop();
push(2);
break;
case'*':
while(s.data[s.top]>=3)
post[j++]=pop();
push(3);
break;
case'/':
while(s.data[s.top]>=4)
post[j++]=pop();
push(4);
break;
case'^':
while(s.data[s.top]>=4)
post[j++]=pop();
push(5);
break;
case'(':
push(0);
break;
case')':
while(s.data[s.top]!=0)
post[j++]=pop();
s.top--;
break;
default:
post[j++]=infix[i];
}
}
while(s.top>0)
post[j++]=pop();
printf("ntThe postfix expressionis t%s",post);
}
voidpush(intv)
{
s.top++;
s.data[s.top]=v;
}
char pop()
{
inta;
char c;
a=s.data[s.top];
s.top--;
switch(a)
{
case 1:
c='+';
break;
case 2:
c='-';
break;
case 3:
c='*';
break;
case 4:
c='/';
break;
case 5:
c='^';
break;
}
return(c);
}
OUTPUT:
Enter the infix expression
(a+b*c-d/e)
The postfix expressionis abc*+de/-
6. EVALUATING A POSTFIX EXPRESSION
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<ctype.h>
struct stack
{
inttop;
floata[20];
}s;
voidmain()
{
inti,n;
floatopt1,opt2,opt3;
char ex[20];
clrscr();
s.top=-1;
printf("nEnterthe postfix expression:");
gets(ex);
n=strlen(ex);
for (i=0;i<n;i++)
{
if (isdigit(ex[i]))
{
s.top++;
s.a[s.top]=ex[i]-'0';
}
else
{
opt2=s.a[s.top--];
opt1=s.a[s.top--];
switch(ex[i])
{
case'+':
opt3=opt1+opt2;
break;
case'-':
opt3=opt1-opt2;
break;
case'*':
opt3=opt1*opt2;
break;
case'/':
opt3=opt1/opt2;
break;
case'^':
opt3=pow(opt1,opt2);
break;
}
s.a[++s.top]=opt3;
}
}
printf("nTheResult%sis%.2f",ex,s.a[s.top]);
getch();
}
OUTPUT:
Enter the postfix expression:853*+94/-
The Result853*+94/- is20.75
7. QUEUE OPERATIONS USING ARRAY
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct queue
{
intdata[50];
intfront;
intrear;
};
inti,opt,item;
intx;
voidmain()
{
struct queue q;
clrscr();
q.front=-1;
q.rear=-1;
do
{
printf("Selectanyone option n");
printf("1.Create n");
printf("2.Listn");
printf("3.Insert n");
printf("4.Deleten");
printf("5.Exitn");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("Enter10data one by one n");
for(i=0;i<10;i++)
scanf("%d",&q.data[++q.rear]);
break;
case 2:
printf("The datainthe queue are n");
for(i=q.front+1;i<=q.rear;i++)
printf("%dn",q.data[i]);
break;
case 3:
printf("Enterthe datatoinsert n");
scanf("%d",&item);
q.data[++q.rear]=item;
break;
case 4:
q.front++;
x=q.data[q.front];
printf("nThedeleateditemis t%dn",x);
break;
}
}
while(opt!=5);
getch();
}
OUTPUT:
Selectanyone option
1.Create
2.List
3.Insert
4.Delete
5.Exit
1
Enter 10 data one byone
1
2
3
4
5
6
7
8
9
10
Selectanyone option
1.Create
2.List
3.Insert
4.Delete
5.Exit
3
Enter the data to insert
11
1.Create
2.List
3.Insert
4.Delete
5.Exit
2
The data in the queue are
1
2
3
4
5
6
7
8
9
10
11
Selectanyone option
1.Create
2.List
3.Insert
4.Delete
5.Exit
4
The deleateditemis 1
Selectanyone option
1.Create
2.List
3.Insert
4.Delete
5.Exit
4
The deleateditemis 2
Selectanyone option
1.Create
2.List
3.Insert
4.Delete
5.Exit
5
Data structure output 1

Data structure output 1

  • 1.
    1. INSERTION ANDDELETION PROGRAM: #include<stdio.h> void main() { int n,c,a,value,position,array[50]; clrscr(); printf("Enter the number of elements in arrayn"); scanf("%d",&n); printf("Enter the %d elements n",n); for(c=0;c<n;c++) scanf("%d",&array[c]); printf("1.Insertiont2.Deletionn"); scanf("%d",&a); if(a==1) { printf("Enter the location where you wish to insert elementn"); scanf("%d",&position); printf("Enter the value to insertn"); scanf("%d",&value); for(c=n-1;c>=position-1;c--) array[c+1]=array[c]; array[position-1]=value; printf("Array is"); for(c=0;c<=n;c++) printf("%dt",array[c]); getch(); } else { printf("Enter the location where you wish to delete elementn"); scanf("%d",&position); if(position>=n+1) printf("Deletion not posiblen"); for(c=position-1;c<n;c++) array[c]=array[c+1]; printf("Array after deletion isn"); for(c=0;c<n-1;c++) printf("%dt",array[c]); getch(); } }
  • 2.
    OUTPUT: Enter the numberof elements in array 5 Enter the 5 elements 2 4 6 8 10 1.Insertion 2.Deletion 1 Enter the location where you wish to insert element 4 Enter the value to insert 7 Array is 2 4 6 7 8 10 Enter the number of elements in array 5 Enter the 5 elements 2 4 6 8 10 1.Insertion 2.Deletion 2 Enter the location where you wish to delete element 4 Array after deletion is 2 4 6 10
  • 3.
    2. MERGING TWOSORTED ARRAYS PROGRAM: #include<stdio.h> voidmain() { inta[50],b[50],c[100],m,n,i,j,k=0; clrscr(); printf("nEntersize of array A:n"); scanf("%d",&m); printf("nEntersortedelementsof arrayA:n"); for(i=0;i<m;i++) { scanf("%d",&a[i]); } printf("nEntersize of arrayB:n"); scanf("%d",&n); printf("nEntersortedelementsof arrayB:n"); for(i=0;i<n;i++) { scanf("%d",&b[i]); } i=0; j=0; while(i<m&&j<n) { if(a[i]<b[j]) { c[k]=a[i]; i++; } else { c[k]=b[j]; j++; } k++; } if(i>=m) { while(j<n) { c[k]=b[j];
  • 4.
    j++; k++; } } if(j>=n) { while(i<m) { c[k]=a[i]; i++; k++; } } printf("nAftermerging:n"); for(i=0;i<m+n;i++) { printf("n%d",c[i]); } getch(); } OUTPUT: Enter sorted elementsof array A: 2 4 6 8 Enter size of array B: 4 Enter sorted elements of array B: 1 3 5 7 After merging: 1 2 3 4 5 6 7 8
  • 5.
    3. SEARCHING ANELEMENT IN 2-D ARRAY PROGRAM: #include<stdio.h> #include<conio.h> voidmain() { inta[20][20],i,j,m,n,pos,item; char f; int*p=&a[0][0]; clrscr(); printf("Enterthe numberof row n"); scanf("%d",&m); printf("Enterthe numberof column n"); scanf("%d",&n); printf("Enterthe dataone byone in row wise n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf("%d",&a[i][j]); } printf("Enterthe searchitem n"); scanf("%d",&item); for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(a[i][j]==item) { p=p+(i*n+j)*16; f='y'; printf("The searchitem%disinposition[%d][%d]",item,i,j); printf("nTheaddressof the searchitem%dis%u",item,p); break; } } } if(f!='y') printf("nThesearchitem%disnotpresentinthe array",item); getch(); }
  • 6.
    OUTPUT: Enter the numberofrow 4 Enter the numberof column 3 Enter the data one by one inrow wise 2 4 6 8 10 12 14 16 18 20 22 24 Enter the searchitem 12 The search item12 isin position[1][2] The addressof the searchitem12 is 64876
  • 7.
    4. STACK OPERATIONUSING ARRAY PROGRAM: #include<stdio.h> #include<conio.h> #define n 10 struct stack { int top; int data[n]; }; void main() { struct stack s; int i,opt,item; clrscr(); s.top=-1; do { printf("n1.Pushn"); printf("2.Popn"); printf("3.Listn"); printf("4.Exitn"); printf("Enter any one optionn"); scanf("%d",&opt); switch(opt) { case 1:if(s.top==n-1) printf("Stack Fulln"); else { printf("Enter the data to push into the stackn"); scanf("%d",&item); s.top=s.top+1; s.data[s.top]=item; } break; case 2:if(s.top==-1) printf("Stack emptyn"); else { printf("The deleted element from stack is %d",s.data[s.top]);
  • 8.
    s.top--; } break; case 3:if(s.top==-1) printf("n stackempty"); else { printf("The elements in the stack aren"); for(i=s.top;i>=0;i--) printf("n%dn",s.data[i]); } break; } } while(opt!=4); getch(); }
  • 9.
    OUTPUT: 1.Push 2.Pop 3.List 4.Exit Enter any oneoption 1 Enter the data to push into the stack 10 1.Push 2.Pop 3.List 4.Exit Enter any one option 1 Enter the data to push into the stack 20 1.Push 2.Pop 3.List 4.Exit Enter any one option 1 Enter the data to push into the stack 30 1.Push 2.Pop 3.List 4.Exit Enter any one option 3 The elements in the stack are 30 20 10
  • 10.
    1.Push 2.Pop 3.List 4.Exit Enter any oneoption 2 The deleted element from stack is 30 1.Push 2.Pop 3.List 4.Exit Enter any one option 2 The deleted element from stack is 30 1.Push 2.Pop 3.List 4.Exit Enter any one option 3 The elements in the stack are 20 10 1.Push 2.Pop 3.List 4.Exit Enter any one option 4
  • 11.
    5. CONVERTING INFIXEXPRESSION TO POSTFIX EXPRESSION PROGRAM: #include<stdio.h> #include<conio.h> struct stack { intdata[50]; inttop; }; struct stack s; char infix[50],post[50]; voidpostfix(); voidpush(int); char pop(); voidmain() { clrscr(); printf("Enterthe infix expressionn"); scanf("%s",infix); postfix(); getch(); } voidpostfix() { inti,j=0; for(i=0;infix[i]!='0';i++) { switch(infix[i]) { case'+': while(s.data[s.top]>=i) post[j++]=pop(); push(1); break; case'-': while(s.data[s.top]>=1) post[j++]=pop(); push(2); break; case'*': while(s.data[s.top]>=3)
  • 12.
  • 13.
    c='+'; break; case 2: c='-'; break; case 3: c='*'; break; case4: c='/'; break; case 5: c='^'; break; } return(c); } OUTPUT: Enter the infix expression (a+b*c-d/e) The postfix expressionis abc*+de/-
  • 14.
    6. EVALUATING APOSTFIX EXPRESSION PROGRAM: #include<stdio.h> #include<conio.h> #include<math.h> #include<ctype.h> struct stack { inttop; floata[20]; }s; voidmain() { inti,n; floatopt1,opt2,opt3; char ex[20]; clrscr(); s.top=-1; printf("nEnterthe postfix expression:"); gets(ex); n=strlen(ex); for (i=0;i<n;i++) { if (isdigit(ex[i])) { s.top++; s.a[s.top]=ex[i]-'0'; } else { opt2=s.a[s.top--]; opt1=s.a[s.top--]; switch(ex[i]) { case'+': opt3=opt1+opt2; break; case'-': opt3=opt1-opt2; break; case'*':
  • 15.
  • 16.
    7. QUEUE OPERATIONSUSING ARRAY PROGRAM: #include<stdio.h> #include<conio.h> struct queue { intdata[50]; intfront; intrear; }; inti,opt,item; intx; voidmain() { struct queue q; clrscr(); q.front=-1; q.rear=-1; do { printf("Selectanyone option n"); printf("1.Create n"); printf("2.Listn"); printf("3.Insert n"); printf("4.Deleten"); printf("5.Exitn"); scanf("%d",&opt); switch(opt) { case 1: printf("Enter10data one by one n"); for(i=0;i<10;i++) scanf("%d",&q.data[++q.rear]); break; case 2: printf("The datainthe queue are n"); for(i=q.front+1;i<=q.rear;i++) printf("%dn",q.data[i]); break; case 3: printf("Enterthe datatoinsert n");
  • 17.
  • 18.
    OUTPUT: Selectanyone option 1.Create 2.List 3.Insert 4.Delete 5.Exit 1 Enter 10data one byone 1 2 3 4 5 6 7 8 9 10 Selectanyone option 1.Create 2.List 3.Insert 4.Delete 5.Exit 3 Enter the data to insert 11 1.Create 2.List 3.Insert 4.Delete 5.Exit 2 The data in the queue are 1 2 3 4 5
  • 19.
    6 7 8 9 10 11 Selectanyone option 1.Create 2.List 3.Insert 4.Delete 5.Exit 4 The deleateditemis1 Selectanyone option 1.Create 2.List 3.Insert 4.Delete 5.Exit 4 The deleateditemis 2 Selectanyone option 1.Create 2.List 3.Insert 4.Delete 5.Exit 5