0% found this document useful (0 votes)
134 views67 pages

Algorithm of Insertion of An Element at Desire Position Into An Array

The document contains algorithms and C code for operations on linear queues like insertion, deletion and display. It describes the algorithms for inserting an element into a queue by incrementing the rear index and adding the element. It also describes the algorithm for deleting an element by removing the element at the front index and incrementing front. The C code implements these queue operations by maintaining front and rear indexes to track the queue, and functions for insertion, deletion and displaying the queue elements.

Uploaded by

Amit Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views67 pages

Algorithm of Insertion of An Element at Desire Position Into An Array

The document contains algorithms and C code for operations on linear queues like insertion, deletion and display. It describes the algorithms for inserting an element into a queue by incrementing the rear index and adding the element. It also describes the algorithm for deleting an element by removing the element at the front index and incrementing front. The C code implements these queue operations by maintaining front and rear indexes to track the queue, and functions for insertion, deletion and displaying the queue elements.

Uploaded by

Amit Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 67

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

ALGORITHM OF INSERTION OF AN ELEMENT AT


DESIRE POSITION INTO AN ARRAY
STEP1:INTIALIZATION
TEMP=NUM
STEP 2 : REPEAT WHILE TEMP>= POSITION
(1)
(2)

ARR[TEMP]
TEMP=TEMP-1

STEP 3:ARR[POSITION]=ELEMENT
STEP4:NUM=NUM+1
STEP5:RETURN(NUM)

ANKIT KUMAR SHARMA

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

W.A.P TO INSERT & DISPLAY AN ELEMENT IN AN ARRAY


