Upload
vnsharma1992
View
222
Download
0
Embed Size (px)
Citation preview
7/30/2019 data structure file
1/310
Data StructuresUsing
C Language
1
7/30/2019 data structure file
2/310
Q.1 Write pseudo code and program for inputting an array of integer and to sort them using Bubble sort.
Ans.
PSEUDO CODE
Algorithm. MAIN( ). This algorithm input the elements, sorts them in an orderly way
using bubble sort technique and displays them on the screen.
Step 1: [Inputting the elements.]
Call GET( )
Step 2: [Displaying the elements in an unsorted way.]
Write( Unsorted array is: )
Call DISPLAY( )
Step 3: [Sorting the elements]
Call BUB_SORT( )
Step 4: [Displaying the elements in ascending order]Write( Sorted array is: )Call DISPLAY( )
Step 5: [Finished]
Exit
Procedure. GET( ). This procedure inputs the elements into array. Here A is the array
variable, I is the loop-control variable and N depicts the number of
elements in array A.
Step 1: [Input the number of elements.]
Write( Enter how many elements you want:)
Read(N)
Step 2: [Input the elements.]
Repeat for I 1, 2, 3, ,N
Read(A[I])Step 3: [Finished]
Return
Procedure. BUB_SORT ( ). This procedure sort the elements in an ascending order
using Bubble sort technique. Here A is the array variable, I is
the loop-control variable, DUMMY is a temporary variable, N
depicts the number of elements in array and J is the variable
that tells the position of next element in the array to be
sorted.
Step 1: [Sorting of elements in array]
Repeat for I N, N-1, 2 Step -1
Repeat for J 1, 2, 3, , I
If ( A[J] > A[I])
Then DUMMY A[I]
2
7/30/2019 data structure file
3/310
A[I] A[J]
A[J] DUMMY
Step 2: [Finished]
Return
Procedure. DISPLAY( ). This procedure is displaying the elements on the screen. Here
A is the array variable, I is the loop-control variable and Ndepicts the number of elements in array.
Step 1: [Displaying the elements on the screen.]
Repeat for I 1, 2, 3, , NWrite (A[I])
Step 2: [Finished]
Return
3
7/30/2019 data structure file
4/310
Program Coding
#include#include
int a[20], n, i;
void get( );void bub_sort( );
void display( );
void main( )
{
clrscr( );
get( );
clrscr( );
printf("\n :UNSORTED ARRAY: \n");display( );
bub_sort( );
printf("\n\n :SORTED ARRAY: \n");display( );getch( );
}
void get( )
{ printf("\n Enter how many elements do you want :- ");
scanf("%d", &n);
printf("\n Enter your array elements :- \n");
for(i = 0; i< n; i++)
scanf("%d", &a[i]);
getch( );
}void bub_sort( )
{ int j, dummy;
for(i= n-1; i >= 1; i--){
for(j= 0; j < i; j++)
{
if(a[j] > a[i])
{
dummy= a[i];
a[i]= a[j];
a[j]= dummy;
}}
}
}
void display( )
{ for(i= 0; i< n; i++)
printf("\n %d", a[i]);
getch( );
}
4
7/30/2019 data structure file
5/310
Q.2 Write pseudo code and program for inputting an array and to sort them using Insertion sort.
Ans.
PSEUDO CODE
Algorithm. MAIN( ). This algorithm input the elements, sorts them in an orderly way
using Insertion sort technique and displays them on the
screen.
Step 1: [Input the elements.]
Call GET( )
Step 2: [Displaying the elements in an unsorted array.]
Write(' Unsorted array is: ')
Call DISPLAY( )
Step 3: [Sorting of elements.]
Call INS_SORT( )
Step 4: [Displaying the elements in an ascending order.]
Write(' Sorted array is: ')Call DISPLAY( )
Step5: [Finished]
Exit
Procedure. GET( ). This procedure is inputs the elements into array. Here A is the
array variable, I is the loop- control variable and N depicts the
number of elements in array.
Step1: [Input the number of elements]Write(' Enter how many elements do you want)
Read(N)
Step2: [Inputting the elements]
Write( Enter your array elements: )
Repeat for I 1, 2, ... , N
Read(A[I])
Step 3: [Finished]
Return
Procedure. INS_SORT( ). This procedure sorts inputted and unsorted elements in a
particular way using Insertion sort technique. Here A is thearray variable, I is the loop-control variable, DUMMY is a
temporary variable, N depicts the number of elements in the
array and J is the variable that tells the position of next
element in the array to be sorted
Step 1: [Sorting of elements in array.]
J 2
Repeat While (J < N)
DUMMY A[J]
5
7/30/2019 data structure file
6/310
I J-1
Repeat While(( I 0) && (A[I] > DUMMY))
A[I+1] A[I]
I I-1
J J+1
A[I+1] DUMMY
Step 2: [Finished]
Return
Procedure. DISPLAY( ). This procedure is displaying the array of elements on the
screen. Here A is the array variable, I is the loop-control
variable and N depicts the number of elements in array.
Step1: [Displaying the elements on the screen.]
Repeat for I 1, 2, ... , N
Write(A[I])
Step 2: [Finished]
Return
6
7/30/2019 data structure file
7/310
Program Coding#include
#include
int a[20], n, i;
void get( );
void ins_sort( );void display( );
void main( )
{
clrscr( );
get( );
clrscr( );
printf(" \n :UNSORTED ARRAY: \n");
display( );ins_sort( );
printf(" \n\n :SORTED ARRAY: \n");
display( );getch( );
}
void get( )
{
printf(" \n Enter how many elements do you want :- ");
scanf(" %d", &n);
printf(" \n Enter your array elements :- \n");
for(i = 0; i < n; i++)
scanf(" %d", &a[i]);
getch( );
}void ins_sort( )
{ int j = 1, dummy;
while(j < n){
dummy = a[j];
i = j-1;
while((i >= 0) && (a[i]> dummy))
{
a[i+1]= a[i];
i--;
}
a[i+1]= dummy;j++;
}
}
void display( )
{ for(i = 0; i < n; i++)
printf(" \n %d", a[i]);
getch( );
}
7
7/30/2019 data structure file
8/310
Q.3 Write pseudo code and program for inputting an array of integer and to sort them using Selection sort.
Ans.
PSEUDO CODE
Algorithm. MAIN( ). This algorithm input the elements, sorts them in an orderly way
using Insertion sort technique and displays them on the
screen.
Step 1: [Input the elements.]
Call GET( )
Step 2: [Displaying the elements in an unsorted array.]
Write(' Unsorted array is: ')
Call DISPLAY( )
Step 3: [Sorting of elements.]
Call SEL_SORT( )
Step 4: [Displaying the elements in an ascending order.]Write(' Sorted array is: ')
Call DISPLAY( )
Step5: [Finished]
Exit
Procedure. GET( ). This procedure is inputs the elements into array. Here A is the
array variable, I is the loop- control variable and N depicts the
number of elements in array.
Step1: [Input the number of elements]
Write(' Enter how many elements do you want)
Read(N)
Step2: [Inputting the elements]
Write( Enter your array elements: )
Repeat for I 1, 2, ... , N
Read(A[I])
Step 3: [Finished]
Return
Procedure. SEL_SORT( ). This procedure sorts the elements in an ascending order
using Selection sort technique. Here A is the array variable, I
is the loop-control variable, DUMMY is a temporary variable, N
depicts the number of elements in array and J is the variable
that tells the position of next element in the array to be
sorted.
Step 1: [Sorting of elements in array.]
Repeat for I 1, 2, 3, , N
Repeat for J I+2, I+3, , N
8
7/30/2019 data structure file
9/310
If (A[I] > A[J])
Then DUMMY A[J]
A[J] A[I]
A[I] DUMMY
Step 2: [Finished]
Return
Procedure. DISPLAY( ). This procedure is displaying the array of elements on the
screen. Here A is the array variable, I is the loop-control
variable and N depicts the number of elements in array.
Step1: [Displaying the elements on the screen.]
Repeat for I 1, 2, ... , N
Write(A[I])
Step 2: [Finished]
Return
9
7/30/2019 data structure file
10/310
Program Coding
#include#include
int a[20], n, i;
void get( );void sel_sort( );
void display( );
void main( )
{
clrscr( );
get( );
clrscr( );
printf(" \n :UNSORTED ARRAY: \n");display( );
sel_sort( );
printf("\n\n :SORTED ARRAY: \n");display( );getch( );
}
void get( )
{
printf(" \n Enter how many elements do you want :- ");
scanf("%d", &n);
printf("\n Enter your array elements :- \n");
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
getch( );
}void sel_sort( )
{ int j, dummy;
for(i = 0; i < n-1; i++){
for(j= i+1; j< n; j++)
{
if(a[i] > a[j])
{
dummy= a[j];
a[j]= a[i];
a[i]= dummy;
}}
}
}
void display( )
{ for(i= 0; i< n; i++)
printf("\n %d", a[i]);
getch( );
}
10
7/30/2019 data structure file
11/310
Q4. Write pseudo code and program for inputting an array of integer and sort them using Merge sort.
Ans.
PSEUDO CODE
Algorithm. MAIN( ). This algorithm input the elements, sorts them in an orderly way
using Merge sort technique and displays them on the screen.
Step 1: [Input the elements.]
Call GET(A, N )
Step 2: [Displaying the elements in an unsorted array.]
Write(' Unsorted array is: ')
Call DISPLAY( )
Step 3: [Sorting of elements.]
Call SORT( 1, N)
Step 4: [Displaying the elements in an ascending order.]
Write(' Sorted array is: ')Call DISPLAY( )
Step5: [Finished]
Exit
Procedure. GET( A, N). This procedure is inputs the elements into array. Here A is the
array variable, I is the loop- control variable and N depicts the
number of elements in array.
Step1: [Input the number of elements]Write(' Enter how many elements do you want)
Read(N)
Step2: [Inputting the elements]
Write( Enter your array elements: )
Repeat for I 1, 2, ... , N
Read(A[I])
Step 3: [Finished]
Return
Procedure. MERGE( FIRST, SEC, LAST). This procedure merges the input elements,
divides into halves and unsorted elements using mergetechnique. Here A and C are the array variable, I is the loop-
control variable, FIRST(1), SEC((LAST+ FIRST)/2) and
LAST(N) is a temporary variable containing formal
parameters, N depicts the element in the array to be sorted.
Step 1: [Sorting of elements in array.]
I FIRST
J SEC
K FIRST 1
11
7/30/2019 data structure file
12/310
Repeat While((I SEC) and (J LAST))
If (A[I] A[J])
Then K K+1
C[K] A[I]
I I+1
Else
K K+1
C[K] A[J]J J+1
Repeat While (I SEC)
K K+1
C[K] A[J]J J+1
Repeat While (J LAST)
K K+1
C[K] A[J]
J J+1
Repeat for I FIRST, , LAST
A[I] C[I]
Step 2: [Finished]
Return
Procedure. SORT(FIRST, LAST ). This procedure is sorts input and unsorted elements
using Merge sort technique. Here A and C Are the array
variables, I is the loop-control variable, FIRST(1) & LAST(N)is a temporary variable containing formal parameters, N
depicts the number of elements in array, SIZE is a temporary
variable and MID is the variable that tells the position of
middle element in the array to be sorted.
Step 1: [Sorting of input elements]SIZE LAST FIRST +1
If (SIZE > 1)
Then MID= (FIRST+LAST)/2
Call SORT(FIRST, MID)
Call SORT(MID+1, LAST)
Call MERGE(FIRST, MID+1, LAST)
Step 2: [Finished]
Return
Procedure. DISPLAY( ). This procedure is displaying the array of elements on the
screen. Here A is the array variable, I is the loop-controlvariable and N depicts the number of elements in array.
Step1: [Displaying the elements on the screen.]
Repeat for I 1, 2, ... , N
Write(A[I])
Step 2: [Finished]
Return
Program Coding12
7/30/2019 data structure file
13/310
#include
#include
int a[20], n, i, c[20];
void get( );
void sort(int, int);
void merge(int, int, int);void display( );
void main( )
{clrscr( );
get( );
clrscr( );
printf("\n :UNSORTED ARRAY: \n");
display( );
sort(0, n-1);
printf(" \n\n :SORTED ARRAY: \n");
display( );getch( );
}
void get( )
{
printf("\n Enter how many elements do you want :- \n");scanf("%d", &n);
printf("\n Enter your array element :- \n");
for(i= 0; i< n; i++)
scanf("%d", &a[i]);getch( );
}
void sort(int first, int last)
{
int size, mid;
size= last-first+1;
if(size> 1)
{
mid= (first+last)/2;
sort(first, mid);
sort(mid+1, last);
merge(first, mid+1, last);
}}
void merge(int first, int sec, int last)
{
int i, j, k;
i= first, j= sec, k= first-1;
while((i
7/30/2019 data structure file
14/310
if(a[i]
7/30/2019 data structure file
15/310
Q.5 Write pseudo code and program for inputting an array and sort them using Quick sort.
Ans. PSEUDO CODE
Algorithm. MAIN( ). This algorithm input the elements, sorts them in an orderly way
using Quick sort technique and displays them on the screen.
Step 1: [Input the elements.]Call GET( )
Step 2: [Displaying the elements in an unsorted array.]
Write(' Unsorted array is: ')
Call DISPLAY( )
Step 3: [Sorting of elements.]
Call SORT(1, N )
Step 4: [Displaying the elements in an ascending order.]
Write(' Sorted array is: ')
Call DISPLAY( )
Step 5: [Finished]
Exit
Procedure. GET( A, N). This procedure is inputs the elements into array. Here A is the
array variable, I is the loop- control variable and N depicts the
number of elements in array.
Step 1: [Input the number of elements]
Write(' Enter how many elements do you want)
Read(N)
Step 2: [Inputting the elements]
Write( Enter your array elements: )
Repeat for I 1, 2, ... , NRead(A[I])
Step 3: [Finished]
Return
Procedure. SORT(X, W). This procedure sorts inputted and unsorted elements in a
particular way. Here A is the array variable, I is the loop-
control variable, KEY, DUMMY and FLSG are temporaryvariable, X(1) and W(N) are the number variable and J is the
variable that tells the position of next element in the array to
be sorted.
Step 1: [Sorting of elements in the array.]
I X+1
J W
KEY A[X]
FLAG 1
15
7/30/2019 data structure file
16/310
Repeat While (FLAG)
Repeat While(A[I] < KEY)
I I+1
Repeat While (A[J] > Key)
J J-1
If (I < J)
Then DUMMY A[X]
A[I] A[J]
A[J] DUMMYStep 4: FLAG 1
DUMMY A[X]
A[X] A[J]
A[J] DUMMYCall SORT( X, J-1)
Call SORT(J+1, W)
Step 5: [Finished]
Return
Procedure. DISPLAY( ). This procedure is displaying the array of elements on the
screen. Here A is the array variable, I is the loop-control variableand N depicts the number of elements in array.
Step1: [Displaying the elements on the screen.]
Repeat for I 1, 2, ... , N
Write(A[I])
Step 2: [Finished]Return
16
7/30/2019 data structure file
17/310
Program Coding
#include#include
int a[20], n, i;
void display( );void get( );
void sort(int, int);
void main( )
{
clrscr( );
get( );
clrscr( );
printf("\n :UNSORTED ARRAY: \n");display( );
sort(0, n-1);
printf("\n\n :SORTED ARRAY: \n");display( );getch( );
}
void get( )
{
printf("\n Enter the number of elements do you want :- \n");
scanf("%d", &n);
if(n< 20)
{
printf("\n Enter the element :- \n");
for(i= 0; i< n; i++)scanf("%d", &a[i]);
}
else
printf("\n Entered number of elements exceeds size of array... \n");
}
void sort(int x, int w)
{
int j, key, dummy, flag= 1;
if(x< w)
{ i= x+1;
j= w;
key= a[x];
while(flag)
{
while(a[i] < key)
i++;
17
7/30/2019 data structure file
18/310
while(a[j] > key)
j--;
if(i < j)
{
dummy= a[i];
a[i]= a[j];
a[j]= dummy;
}
else
flag= 0;
}
dummy= a[x];
a[x]= a[j];
a[j]= dummy;
sort(x, j-1);
sort(j+1, w);
}
}
void display( )
{
for(i= 0; i< n; i++)
printf("\n %d", a[i]);
getch( );}
18
7/30/2019 data structure file
19/310
Q.6 Write pseudo code and program for inputting an array of integer and to search a particular element by using
Linear search.
Ans.
PSEUDO CODEAlgorithm. MAIN( ). This algorithm input the elements, and searches for a particular
element using linear search technique and displays the result of
search on the screen.
Step 1: [Input the elements in the array.]
Call GET( )
Step 2: [Searching the element]
Call LIN_SEARCH( )
Step 3: [Displaying the result.]
Call DISPLAY( )
Step 4: [Finished]
Exit
Procedure. GET( ). This procedure inputs the elements into array. Here A is the array
variable, I is the loop-control variable and X contain the element
which is to be searched from the array.
Step 1: [Input the number elements]
Write( Enter how many elements you want: )
Read(N)
Step 2: [Input the elements]
Write( Enter your array elements:)
Repeat for I 1, 2, 3, , NRead(A[I])
Step 3: [Input the search element]Write( Enter your element: )
Read(X)
Step 4: [Finished]
Function. LIN_SEARCH( ). This function searches a specific element using linear
search technique. Here A is the array element. I is the loop-control
variable, N depicts the number of elements in array and X contains
the element that is to be searched in the array.
Step 1: [Searching of the element in the array.]Repeat for I 1, 2, 3, , N
If (A[I] = X)
Then Return(I)
Step 2: [Finished]
Return(0)
Procedure. DISPLAY( ). This procedure is displaying the elements on the screen. Here
19
7/30/2019 data structure file
20/310
POS is the variable containing the position of element.
Step 1: [Displaying the result on the screen]
If (POS = 0)
Then Write( Search unsuccessful)
Else
Write( Given element found at position:- POS+1)
Step 2: [Finished]Return
20
7/30/2019 data structure file
21/310
Program Coding
#include#include
int a[20], n, i, pos, x;
void get( );void display( );
int lin_search( );
void main( )
{
clrscr( );
get( );
pos= lin_search( );
display( );getch( );
}
int lin_search( ){
for(i= 0; i< n; i++)
{
if(a[i] == x)
return(i);
}
return(-1);
}
void get( )
{ printf("\n Enter how many elements do you want :- \n");
scanf("%d", &n);
printf("\n Enter your elements in array :- \n");for(i= 0; i< n; i++)
scanf("%d", &a[i]);
printf("\n Enter the element you want to search:- \n");
scanf("%d", &x);
getch( );
}
void display( )
{ if(pos== -1)
printf("\n Given element i.e. %d not found in array...",x);
else
printf("\n Given element found in array at the position = %d",pos+1);
getch( );
}
21
7/30/2019 data structure file
22/310
Q.7 Write pseudo code and coding for inputting an array of integer and to search a particular element by using
Linear search recursively.
Ans.
PSEUDO CODE
Algorithm. MAIN( ). This algorithm input the elements, and searches for a particular
element using linear search technique recursively. U is the variablecontain the position of the element found in the array and displays
the result of search on the screen.
Step 1: [Input the elements in the array.]
Call GET( )
Step 2: [Searching the element]
U = Call LIN_SEARCHR( I )
Step 3: [Displaying the result.]
Call DISPLAY( )
Step 4: [Finished]
Exit
Procedure. GET( ). This procedure inputs the elements into array. Here A is the array
variable, I is the loop-control variable and X contain the element
which is to be searched from the array.
Step 1: [Input the number elements]
Write( Enter how many elements you want: )
Read(N)
Step 2: [Input the elements]
Write( Enter your array elements:)
Repeat for I 1, 2, 3, , NRead(A[I])
Step 3: [Input the search element]
Write( Enter your element: )
Read(X)
Step 4: [Finished]
Return
Function. LIN_SEARCH( I ). This function searches a specific element using linearsearch technique. Here A is the array element. I is the loop-control
variable, N depicts the number of elements in array and X contains
the element that is to be searched in the array.
Step 1: [Searching of the element in the array.]
If (I = N+1)
Then Return(0)
Else If (A[I] = X)
Return (I)
22
7/30/2019 data structure file
23/310
Step 2: [Calling the recursive function.]
Return (Call LIN_SEARCHR(I+1))
Step 3: [Finished]
Return
Procedure. DISPLAY( ). This procedure is displaying the elements on the screen. HereU is the variable containing the position of element.
Step 1: [Displaying the result on the screen]
If (U = 0)Then Write( Search unsuccessful)
Else
Write( Given element X found at position:- U+1)
Step 2: [Finished]
Return
23
7/30/2019 data structure file
24/310
Program Coding#include
#include
int a[20], n, i, u, x;
void get( );
void display( );int lin_searchr( int);
void main( )
{ clrscr( );
get( );
u= lin_searchr(0);
display( );
getch( );
}
int lin_searchr(int i)
{ if(i == n)return(-1);
else
if(a[i] ==x)
return(i);
else
return(lin_searchr(i+1));
}
void get( )
{ printf("\n Enter how many elements U want in array :- ");
scanf("%d", &n);
printf("\n Enter your elements in array :- \n");for(i= 0; i< n; i++)
scanf("\n %d", &a[i]);
printf("\n Enter the element you want to search :- \n");
scanf("%d", &x);
}
void display( )
{
if(u== -1)printf("\n Given element not found in array ...");
else
printf("\n Given element found at position %d",u+1);
getch( );
}
24
7/30/2019 data structure file
25/310
Q. 8 Write pseudo code and program for inputting an array of integer and to search a particular element by
using Binary Search
Ans.
PSEUDO CODE
Algorithm. MAIN( ). This algorithm input the elements, and searches for a particular
element using Binary search technique and displays the result of search on the screen.
Step 1: [Input the elements in the array.]
Call GET( )
Step 2: [Searching the element]
Call BIN_SEARCH( )
Step 3: [Displaying the result.]
Call DISPLAY( )
Step 4: [Finished]
Exit
Procedure. GET( ). This procedure inputs the elements into array. Here A is the array
variable, I is the loop-control variable and X contain the element
which is to be searched from the array.
Step 1: [Input the number elements]
Write( Enter how many elements you want: )
Read(N)
Step 2: [Input the elements]
Write( Enter your array elements:)
Repeat for I 1, 2, 3, , NRead(A[I])
Step 3: [Input the search element]Write( Enter your element: )
Read(X)
Step 4: [Finished]
Return
Procedure. BIN_SEARCH( ). This procedure searches a specific element using binary
search technique. Here A is the array element, I is the loop-
control variable, N depicts the number of elements in the
array, FIRST(1) and LAST(N) are the variable containing
formal parameters and X contains the element which is to besearched from the array.
Step 1: [Searching of the element in the array.]
FIRST 1
LAST N
While(FIRST 1)
Then MID= (FIRST + LAST)/2
If (A[MID] = X)
Then Return (MID)
25
7/30/2019 data structure file
26/310
FLAG 2
Else
If (A[MID] < X)
FIRST = MID+1
Else
LAST= MID -1
Step 2: [Finished]Return(0)
Procedure. DISPLAY( ). This procedure is displaying the elements on the screen. Here
U is the variable containing the position of element.
Step 1: [Displaying the result on the screen]
If (U= 0)
Then Write( Search unsuccessful)
Else
Write( Given element X found at position:- U+1)
Step 2: [Finished]Return
26
7/30/2019 data structure file
27/310
Program Coding
#include#include
int a[20], n, x, u, i;
void get( );void display( );
int bin_search( );
void main( )
{
clrscr( );
get( );
u=bin_search( );
display( );getch( );
}
int bin_search( ){
int mid, first, last;
first= 0, last= n-1;
while(first
7/30/2019 data structure file
28/310
if(u== -1)
printf("\n Given element i.e. %d not found in array ...");
else
printf("\n Given element i.e. %d found at the position :- %d",x,u+1);
getch( );
}
28
7/30/2019 data structure file
29/310
Q 9. Write pseudo code and program for inputting an array of integer and to search a particular element by using
Binary search recursively.
Ans.
PSEUDO CODE
Algorithm. MAIN( ). This algorithm input the elements, and searches for a particular
element using Binary search technique recursively and displays theresult of search on the screen.
Step 1: [Input the elements in the array.]
Call GET( )
Step 2: [Searching the element]
Call BIN_SEARCH( 1, N)
Step 3: [Displaying the result.]Call DISPLAY( )
Step 4: [Finished]Exit
Procedure. GET( ). This procedure inputs the elements into array. Here A is the array
variable, I is the loop-control variable and X contain the element
which is to be searched from the array.
Step 1: [Input the number elements]
Write( Enter how many elements you want: )
Read(N)
Step 2: [Input the elements]Write( Enter your array elements:)
Repeat for I 1, 2, 3, , N
Read(A[I])
Step 3: [Input the search element]
Write( Enter your element: )
Read(X)
Step 4: [Finished]
Return
Function. BIN_SEARCHR( FIRST, LAST). This function searches a specific element
using binary search recursively. Here A is the array
element, I is the loop-control variable, N depicts thenumber of elements in the array, FIRST(1) and LAST(N)
are the variables containing formal parameters and X
contain the element which is to be search from the
array.
Step 1: [Searching of the element from the array.]
If ( FIRST LAST)/2
Then MID = (FIRST + LAST)/2
If (A[MID] = X)
29
7/30/2019 data structure file
30/310
Then RETURN( MID)
Else
If (A[MID] < X)
Then Return( Call BIN_SEARCHR(MID+1,LAST)
Step 2: [Finished]
Return (Call BIN_SEARCHR(FIRST, MID-1))
Procedure. DISPLAY( ). This procedure is displaying the elements on the screen. Here
U is the variable containing the position of element.
Step 1: [Displaying the result on the screen]If (U = 0)
Then Write( Search unsuccessful)
Else
Write( Given element X found at position:- U+1)
Step 2: [Finished]
Return
30
7/30/2019 data structure file
31/310
Program Coding#include
#include
int a[20], n, x, u, i;
void get( );
void display( );int bin_searchr(int, int);
void main( )
{ clrscr( );
get( );
u= bin_searchr(0, n-1);
display( );
getch( );
}
int bin_searchr(int first, int last){ int mid;
if(first
7/30/2019 data structure file
32/310
Q.10 Write pseudo code and program for implementing different operation on simple array.
Ans.
PSEUDO CODE
Algorithm. MAIN( ). This algorithm show menu and Calling different procedures for
operation on simple array. CHOICE and ANS are integer and
character variables respectively for storing selected option.
Step 1: [Menu for operation.]
N 1
Repeat While (True)
Write( Operations on simple array)
Write( 1- Input)
Write( 2- Insertion)
Write(3- Deletion)
Write(4- Output)Write(5- Exit)
Write( Enter your choice)
Read(CHOICE)
Step 2: [Calling the functions]
Select Case(CHOICE)
Case 1: Call INPUT( )
Case 2:
If (N 10)
Call INSERT( )
Else
Write( Array full, Insertion not possible)
Case 3:If ( N 1)
Call DEL( )
ElseWrite( Array empty, Deletion not possible)
Case 4:
Call OUTPUT( )
Case 5:
Write( You have chosen exit)
Write(You want to exit? If yes press y)
Read(ANS)If ( ANS = y or ANS = Y)
Then EXIT
Default:
Write(Invalid Condition)
Step 3: [Finished]
Exit
Procedure. INPUT( ). This procedure input the elements into the array, N depicts the
32
7/30/2019 data structure file
33/310
number of elements in the array, I is the loop- control
variable. A represents array.
Step 1: [Input elements into array]
Write( Enter number of elements:)
Read(N)
Write( Enter number of elements:)
Repeat for I 1, 2, 3, .. , N
Read(A[I])
Step 2: [Finished]
Return
Procedure. OUTPUT( ). This procedure displays the elements in the array on the
screen. A is the array element. I is the loop-control variable.
N depicts the number of elements in array A.
Step 1: [Displaying the result on the screen.]
Write(Elements in the array are)
Repeat for I 1, 2, 3, , NRead(A[I])
Step 2: [Finished]
Return
33
7/30/2019 data structure file
34/310
Procedure. INSERT( ). This procedure inserts an element or given information into
given position, POS contains the position where the element is
to be inserted. A represents the array. N depicts the number
of elements in the array A.
Step 1: [Entering and inserting the element]
Write( Enter the position where element is to be inserted:)
Read(POS)
If ((POS < 1) or (POS> N+1))Then Write(Specified position is out of range)
Else
Repeat for I N, N-1, , POS, STEP-1
A[I+1] A[I]Write(Enter the element to be inserted)
Read(A[POS-1])
N N+1
Step 2: [Finished]
Return
Procedure. DELETION( ). This procedure deletes an element from the given position.
POS contains the position where the element is to be inserted.
A represents the array. N depicts the number of elements in array A.
Step 1: [Entering and deleting element]
Write(Enter the position where element is to be deleted:)Read(POS)
If ((POS>10) or (POS(N+1)))
Then Write(Specified position is out of range)Else
Write(Element to be deleted: , A[POS-1])Repeat for I POS, POS-1, , N-1
A[I] A[I+1]
N N-1
Step 2: [Finished]
Return
34
7/30/2019 data structure file
35/310
Program Coding
#include#include
#include
void input( );
void output( );void deletion( );
void insert( );
int a[10], i, n;
void main( )
{
clrscr( );
int ch;
char ans;while(1)
{
clrscr( );printf("\n \t Operations on array");printf("\n\t------------------------");
printf("\n\t 1- Input);
printf( \n\t 2- Insertion);
printf( \n\t 3- Deletion );
printf(\n\t 4- Output );
printf(\n\t 5- Exit ");
fflush(stdin);
printf("\n\n Enter your choice :- ");
scanf("%d", &ch);
switch(ch){
case 1:
input( );break;
case 2:
if(n != 10)
insert( );
else
{
printf("\n Array full, insertion not possible. ");getch( );
}
break;
case 3:
if(n != 0)
deletion( );
else
35
7/30/2019 data structure file
36/310
{
printf("\n Array empty, deletion not possible. ");
getch( );
}
break;
case 4:
output( );
break;
case 5:
printf("\n You have chosen to exit");
printf("\n You want to exit? If yes press y");fflush(stdin);
scanf("%c", &ans);
if(ans == 'y' || ans == 'Y')
exit(0);
else
break;
default:
printf("\n Invalid choice .....");
getch( );
break;
}}
}
void input( ){
printf("\n Enter the number of elements:- ");scanf("%d", &n);
printf("\n Enter elements :- \n");
for(i= 0; i< n; i++)
scanf("%d", &a[i]);
}
void output( )
{
printf("\n Element in array are :-");
for(i= 0; i< n; i++)
printf("\n %d", a[i]);
getch( );}
void insert( )
{
int pos;
printf("\n Enter the position where element is to be inserted :- ");
scanf("%d", &pos);
36
7/30/2019 data structure file
37/310
if((pos < 1) || (pos > (n+1)))
printf("\n Specified position is out of range... ");
else
{
for(i= n-1; i>= pos-1; i--)
a[i+1] = a[i];
printf("\n Enter the element to be inserted :- ");
scanf("%d", &a[pos-1]);n++;
}
getch( );
}
void deletion( )
{
int pos;
printf("\n Enter the position where element is to be deleted :- ");
scanf("%d", &pos);
if((pos < 0) || (pos > n))printf("\n Specified position is out of range... ");
else
{
for(i= pos; i
7/30/2019 data structure file
38/310
Q.11 Write the pseudo code and program for implementing stack using array
Ans.
PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show the menu operation on the stack
and Calling of all functions. TOP points to the element on the top of
stack, DATA contains the element to be pushed or popped. CHOICEand ANS are integer and character variables for storing chosen
option.
Step 1: [Displaying the menu.]
TOP 0
Repeat While(True)
Write( Implementing stack using array)
Write(1- Push an element from the stack)
Write(2- Pop an element from the stack)Write(3- Display the stack)
Write(4- Exit from output window)
Write(Enter your choice)Read(CHOICE)
Step 2: [Calling the functions]
Select Case(CHOICE)
Case 1: Write(Enter the element pushed into the stack)
Read(DATA)
Call PUSH(DATA)
Case 2: DATA Call POP( )
If (DATA -99)
Then Write(Element popped is :DATA)
Case 3: Call DISPLAY( )
Case 4: Write(You want to exit? If yes then press y)Read(ANS)
If (ANS = y or ANS = Y)
Then EXIT
Default: Write(Invalid choice)
Step 3: [Finished]
Return
Procedure. PUSH(X). This procedure input or push an element passed on as parameter
into the stack. Here A is the array variable, TOP points to the element on
the top of the stack and X contain the element which user have enteredand want to perform operation on it.
Step 1: [Input or pushing the element]
If (TOP = 20)
Then Write(Stack overflow)
Else
TOP TOP+1
A[TOP] X
38
7/30/2019 data structure file
39/310
Step 2: [Finished]
Return
Function. POP( ). This procedure pop or delete the input elements from the top of the
stack. Here A represents the array variable, TOP points to the
element on the top of the stack and DUMMY is an integer variable to store element temporarily.
Step 1: [Sorting of elements in the array.]
If (TOP = 0)
Then Write(Stack underflow)
Return(-99)
Else
DUMMY A[TOP]
TOP TOP+1
Step 2: [Finished]
Return(DUMMY)
Procedure. DISPLAY( ). This procedure display the elements on the screen. Here A is
the array variable, I is the loop-control variable and TOP points to
the element on the stack.
Step 1: [Displaying the elements on the screen.]If (TOP = 0)
Then Write(Stack is empty)
Else
Write(Elements of stack are:)Repeat for I TOP, , 1
Write (A[I])
Step 2: [Finished]
Return
39
7/30/2019 data structure file
40/310
Program Coding
#include#include
#include
void push(int);
int pop( );void display( );
int a[20], top;
void main( )
{
clrscr( );
int ch, data;
char ans;while(1)
{
clrscr( );printf("\n \t Implementing stack using array");printf("\n\t--------------------------------------");
printf("\n\t 1- Push an element into stack ");
printf("\n\t 2- Pop an element from stack ");
printf("\n\t 3- Display the stack ");
printf("\n\t 4- Exit from output window ");
fflush(stdin);
printf("\n\n Enter your choice :- ");
scanf("%d", &ch);
switch(ch)
{ case 1:
printf("\n Enter element to be pushed into the stack");
fflush(stdin);scanf("%d", &data);
push(data);
break;
case 2:
data= pop( );
if(data != -99)
printf("\n Element popped is :%d", data);
fflush(stdin);getch( );
break;
case 3:
display( );
break;
case 4:
printf("\n You have chosen to exit");
40
7/30/2019 data structure file
41/310
printf("\n You want to exit? If yes press y");
fflush(stdin);
scanf("%c", &ans);
if(ans == 'y' || ans == 'Y')
exit(0);
else
break;
default:
printf("\n Invalid choice .....");
getch( );break;
}
}
}
void push(int x){
if(top == 19)
printf("\n\n Stack overflow");
else
{top++;
a[top] = x;
}
}
int pop( ){
int dummy;
if(top == -1)
{
printf("\n\n Stack underflow");
getch( );
return(-99);
}
else
{
dummy= a[top];top--;
return(dummy);
}
}
void display( )
{
41
7/30/2019 data structure file
42/310
int i;
if(top == -1)
printf("\n\n Stack is empty");
else
{
printf("\n\n Elements in stack :- ");
for(i= top; i >= 0; i--)
printf("\n %d", a[i]);}
getch( );
}
42
7/30/2019 data structure file
43/310
Q.12 Write pseudo code and program for implementing two stack within one array.
Ans.
PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show the menu operation on the stack
and Calling of all functions. TOP1 and TOP2 points to the element on
the top of the two stacks, DATA contains the element to be pushedor popped. CHOICE, CH and ANS are integer and character variables
for storing chosen option.
Step 1: [Displaying the menu.]
TOP1 0
TOP2 10
Repeat While(True)
Write( Implementing stack using array)
Write(1- Push an element from the stack)Write(2- Pop an element from the stack)
Write(3- Display the stack)
Write(4- Exit from output window)Write(Enter your choice)
Read(CHOICE)
Step 2: [Calling different functions]
Select Case(CHOICE)
Case 1: Write( Enter where the element is to be pushed)
Write( For Stack1-press 1 & for Stack2-press 2:)
Read(CH)
Write(Enter element to be pushed into stack:)
Read(DATA)
If (CH = 1)
Then Call PUSH1(DATA)
Else
If (CH = 2)Call PUSH2(DATA)
Else
Write(Invalid choice)
Case 2: Write(Enter from where element to be deleted)
Write(For Stack 1-press 1 & Stack 2-press 2:)
Read(CH)
If (CH = 1)
Then DATA Call POP1( )Else
If (CH = 2)
DATA Call POP2( )
Else
Write(Invalid choice)
If (DATA -99)
Then Write( Element popped is:, DATA)
43
7/30/2019 data structure file
44/310
Case 3: Write(Enter which stack is to be viewed)
Write( For Stack 1-press 1 & for Stack 2-press 2)
Read (CH)
If (CH = 1)
Then Call DISPLAY1( )
Else
If (CH = 2)
Call CISPLAY2( )
ElseWrite(Invalid Choice)
Case 4: Write(You have chosen to exit)
Write( Sure you want to exit? If press y:)Read(ANS)
If (ANS = y or ANS = Y)
Then EXIT
Default: Write( Invalid choice)
Step 3: [Finished]
Return
Procedure. PUSH1(X). This procedure input or push an element passed on as parameter
into the stack. Here A is the array variable, TOP1 points to the
element on the top of the stack and X contain the element which
user have entered and want to perform operation on it.
Step 1: [Input or pushing the element]
If (TOP1 = 10)Then Write(Stack overflow)
ElseTOP1 TOP1+1
A[TOP1] X
Step 2: [Finished]
Return
Procedure. PUSH2(X). This procedure input or push an element passed on as
parameter into the stack. Here A is the array variable, TOP2 points
to the element on the top of the stack and X contain the element
which user have entered and want to perform operation on it.
Step 1: [Input or pushing the element]
If (TOP2 = 20)
Then Write(Stack overflow)
Else
TOP2 TOP2+1
A[TOP2] X
Step 2: [Finished]
44
7/30/2019 data structure file
45/310
Return
Function. POP1( ). This procedure pop or delete the input elements from the top of the
stack. Here A represents the array variable, TOP1 points to the
element on the top of the stack and DUMMY is an integer variable to
store element temporarily.
Step 1: [Sorting of elements in the array.]
If (TOP1 = 0)
Then Write(Stack underflow)
Return(-99)
Else
DUMMY A[TOP1]
TOP1 TOP1+1
Step 2: [Finished]
Return(DUMMY)
Function. POP2( ). This procedure pop or delete the input elements from the top of
the stack. Here A represents the array variable, TOP2 points to the
element on the top of the stack and DUMMY is an integer variable to
store element temporarily.
Step 1: [Sorting of elements in the array.]
If (TOP2 = 10)
Then Write(Stack underflow)Return(-99)
Else
DUMMY A[TOP2]
TOP2 TOP2+1
Step 2: [Finished]
Return(DUMMY)
Procedure. DISPLAY1( ). This procedure display the elements on the screen. Here A is
the array variable, I is the loop-control variable and TOP1 points to
the element on the stack.
Step 1: [Displaying the elements on the screen.]
If (TOP1 = 0)
Then Write(Stack is empty)
Else
Write(Elements of stack are:)
Repeat for I TOP1, , 1
Write (A[I])
45
7/30/2019 data structure file
46/310
Step 2: [Finished]
Return
Procedure. DISPLAY2( ). This procedure display the elements on the screen. Here A is
the array variable, I is the loop-control variable and TOP2 points to
the element on the stack.
Step 1: [Displaying the elements on the screen.]
If (TOP2 = 10)
Then Write(Stack is empty)Else
Write(Elements of stack are:)
Repeat for I TOP1, , 20
Write (A[I])Step 2: [Finished]
Return
46
7/30/2019 data structure file
47/310
Program Coding
#include#include
#include
void push1(int);
int pop1( );void display1( );
void push2(int);
int pop2( );
void display2( );
int a[20], top1, top2;
void main( )
{clrscr( );
int choice, ch, data;
char ans;top1= -1;top2= 9;
while(1)
{
clrscr( );
printf("\n \t Implementing stack using one array");
printf("\n\t------------------------------------------------);
printf("\n\t 1- Push an element into stack);
printf("\n\t 2- Pop an element from stack);
printf("\n\t 3- Display the stack ");
printf("\n\t 4- Exit from output window ");fflush(stdin);
printf("\n\n Enter your choice :- ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\n Enter where element is to be pushed ");
printf("\nFor Stack1-press 1 & Stack2-press 2 :");
fflush(stdin);
scanf("%d", &ch);
printf("\n\n Enter element to be pushed into stack"); fflush(stdin);scanf("%d", &data);
if(ch== 1)
push1(data);
else if(ch== 2)
push2(data);
else
47
7/30/2019 data structure file
48/310
printf("\n Invalid choice ........");
break;
case 2:
printf("\nEnter from where element to be deleted ");
printf("\n For Stack1-press 1 & Stack2-press 2 :");
fflush(stdin);
scanf("%d", &ch);
if(ch==1)
data= pop1( );
else if(ch==2)data=pop2( );
else
printf("\n Invalid choice ......");
if(data!= -99)
printf("\n Element popped is :%d", data);
fflush(stdin);printf("\n\n Press any key to continue ");
fflush(stdin);
getch( );
break;
case 3:printf("\n Enter which stack is to be viewed ");
printf("\n For Stack1-press 1 & Stack2-press 2 :");
fflush(stdin);
scanf("%d", &ch);
if(ch==1)display1( );
else if(ch==2)
display2( );
else
printf("\n Invalid choice ........");
printf("\n\n Press any key to continue ");
fflush(stdin);
getch( );
break;
case 4:
printf("\n You have chosen to exit");
printf("\n You want to exit? If yes press y");
fflush(stdin);
scanf("%c", &ans);
if(ans == 'y' || ans == 'Y')
exit(0);
48
7/30/2019 data structure file
49/310
else
break;
default:
printf("\n Invalid choice .....");
getch( );
break;
}}
}
void push1(int x){
if(top1== 9)
printf("\n\n Stack1 overflow");
else
{
top1++;
a[top1]=x;}
}
void push2(int x)
{
if(top2== 19)printf("\n\n Stack2 overflow");
else
{top2++;
a[top2]=x;}
}
int pop1( )
{
int dummy;
if(top1== -1)
{
printf("\n\n Stack1 underflow");
getch( );
return(-99);
}
else
{
dummy= a[top1];
top1--;
return(dummy);
}
}
49
7/30/2019 data structure file
50/310
int pop2( )
{
int dummy;
if(top2== 9)
{
printf("\n\n Stack2 underflow");
getch( );
return(-99);}
else
{dummy=a[top2];
top2--;
return(dummy);
}
}
void display1( )
{int i;
if(top1== -1)
printf("\n\n Stack1 is empty");
else
{printf("\n\n Elements in stack1 are :- ");
for(i= top1; i>= 0; i--)
printf("\n %d", a[i]);
}getch( );
}
void display2( )
{
int i;
if(top2== 9)
printf("\n\n Stack2 is empty");
else
{
printf("\n\n Elements in stack2 are :- ");
for(i= top2; i>= 10; i--)
printf("\n %d", a[i]);}
getch( );
}
50
7/30/2019 data structure file
51/310
Q. 13 Write pseudo code and program for Implementing simple queue using array.
Ans.
PSEUDO CODE
Algorithm. MANI( ). This algorithm is used to show the menu operation on queue and
Calling of all functions. FRONT points to the element on the top of
the queue, DATA contains the element to be inserted or deleted,REAR points to the element in the back. CHOICE and ANS are
variables for storing chosen option.
Step 1: [Displaying the menu.]
FRONT 0
REAR 0
Repeat While(True)
Write( Implementing Simple queue using array)
Write(1- Insert an element from the queue)Write(2- Delete an element from the queue)
Write(3- Display the queue)
Write(4- Exit from output window)Write(Enter your choice)
Read(CHOICE)
Step 2: [Calling the functions.]
Select Case(CHOICE)
Case 1: Write( Enter element to inserted into the stack)
Read(DATA)
Call INSERT(DATA)
Case 2: DATA Call DEL( )
If (DATA -99)
Then Write(Element deleted is:, DATA)
Case 3: Call DISPLAY( )
Case 4: Write (You want to exit? If yes press y)Read(ANS)
If (ANS = y or ANS = Y)
Then EXIT
Default: Write( Invalid choice)
Step 3: [Finished]
Return
Procedure. INSERT( X). This procedure input or pushing an element passed as
parameter into the queue. Here A is the element in the last and X is
the variable containing the element.
Step 1: [Input or insertion of element]
If (REAR = 20)
Then Write( Simple queue is full)
Else
REAR REAR +1
A[REAR] X
If (FRONT = 0)
51
7/30/2019 data structure file
52/310
Then FRONT FRONT +1
Step 2: [Finished]
Return
Function. DEL( ). This function delete the input elements from the queue. Here A
represents the array variable, FRONT points to the element on the
top of the queue, DUMMY is an integer variable to store elementtemporarily and REAR points to the elements in the last.
Step 1: [Deletion of elements from the queue]
If (FRONT = 0)Then Write( Simple Queue is empty)
Return (-99)
Else
DUMMY A[FRONT]
If (FRONT = REAR)
Then FRONT FRONT+1
Step 2: [Finished]
Return(DUMMY)
Procedure. DISPLAY( ). This procedure display the elements on the screen. Here A is
the array variable, I is the loop-control variable and FRONTpoints to the element in the last.
Step 1: [Displaying the elements on the screen.]
If (FRONT = 0)Then Write( Queue is empty)
ElseWrite(Elements of queue are:)
Repeat for I FRONT, , REAR
Write (A[I])
Step 2: [Finished]
Return
52
7/30/2019 data structure file
53/310
Program Coding
#include#include
#include
void insert(int);
int del( );void display( );
int a[20], rear, front;
void main( )
{
clrscr( );
int ch, data;
char ans;front= -1;
rear= -1;
while(1)
{
clrscr( );
printf("\n \t Implementing simple queue using array");
printf("\n\t------------------------------------------------");
printf("\n\t 1- Insert an element into queue ");
printf("\n\t 2- Delete an element from the queue ");
printf("\n\t 3- Display the queue ");
printf("\n\t 4- Exit from output window ");
fflush(stdin);
printf("\n\n Enter your choice :- ");
scanf("%d", &ch);
switch(ch)
{case 1:
printf("\n Enter the element to be inserted into
the simple queue: );
fflush(stdin);
scanf("%d",&data);
insert(data);
break;
case 2:data=del( );
if(data != -99)
printf("\n Element deleted is :- %d", data);
printf("\n Press any key to continue ");
fflush(stdin);
getch( );
break;
case 3:
53
7/30/2019 data structure file
54/310
display( );
printf("\n Press any key to continue ");
fflush(stdin);
getch( );
break;
case 4:
printf("\n You have chosen to exit");
printf("\n You want to exit? If yes press y");fflush(stdin);
scanf("%c", &ans);
if(ans=='y' || ans=='Y')exit(0);
else
break;
default:
printf("\n Invalid choice .....");
getch( );break;
}
}
}
void insert(int x){
if(rear == 19)
printf("\n Simple queue is full");
else{
rear++;a[rear] = x;
if(front == -1)
front++;
}
}
int del( )
{
int dummy;
if(front == -1)
{
printf("\n Simple queue is empty");getch( );
return(-99);
}
else
{
dummy = a[front];
if(front == rear)
54
7/30/2019 data structure file
55/310
front= rear= -1;
else
front++;
return(dummy);
}
}
void display( )
{
int i;
if(front == -1)printf("\n Queue is empty");
else
{
printf("\n Elements in queue :- \n");
for(i= front; i
7/30/2019 data structure file
56/310
Q.14 Write pseudo code and program for Implementing Circular queue using array
Ans.
PSEUDO CODE
Algorithm. MANI( ). This algorithm is used to show the menu operation on queue and
Calling of all functions. FRONT points to the element on the top ofthe queue, DATA contains the element to be inserted or deleted,
REAR points to the element in the back. CHOICE and ANS are
variables for storing chosen option.
Step 1: [Displaying the menu.]
FRONT 0
REAR 0
Repeat While(True)
Write( Implementing Circular queue using array)Write(1- Insert an element from the queue)
Write(2- Delete an element from the queue)
Write(3- Display the queue)Write(4- Exit from output window)
Write(Enter your choice)
Read(CHOICE)
Step 2: [Calling the functions.]
Select Case(CHOICE)
Case 1: Write( Enter element to inserted into the stack)
Read(DATA)
Call INSERT(DATA)
Case 2: DATA Call DEL( )
If (DATA -99)Then Write(Element deleted is:, DATA)
Case 3: Call DISPLAY( )
Case 4: Write (You want to exit? If yes press y)
Read(ANS)
If (ANS = y or ANS = Y)
Then EXIT
Default: Write( Invalid choice)
Step 3: [Finished]Return
Procedure. INSERT( X). This procedure input or pushing an element passed as
parameter into the queue. Here A is the element in the last and X is
the variable containing the element.
Step 1: [Input or insertion of element.]
If (((REAR = 20)AND(FRONT =0))or(REAR= FRONT-1))
56
7/30/2019 data structure file
57/310
Then Write(Circular queue is full)
Else
If (REAR = 0)
Then FRONT FRONT +1
REAR REAR+1
Else
If (REAR = 20)
REAR 1
ElseREAR REAR+1
A[REAR] X
Step 2: [Finished]Return
Function. DEL( ). This function deletes inputted elements from the queue. Here A
represents the array variable, FRONT points to the element on
the top of the queue, DUMMY is an integer variable to store
temporarily and REAR points to the elements in the last.
Step 1: [Deletion of elements from the queue]
If (FRONT = 0)
Then Write( Simple Queue is empty)
Return (-99)
ElseDUMMY A[FRONT]
If (FRONT = 20)
Then FRONT 1Else
If(FRONT = REAR)FRONT = REAR= 0
Else
FRONT FRONT +1
Step 2: [Finished]
Return(DUMMY)
Procedure. DISPLAY( ). This procedure display the elements on the screen. Here A is
the array variable, I is the loop-control variable and FRONT
points to the element in the last.
Step 1: [Displaying the elements on the screen.]
If (FRONT = 0)
Then Write( Queue is empty)
Else
Write(Elements of queue are:)
If (FRONT REAR)
Then Repeat for I FRONT, , REAR
57
7/30/2019 data structure file
58/310
Write(A[I])
Else
Repeat for I FRONT, , 20
Write(A[I])
Repeat for I 1, , REAR
Write(A[I])
Step 2: [Finished]Return
58
7/30/2019 data structure file
59/310
Program Coding
#include#include
#include
void insert(int);
int del( );void display( );
int a[20], rear, front;
void main( )
{
clrscr( );
int ch,data;
char ans;front= -1;
rear= -1;
while(1){
clrscr( );
printf("\n \t Implementing circular queue using array");
printf("\n\t------------------------------------------------");
printf("\n\t 1- Insert an element into queue ");
printf("\n\t 2- Delete an element from the queue ");
printf("\n\t 3- Display the queue ");
printf("\n\t 4- Exit from output window ");
fflush(stdin);
printf("\n\n Enter your choice :- ");
scanf("%d", &ch);
switch(ch)
{case 1:
printf("\n Enter element inserted into queue");
fflush(stdin);
scanf("%d", &data);
insert(data);
break;
case 2:
data=del( );if(data != -99)
printf("\n Element deleted is :- %d", data);
printf("\n Press any key to continue ");
fflush(stdin);
getch( );
break;
case 3:
display( );
59
7/30/2019 data structure file
60/310
printf("\n Press any key to continue ");
fflush(stdin);
getch( );
break;
case 4:
printf("\n You have chosen to exit");
printf("\n You want to exit? If yes press y");
fflush(stdin);scanf("%c", &ans);
if(ans == 'y' || ans == 'Y')
exit(0);
else
break;
default:
printf("\n Invalid choice .....");
getch( );
break;}
}
}
void insert(int x)
{if(((rear == 19) && (front == 0)) || (rear == front-1))
printf("\n Circular queue is full");
else if(rear== -1){
front++;rear++;
}
else if(rear==19)
rear= 0;
else
rear++;
a[rear]=x;
}
int del( )
{
int dummy;if(front== -1)
{
printf("\n Circular queue is empty");
getch( );
return(-99);
}
else
60
7/30/2019 data structure file
61/310
{
dummy=a[front];
if(front==19)
front=0;
else if(front==rear)
front=rear= -1;
elsefront++;
return(dummy);
}
}
void display( )
{
int i;
if(front==-1)
printf("\n Circular queue is empty");
else if{
printf("\n Elements in queue :- \n");
if(front
7/30/2019 data structure file
62/310
Q.15 Write pseudo code and program for Implementing two Priority Queue using two different array.
Ans.
PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show the menu operations on priority
queue and Calling of all functions. FRONT1 and FRONT2
points to the elements to the top of the queue, DATAcontains the element to be inserted or deleted, REAR1 and
REAR2 points to the element in the back. CHOICE and ANS
are variable for storing chosen option.
Step 1: [Display of menu]
FRONT1 0
REAR1 0
FRONT2 0
REAR2 0Repeat While(True)
Write(Implementing two priority queue using two arrays)
Write(1- Insert an element into queue)Write(2- Delete an element from the queue)
Write(3- Display the queue)
Write(4- Exit from output window)
Write(Enter your choice)
Read(CHOICE)
Step 2: [Calling the functions.]
Select Case(CHOICE)
Case 1: Write(Enter element to inserted into the queue)
Read(DATA)
Call INSERT(DATA)
Case 2: Call DEL( )
Case 3: Call DISPLAY( )
Case 4: Write(You want to exit? If yes press y)
Read(ANS)
If (ANS = y or ANS = Y)
Then EXIT
Default: Write( Invalid choice)
Step 3: [Finished]
Return
Procedure. INSERT(X). This procedure input or push an element passed an as
parameter into the queue. Here A and B are the array variables, FRONT1
and FRINT2 points to the element on the top of the queue, REAR1 and
REAR2 points to the element in the last and X contain the element which is
to be inserted. ANS is to store selected choice.
Step 1: [Selection of Queue]
Write( Enter your priority. Press 1 or 2)
Read(ANS)
62
7/30/2019 data structure file
63/310
Step 2: [Inputs or insertion of element]
If (ANS = 1)
Then If (REAR1 = 10)
Then Write(Priority Queue 1 is full)
Else
REAR1 REAR1 +1
A[REAR1] X
If (FRONT1 = 0)Then FRONT1 FRONT +1
Else If (ANS =2)
If (REAR2 = 10)Then Write(Priority Queue 2 is full)
Else
REAR2 REAR2+1
B[REAR2] X
If (FRONT2 = 0)
Then FRONT2 FRONT2 +1
ElseWrite(Invalid choice)
Step 3: [Finished]
Return
Procedure. DEL( ). This procedure deletes input elements from the queue. Here A andB are the array variables, FRONT1 and FRONT2 points to
the element on the top of the queue, DUMMY is an integer
variable to store the value of the array temporarily and
REAR1 and REAR2 points to the elements in the last. ANS isused to store selected choice.
Step 1: [Delete element from the queue.]
If (FRONT1 0)
Then DUMMY A[FRONT1]
If (FRONT1 = REAR1)
FRONT1 = REAR1 = 0
Else
FRONT1 FRONT1+1
Write(Element deleted from priority queue1 :DUMMY)
Else
Write(Priority Queue 1 is empty)
If (FRONT2 0)Then DUMMY B[FRONT2]
If (FRONT2 = REAR2)
Then FRONT2 = REAR2 = 0
Else
FRONT2 FRONT2 + 1
Write(Element deleted from priority queue 2 :DUMMY)
Else
Write(Priority Queue 2 is empty)
63
7/30/2019 data structure file
64/310
Step 3: [Finished]
Return
Procedure. DISPLAY( ). This procedure display elements of queue on the screen. Here
A and B are the array variables, I is the loop-control variable
and FRONT1 and FRONT2 points to the elements on the top
of the queue and REAR1 and REAR2 points to the element inthe last.
Step 1: [Display the elements on the screen.]
If (FRONT1 0)Then Write(Elements of Priority queue 1 are:)
Repeat for I FRONT1, , REAR1
Write(A[I])
Else
Write(Priority queue 1 is empty)
If (FRONT2 0)
Then Write(Elements of Priority queue 2 are:)Repeat for I FRONT2, ,REAR2
Write (B[I])
Else
Write(Priority queue2 is empty)
Step 2: [Finished]Return
64
7/30/2019 data structure file
65/310
Program Coding
#include#include
#include
void insert(int);
void del( );void display( );
int a[10], b[10], i, rear1, front1, front2, rear2;
void main( )
{
clrscr( );
int ch, data;
char ans;front1 = rear1 = -1;
front2 = rear2 = -1;
while(1){
clrscr( );
printf("\n\t Implementing two Priority queue using 2 arrays");
printf("\n\t--------------------------------------------------");
printf("\n\t 1- Insert an element into queue ");
printf("\n\t 2- Delete an element from the queue ");
printf("\n\t 3- Display the queue ");
printf("\n\t 4- Exit from output window ");
fflush(stdin);
printf("\n\n Enter your choice :- ");
scanf("%d", &ch);
switch(ch)
{
case 1:printf("\n Enter element inserted into queue");
fflush(stdin);
scanf("%d", &data);
insert(data);
break;
case 2:
del( );
break;
case 3:
display( );
printf("\n Press any key to continue ");
fflush(stdin);
getch( );
break;
case 4:
65
7/30/2019 data structure file
66/310
printf("\n You have chosen to exit");
printf("\n You want to exit? If yes press y");
fflush(stdin);
scanf("%c", &ans);
if(ans == 'y' || ans == 'Y')
exit(0);
elsebreak;
default:
printf("\n Invalid choice .....");getch( );
break;
}
}
}
void insert(int x)
{
int ans;
printf("\n Enter your priority . Press 1 or 2");
scanf("%d", &ans);
if(ans == 1)
{
if(rear1 == 9)
printf("\n Priority Queue number 1 is full!");
else{
rear1++;
a[rear1] = x;
if(front1 == -1)
front1++;
}
}
else if(ans == 2)
{
if(rear2 == 9)
printf("\n Priority queue 2 is full");
else{
rear2++;
b[rear2] = x;
if(front2 == -1)
front2++;
}
}
else
66
7/30/2019 data structure file
67/310
printf("\n Invalid choice");
getch( );
}
void del( )
{
int dummy, ans;if(front1 != -1)
{
dummy = a[front1];
if(front1 = rear1)front1= rear1= -1;
else
front1++;
printf("\n Element deleted from queue 1 is %d", dummy);
}
else
{printf("\n Priority queue 1 is empty");
if(front2 -1)
{
dummy= b[front2];
if(front2 == rear2)front2= rear2= -1;
else
front2++;printf("\n Element deleted from queue2 is: dummy);
}else
printf("\n Priority queue2 is empty");
}
}
void display( )
{
if(front1 != -1)
{
printf("\n Elements in queue number 1 are:");
for(i= front1; i
7/30/2019 data structure file
68/310
for(i= front2; i
7/30/2019 data structure file
69/310
Q.16 Write pseudo code and program for Implementing two stack using two different array.
Ans.
PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show the menu operation on the stack
and Calling of all functions. Here A and B are two arrays.TOP1 and TOP2
points to the element on the top of the two stacks, DATA contains theelement to be pushed or popped. CHOICE, CH and ANS are integer and
character variables for storing chosen option.
Step 1: [Displaying the menu.]
TOP1 0
TOP2 0
Repeat While(True)
Write( Implementing stack using array)
Write(1- Push an element from the stack)Write(2- Pop an element from the stack)
Write(3- Display the stack)
Write(4- Exit from output window)Write(Enter your choice)
Read(CHOICE)
Step 2: [Calling different functions]
Select Case(CHOICE)
Case 1: Write( Enter where the element is to be pushed)
Write( For Stack1-press 1 & for Stack2-press 2:)
Read(CH)
Write(Enter element to be pushed into stack:)
Read(DATA)
If (CH = 1)
Then Call PUSH1(DATA)
Else
If (CH = 2)Call PUSH2(DATA)
Else
Write(Invalid choice)
Case 2: Write(Enter from where element to be deleted)
Write(For Stack 1-press 1 & Stack 2-press 2:)
Read(CH)
If (CH = 1)Then DATA Call POP1( )
Else
If (CH = 2)
DATA Call POP2( )
Else
Write(Invalid choice)
If (DATA -99)
Then Write( Element popped is:, DATA)
69
7/30/2019 data structure file
70/310
Case 3: Write(Enter which stack is to be viewed)
Write( For Stack 1-press 1 & for Stack 2-press 2)
Read (CH)
If (CH = 1)
Then Call DISPLAY1( )
Else
If (CH = 2)
Call CISPLAY2( )Else
Write(Invalid Choice)
Case 4: Write(You have chosen to exit)Write( You want to exit? If press y:)
Read(ANS)
If (ANS=y or ANS=Y)
Then EXIT
Default: Write( Invalid choice)
Step 3: [Finished]Return
Procedure. PUSH1(X). This procedure input or push an element passed on as parameter
into the stack. Here A is the array variable, TOP1 points to the element on
the top of the stack and X contain the element which user have enteredand want to perform operation on it.
Step 1: [Input or pushing the element]
If (TOP1 = 20)
Then Write(Stack overflow)Else
TOP1 TOP1+1A[TOP1] X
Step 2: [Finished]
Return
Procedure. PUSH2(X). This procedure input or push an element passed on as
parameter into the stack. Here B is the array variable, TOP2 points
to the element on the top of the stack and X contain the element
which user have entered and want to perform operation on it.
Step 1: [Input or pushing the element]If (TOP2 = 20)
Then Write(Stack overflow)
Else
TOP2 TOP2+1
B[TOP2] X
Step 2: [Finished]
Return
70
7/30/2019 data structure file
71/310
Function. POP1( ). This procedure pop or delete the input elements from the top of the
stack. Here A represents the array variable, TOP1 points to the
element on the top of the stack and DUMMY is an integer variable to
store element temporarily.
Step 1: [Sorting of elements in the array.]If (TOP1 = 0)
Then Write(Stack underflow)
Return(-99)
Else
DUMMY A[TOP1]
TOP1 TOP1+1
Step 2: [Finished]
Return(DUMMY)
Function. POP2( ). This procedure pop or delete the input elements from the top of
the stack. Here B represents the array variable, TOP2 points to the
element on the top of the stack and DUMMY is an integer variable to
store element temporarily.
Step 1: [Sorting of elements in the array.]
If (TOP2 = 0)
Then Write(Stack underflow)
Return(-99)
ElseDUMMY B[TOP2]
TOP2 TOP2 +1
Step 2: [Finished]
Return(DUMMY)
Procedure. DISPLAY1( ). This procedure display the elements on the screen. Here A is
the array variable, I is the loop-control variable and TOP1 points to
the element on the stack.
Step 1: [Displaying the elements on the screen.]
If (TOP1 = 0)Then Write(Stack is empty)
Else
Write(Elements of stack are:)
Repeat for I TOP1, , 1
Write (A[I])
Step 2: [Finished]
Return
Procedure. DISPLAY2( ). This procedure display the elements on the screen. Here B is
71
7/30/2019 data structure file
72/310
the array variable, I is the loop-control variable and TOP2 points to
the element on the stack.
Step 1: [Displaying the elements on the screen.]
If (TOP2 = 0)
Then Write(Stack is empty)
Else
Write(Elements of stack are:)
Repeat for I TOP1, , 20
Write (B[I])
Step 2: [Finished]
Return
72
7/30/2019 data structure file
73/310
Program Coding
#include#include
#include
void push1(int);
int pop1( );void display1( );
void push2(int);
int pop2( );
void display2( );
int a[20], top1, top2, b[20];
void main( )
{clrscr( );
int choice, ch, data;
char ans;top1= -1;top2= -1;
while(1)
{
clrscr( );
printf("\n\t Implementing stack using 2 different array);
printf("\n\t-------------------------------------------------");
printf("\n\t 1- Push an element into stack ");
printf("\n\t 2- Pop an element from stack ");
printf("\n\t 3- Display the stack ");
printf("\n\t 4- Exit from output window ");fflush(stdin);
printf("\n\n Enter your choice :- ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\n Enter where element to pushed ");
printf("\n For Stack1-press 1 &Stack2- press2 :");
fflush(stdin);
scanf("%d", &ch);
printf("\n\n Enter element pushed into stack");fflush(stdin);
scanf("%d", &data);
if(ch == 1)
push1(data);
else if(ch == 2)
push2(data);
else
printf("\n Invalid choice ........");
break;
73
7/30/2019 data structure file
74/310
case 2:
printf("\n Enter where element to be deleted ");
printf("\n For Stack1-press 1 &Stack2- press 2 :");
fflush(stdin);
scanf("%d", &ch);
if(ch == 1)
data= pop1( );
else if(ch == 2)
data= pop2( );
else
printf("\n Invalid choice ......");
if(data != -99)
printf("\n Element popped is :%d", data);
fflush(stdin);
printf("\n\n Press any key to continue ");
fflush(stdin);getch( );
break;
case 3:
printf("\n Enter which stack is to be viewed ");
printf("\n For Stack1-press 1 & Stack2-press 2 :");fflush(stdin);
scanf("%d", &ch);
if(ch == 1)display1( );
else if(ch == 2)
display2( );
else
printf("\n Invalid choice ........");
printf("\n\n Press any key to continue ");
fflush(stdin);
getch( );
break;
case 4:
printf("\n You have chosen to exit");printf("\n You want to exit ? If yes pres y);
fflush(stdin);
scanf("%c", &ans);
if(ans == 'y' || ans == 'Y')
exit(0);
else
74
7/30/2019 data structure file
75/310
break;
default:
printf("\n Invalid choice .....");
getch( );
break;
}
}
}
void push1(int x)
{
if(top1 == 19)printf("\n\n Stack1 overflow");
else
{
top1++;
a[top1] = x;
}
}
void push2(int x)
{
if(top2 == 19)
printf("\n\n Stack2 overflow");
else
{
top2++;
b[top2] = x;}
}
int pop1( )
{
int dummy;
if(top1 == -1)
{
printf("\n\n Stack1 underflow");
getch( );
return(-99);
}
else{
dummy= a[top1];
top1--;
return(dummy);
}
}
int pop2( )
75
7/30/2019 data structure file
76/310
{
int dummy;
if(top2 == -1)
{
printf("\n\n Stack2 underflow");
getch( );
return(-99);
}
else
{
dummy = b[top2];
top2--;return(dummy);
}
}
void display1( )
{
int i;
if(top1 == -1)printf("\n\n Stack1 is empty");
else
{
printf("\n\n Elements in stack1 are :- ");
for(i= top1; i>= 0; i--)printf("\n %d", a[i]);
}
getch( );
}void display2( )
{int i;
if(top2 == -1)
printf("\n\n Stack2 is empty");
else
{
printf("\n\n Elements in stack2 are :- ");
for(i= top2; i>= 0; i--)
printf("\n %d", b[i]);
}
getch( );
}
76
7/30/2019 data structure file
77/310
Q.17 Write the pseudo code and program for Implementing two priority queues within one array.
Ans.
PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show the menu operations on priority
queue and Calling of all functions. FRONT1 and FRONT2
points to the elements to the top of the queue, DATAcontains the element to be inserted or deleted, REAR1 and
REAR2 points to the element in the back. CHOICE and ANS
are variable for storing chosen option.
Step 1: [Display of menu]
FRONT1 0
REAR1 0
FRONT2 10
REAR2 10Repeat While(True)
Write(Implementing two priority queue using two arrays)
Write(1- Insert an element into queue)Write(2- Delete an element from the queue)
Write(3- Display the queue)
Write(4- Exit from output window)
Write(Enter your choice)
Read(CHOICE)
Step 2: [Calling the functions.]
Select Case(CHOICE)
Case 1: Write(Enter element to inserted into the queue)
Read(DATA)
Call INSERT(DATA)
Case 2: Call DEL( )
Case 3: Call DISPLAY( )
Case 4: Write(You want to exit? If yes press y)
Read(ANS)
If (ANS = y or ANS = Y)
Then EXIT
Default: Write( Invalid choice)
Step 3: [Finished]Return
Procedure. INSERT(X). This procedure input or push an element passed an as
parameter into the queue. Here A is the array variables, FRONT1 and
FRONT2 points to the element on the top of the queue, REAR1 and REAR2
points to the element in the last and X contain the element which is to be
inserted. ANS is to store selected choice.
Step 1: [Selection of Queue]
Write( Enter your priority. Press 1 or 2)
77
7/30/2019 data structure file
78/310
Read(ANS)
Step 2: [Inputs or insertion of element]
If (ANS = 1)
Then If (REAR1 = 10)
Then Write(Priority Queue 1 is full)
Else
REAR1 REAR1 +1
A[REAR1] X
If (FRONT1 = 0)Then FRONT1 FRONT +1
Else If (ANS = 2)
If (REAR2 = 20)Then Write(Priority Queue 2 is full)
Else
REAR2 REAR2+1
A[REAR2] X
If (FRONT2 = 10)
then FRONT2 FRONT2 +1
ElseWrite(Invalid choice)
Step 3: [Finished]
Return
Procedure. DEL( ). This procedure deletes input elements from the queue. Here A is
the array variables, FRONT1 and FRONT2 points to the
element on the top of the queue, DUMMY is an integervariable to store the value of the array temporarily and
REAR1 and REAR2 points to the elements in the last. ANS is
used to store selected choice.
Step 1: [Delete element from the queue.]
If (FRONT1 0)Then DUMMY A[FRONT1]
If (FRONT1 = REAR1)
FRONT1 = REAR1 = 0
Else
FRONT FRONT +1
Write(Element deleted from priority queue
1 is, DUMMY)
Else Write( Priority Queue 1 is empty)
Else
Write(Priority Queue 1 is empty)
If (FRONT2 10)
Then DUMMY B[FRONT2]If (FRONT2 = REAR2)
Then FRONT2 = REAR2 = 10
Else
FRONT2 FRONT2+1
Write(Element deleted from priority queue
2 is ,DUMMY)
Else
Write(Priority Queue 2 is empty)
78
7/30/2019 data structure file
79/310
Step 3: [Finished]
Return
Procedure. DISPLAY( ). This procedure display elements of queue on the screen. Here
A and B are the array variables, I is the loop-control variable
and FRONT1 and FRONT2 points to the elements on the top
of the queue and REAR1 and REAR2 points to the element in
the last.
Step 1: [Display the elements on the screen.]
If (FRONT1 0)Then Write(Elements of Priority queue 1 are:)
Repeat for I FRONT1, , REAR1
Write(A[I])
ElseWrite(Priority queue 1 is empty)
If (FRONT2 10)
Then Write(Elements of Priority queue 2 are:)
Repeat for I FRONT2, ,REAR2
Write (A[I])
Else
Write(Priority queue2 is empty)Step 2: [Finished]
Return
79
7/30/2019 data structure file
80/310
Program Coding
#include#include
#include
void insert(int);
void del( );void display( );
int a[20], i, rear1, front1, front2, rear2;
void main( )
{
clrscr( );
int ch, data;
char ans;front1= rear1= -1;
front2= rear2= 9;
while(1){
clrscr( );
printf("\n\t Implementing two priority queue using 2 arrays");
printf("\n\t--------------------------------------------------");
printf("\n\t 1- Insert an element into queue ");
printf("\n\t 2- Delete an element from the queue ");
printf("\n\t 3- Display the queue ");
printf("\n\t 4- Exit from output window ");
fflush(stdin);
printf("\n\n Enter your choice :- ");
scanf("%d", &ch);
switch(ch)
{case 1:
printf("\n Enter element inserted into queue");
fflush(stdin);
scanf("%d", &data);
insert(data);
break;
case 2:
del( );break;
case 3:
display( );
printf("\n Press any key to continue ");
fflush(stdin);
getch( );
break;
80
7/30/2019 data structure file
81/310
case 4:
printf("\n You have chosen to exit");
printf("\n You want to exit? If yes press y");
fflush(stdin);
scanf("%c", &ans);
if(ans == 'y' || ans == 'Y')
exit(0);
else
break;
default:printf("\n Invalid choice .....");
getch( );
break;
}
}
}
void insert(int x){
int ans;
printf("\n Enter your priority . Press 1 or 2");
scanf("%d", &ans);
if(ans == 1){
if(rear1==9)
printf("\n Priority Queue number 1 is full!");
else
{rear1++;
a[rear1] = x;
if (front1 == -1)
front1++;
}
}
else if(ans == 2)
{
if(rear2 == 19)
printf("\n Priority queue 2 is full");
else
{rear2++;
a[rear2] = x;
if(front2 == 9)
front2++;
}
}
else
printf("\n Invalid choice");
81
7/30/2019 data structure file
82/310
getch( );
}
void del( )
{
int dummy, ans;
if(front1 != -1){
dummy = a[front1];
if(front1 = rear1)
front1= rear1= -1;
else
front1++;
printf("\n Element to be deleted from queue 1 is %d",dummy);
}
else
{
printf("\n Priority queue 1 is empty");
if(front2 != 9)
{
dummy= a[front2];
if(front2 == rear2)
front2 = rear2 = 9;
else
front2++;
printf("\n Element deleted from queue 2 is %d", dummy);
}else
printf("\n Priority queue2 is empty");
}
}
void display( )
{
if(front1 != -1)
{
printf("\n Elements in queue number 1 are:");
for(i= front1; i
7/30/2019 data structure file
83/310
for(i= front2; i
7/30/2019 data structure file
84/310
Q. 18 Write the pseudo code and program for Implementing stack and simple queue within one array.
Ans.
PSEUDO CODE
Algorithm. MAIN( ). This algorithm is used to show the menu operations on the stack
and simple queue and Calling of all functions. TOP points to
the element on the top of the stack, FRONT points toelement on the top of the queue, REAR points to the element
present in the last of the queue, DATA contains the element
to be inserted or deleted or pushed or popped. CHOICE and
ANS are the integer and character variables for storing
chosen option. CH1 and CH2 are integer variable and used for
chosen options.
Step 1: [Displaying the menu.]
TOP 0FRONT 10
REAR 10
Repeat While(True)Write(Implementing stack & simple queue within an array.)
Write(1- Operations on stack)
Write(2- Operations on simple queue)
Write(3- Exit from window)
Write( Enter your choice)
Step 2: [Calling the functions.]
Select Case(CHOICE)
Case 1: Write( Implementing stack using array)
Write(1- Push an element from the stack)
Write(2- Pop an element from the stack)Write(3- Display the stack)
Write(4- Exit from output window)
Write(Enter your choice)Read(CH1)
Select Case(CH1)
Case 1: Write(Enter element pushed into stack)
Read(DATA)
Call PUSH(DATA)
Case 2: DATA Call POP( )
If (DATA -99)
Then Write(Element popped is :DATA)
Case 3: Call DISPLAY( )
Case 4:
Write(You want to exit? If yes then press y)
Read(ANS)
If (ANS = y or ANS = Y)
Then EXIT
84
7/30/2019 data structure file
85/310
Default: Write(Invalid choice)
Case 2: Write( Implementing Simple queue using array)
Write(1- Insert an element from the queue)
Write(2- Delete an element from the queue)
Write(3- Display the queue)
Write(4- Exit from output window)
Write(Enter your choice)Read(CH2)
Select Case(CH2)
Case 1:
Write( Enter element inserted into stack)Read(DATA)
Call INSERT(DATA)
Case 2:
DATA Call DEL( )
If (DATA -99)
Then Write(Element deleted is:, DATA)
Case 3: Call DISPLAY( )
Case 4: Write (You want to exit? If yes press y)
Read(ANS)
If (ANS = y or ANS = Y)
Then EXIT
Default: Write( Invalid choice)
Case 3: Write (You want to exit? If yes press y)
Read(ANS)If (ANS = y or ANS = Y)
Then EXIT
Default: Write(Invalid Choice)
Step 3: [Finished]
Exit
Procedure. PUSH(X). This procedure input or push an element passed on as parameter into the stack. Here A
is the array variable, TOP points to the element on the top of the stack and Xcontain the element which user have entered and want to perform operation on it.
Step 1: [Input or pushing the element]
If (TOP = 10)
Then Write(Stack overflow)
Else
TOP TOP+1
A[TOP] X
85
7/30/2019 data structure file
86/310
Step 2: [Finished]
Return
Function. POP( ). This procedure pop or delete the input elements from the top of the stack. Here A represents
the array variable, TOP points to the element on the top of the stack and DUMMY is an
integer variable to store element temporarily.
Step 1: [Sorting of elements in the array.]
If (TOP = 0)
Then Write(Stack underflow)Return(-99)
Else
DUMMY A[TOP]
TOP TOP+1
Step 2: [Finished]
Return(DUMMY)
Procedure. DISPLAY1( ). This procedure display the elements on the screen. Here A is the
array variable, I is the loop-control variable and TOP points to
the element on the stack.
Step 1: [Displaying the elements on the screen.]
If (TOP = 0)
Then Write(Stack is empty)
ElseWrite(Elements of stack are:)
Repeat for I TOP, , 1Write (A[I])
Step 2: [Finished]
Return
Procedure. INSERT( X). This procedure input or pushing an element passed as parameter into the queue. Here
A is the element in the last and X is the variable containing the element.
Step 1: [Input or insertion of element]
If (REAR = 20)
Then Write( Simple queue is full)Else
REAR REAR +1
A[REAR] X
If (FRONT = 10)
Then FRONT FRONT +1
Step 2: [Finished]
86
7/30/2019 data structure file
87/310
Return
Function. DEL( ). This function delete the input elements from the queue. Here A
represents the array variable, FRONT points to the element on the top of
the queue, DUMMY is an integer variable to store element temporarily and
REAR points to the elements in the last.
Step 1: [Deletion of elements from the queue]
If (FRONT = 10)
Then Write( Simple Queue is empty)Return (-99)
Else
DUMMY A[FRONT]
If (FRONT = REAR)
Then FRONT FRONT+1
Step 2: [Finished]
Return(DUMMY)
Procedure. DISPLAY2( ). This procedure display the elements on the screen. Here A isthe array variable, I is the loop-control variable and
FRO