AND TO INSERT AN ELEMENT AT A DESISED POSTION
#include<stdio.h>
#include<process.h>
#include<conio.h>
int a[10],i,n,p,m;
void insert(int[],int);
void display(int[],int);
int position(int[],int,int);
void main()
{
clrscr();
int c;
while(1)
{
printf("1 insert\n");
printf("2 display\n ");
printf("3 position\n");
printf("4 exit\n");
printf("\n enter the choce\n ");
scanf("%d",&c);
switch(c)
{
case 1:printf("\n enter the num\n ");
scanf("%d",&n);
insert(a,n);
break;
case 2:display(a,n);
break;
case 3: printf("\n enter the position\n ");
scanf("%d",&p);
printf("\n enter the number do you want insert
particur postion\n");
scanf("%d",&m);
position(a,p,m);
break;

ANKIT KUMAR SHARMA

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

case 4:
exit(0);
}
}
getch();
}
void insert(int a[],int n)
{
for(i=1;i<=n;i++)
{
printf("\n enter rhe data\n ");
scanf("%d",&a[i]);
}
}
void display(int a[],int n)
{
for(i=1;i<n;i++)
{
printf("array is data is %d\n ",a[i]);
}
}
int position(int a[],int p,int m)
{
for(i=n;i>p;i--)
{
a[i]=a[i-1];
}
a[p]=m;
n++;
return(n);
}

ANKIT KUMAR SHARMA

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

OUTPUT:

ANKIT KUMAR SHARMA

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

ALGO OF DELETION OF AN ELEMENT FROM DESIRE


POSITION FROM ARRAY
STEP1:[INITIALIZATION]
STEP2:TEMP=POSITION
STEP3:REPEAT WHILE<=NUM-1
(1)ARR[TEMP]=ARR[TEMP+1]
(2)TEMP=TEMP+1
STEP4:NUM=NUM-1
STEP5:RETURN(NUM)

ANKIT KUMAR SHARMA

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

W.A.P TO INSERT &DISPLAY AN ELEMENT IN AN ARRAY AND


TO DELETE AN ELEMENT FROM THE DESIRED POSITION
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define s 20
void insert( char emp[s][s],int);
void display(char emp[s][s],int);
int del(char emp[s][s],int,char a[s]);
char emp[s][s],a[s];
void main()
{
clrscr();
int n;
printf("enter the no of array\n");
scanf("%d",&n);
insert(emp,n);
display(emp,n);
n=del(emp,n,a);
display(emp,n);
getch();
}
void insert(char emp[s][s],int n)
{
int i;
for(i=1;i<=n;i++)
{
printf("enter the name\n");
fflush(stdin);
gets(emp[i]);
}
}
void display(char emp[s][s],int n)
{
int i;
for(i=1;i<=n;i++)
{
ANKIT KUMAR SHARMA

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

fflush(stdin);
puts(emp[i]);
}
}
int del(char emp[s][s],int n,char a[s])
{
int p,temp,i;
printf("enter the pos\n");
scanf("%d",&p);
temp=p;
a=emp[p];
while(temp<=n-1)
{
for(i=0;i<s;i++)
{
emp[temp][i]=emp[temp+1][i];
}
temp++;
}
n=n-1;
return(n);
}

ANKIT KUMAR SHARMA

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

OUTPUT

ANKIT KUMAR SHARMA

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

ALGO TO FIND OUT DUPLICATE NUMBER IN AN


ARRAY
STEP 1: INTIALIZATION
DUPLICATE=N
STEP 2: STATUS=0
STEP 3: REPEAT THROUGH STEP 8 FOR I=0,1,2..N-2
STEP 4: REPEAT THROUGH STEP 8 FOR J=I+1,I+2N-1
STEP 5: IF ARRAY [I]=ARR[J]
N=N-1
STEP 6: REPEAT FOR K=J ,J=1N-1
ARR[K]=ARR[K+1]
STEP 7: SET FLAG
STATUS=1
STEP 8: J=J-1
STEP 9: IF STATUS=0
OUTPUT NO DUPLICATE IS FOUND
STEP 10: DUPLICATE=DUPLICATE-N
STEP 11: RETURN(DUPLICATE)

ANKIT KUMAR SHARMA

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

W.A.P TO FIND DUPLICATE NO AND DELETE THAT ELEMENT


#include<stdio.h>
#include<conio.h>
void insert(int[],int);
void display(int[],int);
int dup(int[],int);
int arr[100],i;
void main()
{
int n;
printf("\n enter the number of array\n ");
scanf("%d",&n);
insert(arr,n);
display(arr,n);
n=dup(arr,n);
display(arr,n);
getch();
}
void insert(int arr[],int n)
{
for(i=1;i<=n;i++)
{
printf("\n enter the number\n");
scanf("%d",&arr[i]);
}
}
void display(int arr[],int n)
{
for(i=1;i<=n;i++)
{
printf("\n array data is %d\n ",arr[i]);
}
}
int dup(int arr[],int n)
{
int i,j,k;
int c=0;
for(i=1;i<n-1;i++)
{
ANKIT KUMAR SHARMA

10

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

for(j=i+1;j<n;j++)
{
if(arr[i]==arr[j])
{
arr[i]=arr[j];
for(k=j;k<n; k++)
{
arr[k]=arr[k+1];
c=c+1;
}
}
}
}
n=n-1;
if(c==0)
printf("\n array is no no of element duplicate\n");
else
printf("\n array is no of element is
duplicate\n",c);
return(n);
}

OUTPUT
ANKIT KUMAR SHARMA

11

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

ALGORITHM OF MULTIPLICATION OF TWO MATRIX


ANKIT KUMAR SHARMA

12

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

STEP 1: IF COL1=ROW2
REPEAT FOR I=0,1,2..ROW1- 1
REPEAT FOR J=0,1,2..COL2 - 1
MAT_RES[I][J]=0
REPEAT FOR K=0,1,2COL1 - 1
MAT_RES[I][J]=MAT_RES[I][J] + MAT1[I][K] * MAT2[K][J]
ELSE IF ROW1=COL2
REPEAT FOR I=0,1,2..COL1 - 1
REPEAT FOR J=0,1,2..ROW2 - 1
MAT_RES[I][J]=0
REPEAT FOR K=0,1,2.COL2 1
MAT_RES[I][J]=MAT_RES[I][J] + MAT1[I][K] * MAT2[K][J]
ELSE
OUTPUTMULTIPLICATION IS NOT POSSIBLE AND EXIT
STEP 2: EXIT

W.A.P TO MATRIX
ANKIT KUMAR SHARMA

MULTIPLICATION
13

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int mat1[3][3],mat2[3][3],mat3[3][3],i,j,k,sum;
clrscr();
printf("enter the first matrix\n ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&mat1[i][j]);
}
printf("enter the secand matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&mat2[i][j]);
}
printf("display 1 matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",mat1[i][j]);
}
printf("\n");
}
printf("display 2 matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",mat2[i][j]);
}

ANKIT KUMAR SHARMA

14

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

printf("\n");
}
printf("calculate the matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=0;
for(k=0;k<3;k++)
sum=sum+mat1[i][k]*mat2[k][j];
mat3[i][j]=sum;
}
}
printf("display 3 matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",mat3[i][j]);
}
printf("\n");
}
getch();
}

OUTPUT
ANKIT KUMAR SHARMA

15

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

ANKIT KUMAR SHARMA

DATA STRUCTURE USING C

16

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

ALGORITHM TO CALCULATE THE FACTORIAL OF


NUMBER
FACTORIAL(NUM)
STEP 1: IF(NUM==0) THEN
RETURN 1
STEP 2: ELSE
RETURN NUM*FACTORIAL(NUM-1)
STEP 3: EXIT

ANKIT KUMAR SHARMA

17

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

W.A.P TO FIND FACTORIAL NUMBER USING RECURSEN


#include<stdio.h>
#include<conio.h>
int fact(int);
void display(int);
void main()
{
clrscr();
int n,k;
printf("enter the number\n");
scanf("%d",&n);
k=fact(n);
display(k);
getch();
}
int fact(int n)
{
if(n==0)
return(1);
else
return(n*fact(n-1));
}
void display(int k)
{
printf("\n factorial num %d",k);
}

ANKIT KUMAR SHARMA

18

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

OUTPUT

ANKIT KUMAR SHARMA

19

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

ALGO OF TOWER OF HANOI


Move(n, peg1,peg2,peg3)
Step 1: read number of disc, peg1, peg 2, peg3
Step 2: if (n>1)
Step 3: move(n-1,peg1,peg3,peg2)
Step 4: printf(move disk from __ to ___)
Step 5: move (n-1,peg2,peg1,peg3)
Step 6: end

ANKIT KUMAR SHARMA

20

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

W.A.P TO TOWER OF HANOI BASED ON RECURSON


#include<stdio.h>
#include<conio.h>
void hanoi_tower(char,char,char,int);
void main()
{
int n;
char x,y,z;
printf("enter the no of dsk");
scanf("%d",&n);
printf("no of disk%d\n",n);
hanoi_tower('x','y','z',n);
getch();
}
void hanoi_tower(char peg1,char peg2,char
peg3,int n)
{
if(n<=0)
printf("llegal data");
else
if(n==1)
printf("\n move disk form %c To %c\n
",peg1,peg3);
else
{
hanoi_tower(peg1,peg3,peg2,n-1);
hanoi_tower(peg1,peg2,peg3,1);
hanoi_tower(peg2,peg1,peg3,n-1);
}
}

ANKIT KUMAR SHARMA

21

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

OUTPUT

ANKIT KUMAR SHARMA

22

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

Algo to insert an element into Queue.


ADDQ(QUEUE,MAXQ,FRONT,REAR,ITEM)
STEP 1:[check overflow condition]
If REAR=MAXQ,then :Write :OVERFLOW and return
STEP 2:[increment REAR]
If FRONT:=0,then:
Set FRONT :=1 and REAR:=1
Else :
Set REAR :=REAR+1
STEP 3: Set QUEUE[REAR]:=ITEM
STEP 4: return

Algo to delete an element from Queue.


DELQ(QUEUE,FRONT,REAR,ITEM)
STEP 1:[QUEUE already empty?
If FRONT:=0,then:Write :UNDERFLOW,and return
STREP2:[remove an element]
Set ITEM:=QUEUE[FRONT]
STEP3:fi FRONT =REAR then :
Set FRONT:=0,REAR:=0
Else :
Set FRONT:=FRONT+1
STEP4:return

ANKIT KUMAR SHARMA

23

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

W.A.P TO insert and delete & display in a linear QUEUE


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>
void insert(int [],int);
void del(int []);
void display(int []);
int front=-1,rear=-1,q[10];
void main()
{
clrscr();
int k=0;
int n;
char choice;
do
{
printf("\n insert>i\t delete>d\t display>s\t
quit>q\t");
printf("\n input the choice\n");
do
{
choice=getchar();
choice=tolower(choice);
}while(strchr("idsq",choice)==NULL);
printf("your choice %c:",choice);
switch(choice)
{
case 'i':printf("enter the num\n");
scanf("%d",&n);
insert(q,n);
break;
case 'd':del(q);
break;
case 's':display(q);
break;
case 'q':k=1;
}
}while(!k);
ANKIT KUMAR SHARMA

24

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

}
void insert(int q[10],int n)
{
if(rear==9)
printf("que is full");
else if(front==-1)
{
front=0;
q[++rear]=n;
}
else
q[++rear]=n;
}
void display(int q[10])
{
int i;
if(front==-1)
{
printf("queue is empty");
}
else
{
for(i=front;i<=rear;i++)
{
printf("%d",q[i]);
}}}
void del(int q[10])
{
int k;
if(front==-1)
{
printf("que is empty");
}
else
{
k=q[front];
printf("delete element %d",k);
front++;
}}

ANKIT KUMAR SHARMA

25

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

OUTPUT

ANKIT KUMAR SHARMA

26

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

ALGO TO PUSH AN ELEMENT INTO STACK


PUSH (STACK,TOP, MAXSTK,ITEM)
STEP 1:[stack already filled ?]
if the TOP=MAXSTK, then print OVERFLOW and return.
STEP 2:Set TOP=TOP+1
STEP 3:Set STACK[TOP]=ITEM
STEP 4: return

ALGO TO POP AN ELEMENT FROM STACK


POP(STACK,TOP,ITEM)
Step 1:[stack has an item to be removed?]
ifTOP=0,then : print:UNDERFLOW,and return
Step 2:set iITEM :=STACK[TO]
Step 3:set TOP:=TOP-1
Step 4: return

ANKIT KUMAR SHARMA

27

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

W.A.P TO PUSH AND POP IN A STACK


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>
void push(int [],int);
void pop(int []);
void display(int []);
int a[10],i,top=-1;
void main()
{
clrscr();
int k=0;
int n;
char choice;
do
{
printf("\n insert>i\t delete>d\t display>s\t
quit>q\t");
printf("\n input the choice\n");
do
{
choice=getchar();
choice=tolower(choice);
}while(strchr("idsq",choice)==NULL);
printf("your choice %c:\n",choice);
switch(choice)
{
case 'i':printf("enter the num\n");
scanf("%d",&n);
push(a,n);
break;
case 'd':pop(a);
break;
case 's':display(a);
break;
ANKIT KUMAR SHARMA

28

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

case 'q':k=1;
}
}while(!k);
}
void push(int a[10],int n)
{
if(top==9)
printf("stck is full");
else
++top;
a[top]=n;
}
void display(int a[10])
{
if(top==-1)
printf("stck is empty\n " );
else
for(i=top;i>0;i--)
{
printf("\n%d\t",a[i]);
}
}
void pop(int a[10])
{
int k;
if(top==-1)
printf("stack is empty");
else
{
k=a[top];
printf("delete iteam =%d",k);
--top;
}
}

ANKIT KUMAR SHARMA

29

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

OUTPUT

ANKIT KUMAR SHARMA

30

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

ALGORITHM OF GCD
Step 1: read two number A&B.
Step 2: function int gcd(int x,int y)
Step 3: while(x!=y)
Step 4: if(x>y)
return gcd(x-y,y);
Step 5: else
Step 6 return gcd(x,y-x);
Step 7: return x;

ANKIT KUMAR SHARMA

31

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

W.A.P OF GREATEST COMMON DIVEDED USING IN RECURSON


#include<stdio.h>
#include<conio.h>
int divid(int,int);
void main()
{
clrscr();
int n,m,k;
printf("enter the two number");
scanf("%d %d",&n,&m);
printf("%d %d",n,m);
k=divid(n,m);
printf("\ngreatest common divid=%d",k);
getch();
}
int divid(int n,int m)
{
if((n>=m)&&(n%m==0))
return(n);
else
divid(m,(n%m));
}

ANKIT KUMAR SHARMA

32

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

OUTPUT

ANKIT KUMAR SHARMA

33

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

ALGO FOR COMPUTING FIBONACCI SERIES


BINARYFIB(K)
STEP 1: NON-NEGATIVE INTEGER K
STEP 2: THE Kth FIBONACCI NUMBER FK
IF K=1 THEN RETURN K
ELSE
RETURN BINARYFIB(K-1)+BINARYFIB(K-2)
STEP 3:EXIT.

ANKIT KUMAR SHARMA

34

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

W.A.P OF

DATA STRUCTURE USING C

FIBONACCI

SERIES

#include<stdio.h>
#include<conio.h>
long fibo_num(int);
void main()
{
int n;
int i=0;
printf("enter the value");
scanf("%d",&n);
printf("\n fibo num \n");
while(i<n)
{
i++;
printf("\n %d",fibo_num(i));
}
getch();
}
long fibo_num(int n)
{
int r;
if((n==1)||(n==0))
return(0);
else
if(n==2)
return(1);
else
return(fibo_num(n-1)+fibo_num(n-2));
}

ANKIT KUMAR SHARMA

35

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

OUTPUT

ANKIT KUMAR SHARMA

36

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

PROGRAM

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
# define size 10
char ch;
int q[size];
int rear=0;
int front=0;
void insert_queue();
void display_queue();
void delet_queue();
void dqinsert_front();
void dqdelet_rear();

void insert_queue()
{
printf("\n input the element:= ");
scanf("%d",&ch);
if(rear<size-1)
{
ANKIT KUMAR SHARMA

37

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

rear++;
q[rear]=ch;
if (front==0)
front=1;
}
else
printf("\n queue is overflow");
}
void delete_queue()
{
if(front==0)
{
printf("\nunderflow");
return;
}
else
{
ch=q[front];
printf("\n element deleted:= %d",ch);
}
if(front==rear)
{
front=0;
ANKIT KUMAR SHARMA

38

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

rear=0;
}
else
front=front+1;
}
void dqinsert_front()
{
if(front==1)
printf("\nqueue is full");
else
{
front=front-1;
printf("\n input the element=");
scanf("%d",&ch);
q[front]=ch;
}
}
void dqdelet_rear()
{
if (front==rear)
{
front=0;
rear=0;
ANKIT KUMAR SHARMA

39

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

printf("\nqueue is empty");
}
else
{
printf("element deleted= %d",q[rear]);
rear=rear-1;
}
}
void display_queue()
{
int i;
if (front==0)
return;
for(i=front;i<=rear;i++)
printf(" %d",q[i]);
printf("\n------------------------------------"
);
}
void main()
{
clrscr();
int k=0;
char choice;

ANKIT KUMAR SHARMA

40

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

do
{
printf("\n insert->i delete->d quit->q
dqinsert->b dqdelet->e");
printf("\n input the choice:= ");
do
{
choice=getchar();
choice=tolower(choice);
}
while(strchr("idqbe",choice)==NULL);
switch(choice)
{
case'i': insert_queue();
printf("\n queue after inserting=");
display_queue();
break;
case'd': delete_queue();
printf("\nqueue content after deletion is=")
display_queue();
break;
case'b':dqinsert_front();
printf("\n again insert=");

ANKIT KUMAR SHARMA

41

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

display_queue();
break;
case'e':
dqdelet_rear();
printf("\n deleted element=");
display_queue();
break;
case'q':
(k=1);
break;
}}
while(!k);
getch();
}

ANKIT KUMAR SHARMA

42

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

OUTPUT

ALGO OF INSERTION SORT


ANKIT KUMAR SHARMA

43

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

INSERTION_SORT(A)
FOR J<-2 TO N
DO KEY<-A[J]
I<-J-1
WHILE I>0 AND A[I]>KEY
DO A[I+1]<-A[I]
I<-I-1
A[I+1]<-KEY
EXIT

W.A.P OF INSERTION SHORTING

ANKIT KUMAR SHARMA

44

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

#include<stdio.h>
#include<conio.h>
void input(int [],int);
void display(int[],int);
void shorts(int [],int);
int a[10],i;
void main()
{
int n;
clrscr();
printf("enter the length of array");
scanf("%d",&n);
input(a,n);
shorts(a,n);
display(a,n);
getch();
}
void input(int a[10],int n)
{
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void display(int a[10],int n)
{
printf("\n after shorting data");
for(i=0;i<n;i++)
{
printf("\ndata is= %d\t",a[i]);
}
}
void shorts(int a[10],int n)
{
int j,temp;
for(j=1;j<n;j++)
{
temp=a[j];
i=j-1;

ANKIT KUMAR SHARMA

45

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

while ((i>=0)&&(a[i]>temp))
{
a[i+1]=a[i];
i=i-1;
}
a[i+1]=temp;
}
}
OUTPUT

ANKIT KUMAR SHARMA

46

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

ALGORITHM

INSERT
Description: Here QUEUE is an array with N
locations. FRONT and REAR points to the front
and rear
elements of the QUEUE. ITEM is the value to be
inserted.
1. If (FRONT == 1 and REAR == N) or (FRONT
== REAR + 1) Then
2. Print: Overflow
3. Else
4. If (REAR == 0) Then [Check if QUEUE is
empty]
(a) Set FRONT = 1
(b) Set REAR = 1
5. Else If (REAR == N) Then [If REAR reaches
end if QUEUE]
6. Set REAR = 1
7. Else
8. Set REAR = REAR + 1 [Increment REAR by 1]
[End of Step 4 If]
9. Set QUEUE[REAR] = ITEM
10. Print: ITEM inserted
[End of Step 1 If]

ANKIT KUMAR SHARMA

47

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

ALGORITHM
DELETE
Description: Here QUEUE is an array with N
locations. FRONT and REAR points to the front
and rear
elements of the QUEUE.
1. If (FRONT == 0) Then [Check for Underflow]
2. Print: Underflow
3. Else
4. ITEM = QUEUE[FRONT]
5. If (FRONT == REAR) Then [If only element is
left]
(a) Set FRONT = 0
(b) Set REAR = 0
6. Else If (FRONT == N) Then [If FRONT reaches
end if QUEUE]
7. Set FRONT = 1
8. Else
9. Set FRONT = FRONT + 1 [Increment FRONT
by 1]
[End of Step 5 If]
10. Print: ITEM deleted
[End of Step 1 If]

ANKIT KUMAR SHARMA

48

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

WAP OF CIRCULAR QUEUE


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>
#define s 5
void insert(int [s],int);
void del(int [s]);
void display(int [s]);
int front=-1,rear=-1,q[s],count=0;
void main()
{
clrscr();
int k=0;
int n;
char choice;
do
{
printf("\n insert>i\t delete>d\t display>s\t
quit>q\t");
printf("\n input the choice\n");
do
{
choice=getchar();
choice=tolower(choice);
}while(strchr("idsq",choice)==NULL);
printf("your choice %c:",choice);
switch(choice)
{
case 'i': printf("enter the num\n");

ANKIT KUMAR SHARMA

49

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

scanf("%d",&n);
insert(q,n);
break;
case 'd': del(q);
break;
case 's':display(q);
break;
case 'q':k=1;
}
}while(!k);
}
void insert(int q[s],int n)
{
if(count==s)
printf("que is full");
else
rear=(rear+1)%s;
q[rear]=n;
count++;
}
void display(int q[s])
{
int i,j=front;
if(count==0)
{
printf("enpy");
}
else
{
for(i=0;i<count;i++)
{
j=(j+1)%s;
printf("%d",q[j]);
}}}
void del(int q[s])
{
int k;
if(count==0)
{

ANKIT KUMAR SHARMA

50

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

printf("que is empty");
}
else
{
k=q[front];
printf("delete element %d",k);
front=(front+1)%s;
count--;
}}
OUTPUT:

ANKIT KUMAR SHARMA

51

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

Write a Program to Linear


Search
Algorithm
Description: Here A is an array having N
elements. ITEM is the value to be searched.
1. Repeat For J = 1 to N
2. If (ITEM == A[J]) Then
3. Print: ITEM found at location J
4. Return
[End of If]
[End of For Loop]
5. If (J > N) Then
6. Print: ITEM doesnt exist
[End of If]
7. Exit

ANKIT KUMAR SHARMA

52

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

PROGRAM
#include <stdio.h>
#include<conio.h>
void linear_search();
int a[50], item, n, i;
void main()
{
Clrscr();
printf("\n\nEnter size of an array: ");
scanf("%d", &n);
printf("\n\nEnter elements of an array:\n");
for (i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nEnter item to search: ");
scanf("%d", &item);
linear_search();
getch();
}
void linear_search()
{
for (i=0; i<n; i++)
if (item == a[i])
{
printf("\n\nItem found at location %d", i+1);
return;
}
if (i == n)
ANKIT KUMAR SHARMA

53

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

printf("\n\nItem doesnot exist.");


}

OUTPUT

ANKIT KUMAR SHARMA

54

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

Write a Program to BINARY


SEARCH

Algorithm
Description: Here A is a sorted array having N
elements. ITEM is the value to be searched.
BEG denotes
first element and END denotes last element in
the array. MID denotes the middle value.
1. Set BEG = 1 and END = N
ANKIT KUMAR SHARMA

55

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

2. Set MID = (BEG + END) / 2


3. Repeat While (BEG <= END) and (A[MID]
ITEM)
4. If (ITEM < A[MID]) Then
5. Set END = MID 1
6. Else
7. Set BEG = MID + 1
[End of If]
8. Set MID = (BEG + END) / 2
[End of While Loop]
9. If (A[MID] == ITEM) Then
10. Print: ITEM exists at location MID
11. Else
12. Print: ITEM doesnt exist
[End of If]
13. Exit

PROGRAM
#include <stdio.h>
#include<conio.h>
void binary_search();
int a[50], n, item, loc, beg, mid, end, i;
void main()
{
Clrscr();
ANKIT KUMAR SHARMA

56

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

printf("\nEnter size of an array: ");


scanf("%d", &n);
printf("\nEnter elements of an array in sorted
form:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nEnter ITEM to be searched: ");
scanf("%d", &item);
binary_search();
getch();
}
void binary_search()
{
beg = 0; end = n-1;
mid = (beg+end)/2;
while ((beg<=end) && (a[mid]!=item))
{
if (item < a[mid])
end = mid-1;
else
beg = mid+1;
mid = (beg+end)/2;
}
if (a[mid] == item)
printf("\n\nITEM found at location %d", mid+1);
else
printf("\n\nITEM doesn't exist");
}

OUTPUT

ANKIT KUMAR SHARMA

57

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

Write a Program BUBBLE SORT

ANKIT KUMAR SHARMA

58

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

Algorithm
Description: Here A is an unsorted array
having N elements.
1. Repeat For J = 1 to N
2. Repeat For K = 1 to N-J
3. If (A[K] > A[K+1]) Then
4. Interchange A[K] and A[K+1]
[End of If]
[End of Step 2 For Loop]
[End of Step 1 For Loop]
5. Exit

PROGRAM:ANKIT KUMAR SHARMA

59

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

#include <stdio.h>
#include<conio.h>
void bubble_sort();
int a[50], n;
void main()
{
int i;
clrscr();
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
bubble_sort();
printf("\n\nAfter sorting:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void bubble_sort()
{
int j, k, temp;
for(j=0; j<n; j++)
for(k=0; k<(n-1)-j; k++)
if(a[k] > a[k+1])
{
temp = a[k];
a[k] = a[k+1];
a[k+1] = temp;
}
}

ANKIT KUMAR SHARMA

60

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

OUTPUT

ANKIT KUMAR SHARMA

61

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

Write a Program Insertion SORT

Algorithm
Description: Here A is an unsorted array
having N elements.
1. Repeat For J = 2 to N
2. Set TEMP = A[J]
3. Set K = J - 1
4. Repeat While (K >= 1) and (A[K] > TEMP)
5. Set A[K+1] = A[K]
6. Set K = K - 1
[End of While Loop]
7. Set A[K+1] = TEMP
[End of For Loop]
8. Exit

ANKIT KUMAR SHARMA

62

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

PROGRAM:-

#include <stdio.h>
#include<conio.h>
void insertion_sort();
int a[50],n;
void main()
{
int i;
clrscr();
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
insertion_sort();
printf("\n\nAfter sorting:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void insertion_sort()
{
int j, k, temp;
for(j=1; j<n; j++)
{
temp = a[j];
k = j-1;
while (k>=0 && a[k]>temp)
{
a[k+1] = a[k];
k--;
}
a[k+1] = temp;
}

ANKIT KUMAR SHARMA

63

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

OUTPUT

ANKIT KUMAR SHARMA

64

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

Write a Program SELECTION SORT


Algorithm
Description: Here A is an unsorted array having
N elements.
1. Repeat For J = 1 to N
2. Set MIN = J
3. Repeat For K = J+1 to N
4. If (A[K] < A[MIN]) Then
5. Set MIN = K
[End of If]
[End of Step 3 For Loop]
6. Interchange A[J] and A[MIN]
[End of Step 1 For Loop]
7. Exit

ANKIT KUMAR SHARMA

65

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

PROGRAM:
#include <stdio.h>
#include<conio.h>
void selection_sort();
int a[50], n;
void main()
{
int i;
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
selection_sort();
printf("\n\nAfter sorting:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void selection_sort()
{
int i, j, min, temp;
for (i=0; i<n; i++)
{
min = i;
for (j=i+1; j<n; j++)
{
if (a[j] < a[min])
min = j;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
ANKIT KUMAR SHARMA

66

ROLLN0.1100514006

FMCA RBS COLLEGE,AGRA

DATA STRUCTURE USING C

OUTPUT

ANKIT KUMAR SHARMA

67

ROLLN0.1100514006

You might also like