0% found this document useful (0 votes)
20 views13 pages

2 Data Structure Questions

Questions on Data structure on C++
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views13 pages

2 Data Structure Questions

Questions on Data structure on C++
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Data Structure

2/3 Marks Questions


1. Write the definition of a function Alter(int A[], int N) in C++,
whichshould change all the multiples of 5 in the array to 5 and rest of
the elements as 0. For example, if an array of 10 integers is as
follows:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
55 43 20 16 39 90 83 40 48 25
After executing the function, the array content should be changed
as follow:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
5 0 5 0 0 5 0 5 0 5

2. Write the definition of a function Alter(int A[], int N) in C++,


whichshould change all the odd numbers in the array to 1 and even numbers
as 0. For example, if an array of 10 integers is as
follows:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
55 43 20 16 39 90 83 40 48 25
After executing the function, the array content should be changed
as follow:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
1 1 0 0 1 0 1 0 0 1

3. Write code for a function void oddEven (ints[],int N) in C++, to add 5


inall the odd values and 10 in all the even values of the array S.
e.g. If the original content of the array S is: 50 11 19 24 28.
The modified content will be :60 l6 24 34 38

4. Write a function in C++ TWOTOONE() which accepts two array X[ ], Y[ ]


and their size n as argument. Both the arrays X[ ] and Y[ ] have the same
number of elements. Transfer the content from two arrays X[ ], Y[ ] to
array Z[ ]. The even places (0,2,4...) of array Z[ ] should get the
contents from the array X[ ] and odd places (1,3,5...) of array Z[ ] should
get the contents from the array Y[ ].
Example : If the X[ ] array contains 30,60,90 and the Y[ ] array contains 10,20,50. Then Z[ ] should contain
30,10,60,20,90,50.

5. Write a code in C++ for a function void Convert ( int T[], int N) , which repositions all the elements of array
by shifting each of them one to one position before and by shifting the fast element to last.
e.g. if the content of array is
0 1 2 3
22 25 14 30
The changed array content will be:
0 1 2 3
25 14 30 22
6. Write a for a function void ChangeOver ( int P[], int N) in C++ , which repositions all the elements of array by
shifting each of them to next position and shifting last element to first position.
e.g. if the content of array is
0 1 2 3
10 14 11 21
The changed array content will be:
0 1 2 3
21 10 14 11

7.
Write a function SWAP2BEST (intARR[], intSize) in C++ to
modify the content ofthe array in such a way that the elements,

which are multiples of10swap with the value present inthe very
next positionin the array.
For example :
ifthe content ofarray ARRis
90, 56, 45, 20, 34, 54
The contentofarray ARRshould become
56, 90, 45, 34, 20, 54
8. Write a Get1From2( ) function in C++ to transfer the content from
twoarrays FIRST[ ] and SECOND[ ] to array ALL[ ]. The even places(0,2,
4,...) of array ALL[ ] should get the content from the array FIRST[ ] and
oddplaces (1, 3, 5, ) of the array ALL[] should get the content from the
arraySECOND[ ].
Example:
If the FIRST[ ] array contains
30, 60, 90
And the SECOND[ ] array contains
10, 50, 80
The ALL[ ] array should contain
30, 10, 60, 50, 90, 80

9. Write a Get2From1( ) function in C++ to transfer the content from one


arrays ALL[ ] to two arrays Odd[ ]and Even[]. The Even should contain
values from places (0,2,4,………) of ALL[] and Odd[] should contain values
from places ( 1,3,5,……….).
Example:
If The ALL[ ] array contain
30, 10, 60, 50, 90, 80
Then the Even[ ] array should contains
30, 60, 90
And the ODD[ ] array should contains
10, 50, 80
10. Write a function CHANGEO in C++,which accepts an array of integer and
itssize as parameters and divide all thosearray elements by 7 which are
divisible by7 and multiply other-array elements by 3.
Sample Input Data of the array

Content of the array after CallingCHANGE( ) function

11. Write a function REASSIGNO in C++, which accepts an array of integers and its size as parameters
and divide all those array elements by 5 which are divisible by 5 and multiply other array elements by 2.
Sample Input Data of the array

Content of the array after calling REASSIGNO function

12. Write a function in C++, which accepts an integer array and its size
as arguments and swap the elements of every even location with its
following odd location.
Example:
If an array of nine elements initially
Contains the elements as
2, 4, 1, 6, 5, 7, 9, 23, 10
Then the function should rearrange the
Array as 4, 2,6,1,7,5,23,9,10

13. Write a function in C++, which accepts an integer array and its size
as parameters and rearranges the array in reverse.
Example:
If an array of nine elements initially
Contains the elements as 4, 2, 5, 1, 6, 7, 8, 12, 10
Then the function should rearrange the array as
10, 12, 8, 7, 6, 1, 5, 2, 4

14. Write function in C++ which accepts an integer array and size as
arguments and replaces elements having odd values with thrice its value and
elements having even values with twice its value.
Example : if an array of five elements
initially contains elements as 3, 4, 5, 16, 9
The function should rearrange the content of the array as 9, 8, 75, 32,27
15. Write a function in C++ which accepts an integer array and its size as
arguments and exchanges the values of first half side elements with the
second half side elements of the array.
Example:
If an array of 8 elements initial content as 2, 4, 1, 6, 7, 9, 23, and 10
The function should rearrange array as 7, 9, 23, 10, 2, 4, 1 and 6

16. Write a Function to Search for an element from Array A by Linear Search.
17. Write a Function to Search for an element from Array A by Binary Search.
18. Write a function to Sort the array A by Bubble Sort.
19. Write a function to Sort the array A by Selection Sort.
20. Write a function to Sort the array A by Insertion Sort.

21. What will be the status of the following list after fourth pass of bubble sort and fourth pass of
selection sort used for arranging the following elements in descending order?
14, 10, -12, 9, 15, 35

22. A two dimensional array P [20] [50] is stored in the memory along the
row with each of its element occupying 4 bytes, find the address of the
element P [10] [30], if the element P[5] [5] is stored at the memory
location 15000.

23. A two dimensional array ARR [50][20] is stored in the memory along the
row with each of its elements occupying 4 bytes. Find the address of the
element ARR[30][10], if the element ARR[10] [5] is stored at the memory
location 15000.

24. An array T [25] [20] is stored along the row in the memory with each
element requiring 2 bytes of storage. If the base address of array T is
42000, find out the location of T [l0] [15]. Also, find the total number of
elements present in this array.

25.An array A[20][30] is stored along the row in the memory with each
element requiring 4 bytes of storage. If the base address of array A is
32000, find out the location of A[15][10]. Also, find the total number of
elements present in this array.

26. Given an array A[10][12] whose base address is 10000. Calculate the
memory location of A[2][5] if each element occupies 4 bytes and array is
stored column-wise.
27. An array P[15][10] is stored along the column in the memory with
each elementrequiring 4 bytes of storage. If the base address of array P
is 14000, find out thelocation of P[8][5].
28. An array T[15][10] is stored along the row in the memory with each
element requiring 8 bytes of storage. Ifthe base address of array T is
14000, find out the location of T[10][7].
29. An array T[20][10] is stored in the memory along the column with each
of the element occupying 2 bytes, findout the memory location of T[10][5],
if an element T[2][9] is stored at location 7600.

30. An array P[20] [50] is stored in the memory along the column with each
of itselement occupying 4 bytes, find out the 1ocation of P[15][10], if
P[0][0] isstored at 5200.

31. An array G[50] [20] is stored in the memory along the row with each of
itselement occupying 8 bytes, find out the 1ocation of G[10][15], if P[0]
[0] is stored at 4200.

32. An array P[50] [60] is stored in thememory along the column with each
of theelement occupying 2 bytes, find out thememory location for the
element P[10][20],if the Base Address of the array is 6800.

33. An array T[90][100] is stored in thememory along the column with each
of theelements occupying 4 bytes. Find out thememory location for the
element T[10][40],if the Base Address of the array is 7200.

34. An array S[40][30] is stored in thememory along the column with each
of theelement occupying 4 bytes, find out thebase address and address of
element S[20][15], if an element S[15][10] is stored at the
memory location 7200.

35. An array Arr[50][10] is store in thememory along the row with each
elementoccupying 2 bytes. Find out the Baseaddress of the location Arr[20]
[50], if thelocation Arr[10][25] is stored at the address
10000.

36. An array VAL[1…15][1…10] is storedin the memory with each element


requiring4 bytes of storage. If the base address ofthe array VAL is 1500,
determine thelocation of VAL[12][9] when the array VALis stored Row wise.

37. An array VAL[1…15][1…10] is storedin the memory with each element


requiring4 bytes of storage. If the base address ofthe array VAL is 1500,
determine thelocation of VAL[12][9] when the array VALis stored Column
wise.

38. An array S[40][30] is stored in the memoryalong the row with each of
the elementoccupying 2 bytes, find out the memory locationfor the element
S[20][10], if an element S[15][5] is stored at the memory location 5500.
39. Write a function REVCOL (intP[][5], int N, int M) in C++to display the
content of a two dimensional array, with each column content in reverse
order.
Note: Array may contain any number of rows.
For example, if the content of array is as follows:
15 12 56 45 51
13 91 92 87 63
11 23 61 46 81
The function should display output as:
11 23 61 46 81
13 91 92 87 63
15 12 56 45 51

40. Write a function REVROW(int P[][5],int N, int M) in C++ todisplay the


content of a two dimensional array, with each rowcontent in reverse order.
For example, if the content of array is as follows:
15 12 56 45 51
13 91 92 87 63
11 23 61 46 81
The function should display output as:
51 45 56 12 15
63 87 92 91 13
81 46 61 23 81

41. Write a user-defined function SumLast3(int A[][4], int N, int M) in


C++ to find and display the sum of all the values, which are ending with 3
(i.e. Unit place is 3).For example if the content of array is:
33 13 92
99 3 12
The output should be 49
42. Write a user-defined function AddEnd2(int A[][4],intN,int M) in C++ to
find and display the sum of all the values, which are ending with 2 (i.e.,
units place is 2).
For example if the content of array is:
12 16 32
19 5 2
The output should be 46
43. Write a function in C++ which accepts a 2D array of integers and its size arguments
and displays the elements which lie on minor diagonal. [Top right to bottom left diagonal]
[Assuming the 2D array to be square matrix with odd dimension i.e. 3 x 3, 5 x 5, 7, x 7,
etc ...]
For example
If the 2D array is
678
136
793
The following should be displayed :
8
3
7
44. Write a user defined function display (intA[][4], intN,int M) in C++ to find and display
all numbers, which are divisible by 10.
e.g. if the content of array is : 45 50 60
10 3 15
Then output should be 50 60 10

45. Write a function ALTERNATE (intA[][3],intN,int M) in C++ to display


all alternate elementfrom two-dimensional array A (starting from A[0][0]).
e.g. if the Array is containing:
12 13 14
15 59 53
35 36 34
The output will be:
12 14 59 35 34

46. Write a COLSUM( ) function in C++ to find sum of each column of a NxM
Matrix.

47. Write a ROWSUM( ) function in C++ to find sum of each row of a rxc
Matrix.

48. Write a DSUM function in C++ to find the sum of diagonal element of
an*n matrix.

49. Write a user defined function in C++ to display the multiplication of


row elements of two dimensional array A[4][6] containing integers.

50. Write a user defined function in C++ to display the sum of row
elements of two dimensional array A[5][6]containing integers.

51. Write a function in C++ to print the product of each column of a two
dimensional integer array passed as theargument of the function.
Explain: if the two dimensional array contains
1 2 4
3 5 6
4 3 2
2 1 5
Then the output should appear as:
Product of Column 1 = 24
Product of Column 2 = 30
Product of Column 3 = 240
52. Write a function in C++ which accepts a 2D array of integers and its
size as arguments and display the elementswhich lie on diagonals.
[Assuming the 2D Array to be a square matrix with odd dimension i.e., 3 x
3, 5 x 5, 7 x 7 etc….]
Example, if the array content is
5 4 3
6 7 8
1 2 9
Output through the function should be:
Diagonal One: 5 7 9
Diagonal Two: 3 7 1

53. Write a function in C++ which accepts a 2D array of integers and its
size as arguments and display the elementsof middle row and the elements of
middle column.
[Assuming the 2D Array to be a square matrix with odd dimension i.e., 3 x
3, 5 x 5, 7 x 7 etc….]
Example, if the array content is
3 5 4
7 6 9
2 1 8
Output through the function should be:
Middle Row: 7 6 9
Middle Column: 5 6 1

54. Write a function int ALTERSUM (int B[ ][5], int N, int M in C++ to
find and returnthe sum of elements from all alternateelements of a two-
dimensional arraystarting from B[0][0].

55. Convert the following infix expression to its equivalent postfix


expression, showing the stack contents for each step ofconversion.
X / Y + U* (VW)

56.Convert the following infix expression to its equivalent Postfix


expression, showing the stack contents for each step ofconversion.
U * V + R/ (ST)

57. Translate, following infix expression into its equivalent postfix


expression:((A-B)*(D/E))/(F*G*H)

58. Translate, following infix expression into its equivalent postfix


expression:A*(B+D)/E-F-(G+H/K)

59. Write the equivalent infix expression for10,3,*,7,1,-,*,23,+

60. Write the equivalent infix expression for a, b, AND, a, c, AND, OR.

61. Evaluate the infix expression.


P: 12 , 7 , 3 , - , / , 2 , 1 , 5 , + , * , + , )

62. Give postfix form of the following expression A*(B+(C+D)*(E+F)/G)*H


63. Give postfix form expression for: NOT A OR NOT B AND NOT C

64. Consider the infix expressionQ : A+B * C ↑ (D/E)/F.


Translate Q into P, where P is the postfix equivalent expression of Q. what
will be the result of Q if this expressionis evaluated for A, B, C, D, E, F
as 2, 3, 2, 7, 2, 2 respectively.

65. Change the following infix expression into postfix expression.


(A+B) *C+D/E-F
66. Evaluate the following postfix expression. Show the status of stack
after execution of each operationseparately;
F, T, NOT, AND, F, OR, T, AND
67. Evaluate the following postfix expression. Show the status of stack
after execution of each operationseparately;
T, F, NOT, AND, T, OR, F, AND
68. Evaluate the following postfix expression. Show the status of stack
after execution of each operation:
5, 2, *, 50, 5, /, 5, –, +
69. Evaluate the following postfix expression. Show the status if stack
after execution of each operation;
60, 6, /, 5, 2, *, 5, –, +
70. Evaluate the following postfix expression using a stack and show the
contents of stack after execution of eachoperation;
5, 3, 2, *, 4, 2, /, –, *
71. Evaluate the following postfix notation. Show status of stack after
every step of evaluation (i.e. after eachoperator):
False, NOT, True, AND, True, False, OR, AND
72. Evaluate the following postfix notation, show status of stack of
every step of evaluation (i.e. after eachOperator):
True, False, NOT, AND, False, True, OR, AND
73. Evaluate the postfix notation of expression:
50, 60, +, 20, 10, –, *
74. Evaluate the following postfix notation of expression:
True, False, NOT, AND, True, True, AND, OR
75. Evaluate the following postfix notation of expression:
False, True, NOT, OR True, False, AND, OR
76. Evaluate the following postfix notation of expression. Show the
status of stack after each expression:
True, False, NOT, OR, False, True, OR, AND
77. Convert the following infix expression to its equivalent postfix
expression. Showing stack contents for theconversion:
(X – Y / (Z + U)* V)
4 Marks Questions

78. Write the definition of a member function Pop() in C++, to delete a book from a
dynamic stack of TEXTBOOKS considering the following code is already included in the
program.
struct TEXTBOOKS
{
char ISBN[20]; char TITLE[80];
TEXTBOOKS *Link;

class STACK
{
TEXTBOOKS *Top;
public:
STACK() {Top=NULL;}
void Push();
void Pop();
~STACK();

79. Write the definition of a member function PUSH() in C++, to add a new book in a
dynamic stack of BOOKS considering the following code is already included in the
program:
struct BOOKS
{
char ISBN[20], TITLE[80];
BOOKS *Link;

class STACK
{
BOOKS *Top;
public:
STACK()
{Top=NULL;}
void PUSH();
void POP();
~STACK();

80. Write a complete program in c++ to implement a dynamically allocated


Stack containing name of Countries.

81. Write a complete program in C++ toimplement dynamically allocated


Queue containing names of Cities.

82. Write a function QUEINS( ) in C++ to insertan element in a


dynamicallyallocated Queue containing nodes of thefollowing given
structure:
struct Node
{intPId ; //Product Id
charPname [20] ;
NODE *Next ;
} ;
83. Write a function QUEDEL( ) in C++ to display and delete an element
from a dynamically allocated Queue containing nodes of the following given
structure:
struct NODE
{
intItemno;
charItemname[20];
NODE *Link;
} ;

84. Write a function in C++ to delete a nodecontaining Book’s information,


from adynamically allocated Stack of Booksimplemented with the help of the
followingstructure.
struct Book
{
intBNo ;
charBName[20] ;
Book *Next ;
} ;

85. Write a function in C++ to perform Insertoperation in dynamically


allocated Queuecontaining names of students.
Struct NODE
{ char Name[20];
NODE *Link;
};

86. Write a function in C++ to perform aPUSH operation in a dynamically


allocatedstack considering the following :
struct Node
{
int X,Y ;
Node *Link ;
} ;
class STACK
{
Node *Top ;
public :
STACK( )
{Top = Null ;}
void PUSH( ) ;
void POP( ) ;
~STACK( ) ;
} ;
87. Write a function QINSERT () in C++ to perform insert operation on a
Linked Queue which contains client no and client name. Consider the
following definition of NODE in the code of QINSERT()
struct Node
{ longintCno; //ClientNo
charCname[20]; //CliehtName
NODE*Next;

};

88. Write a function PUSHBOOK() in C++ to perform insert operation on a


Dynamic Stack, which contains Book_no and Book_Title. Consider the
following definition of NODE, while writing your C++ code.
struct NODE
{ intBook_No;
charBook_Title[20];
NODE *Next;
};

89. Write a function POPBOOK() in C++ to perform delete operation on a


Dynamic Stack, which contains Book_no and Book_Title. Consider the
following definition of NODE, while writing your C++ code.
struct NODE
{ intBook_No;
charBook_Title[20];
NODE *Link;
};

90. Write a function in C++ to perform a DELETE operation in a dynamically


allocated queue considering the following description:
struct Node
{ float U,V;
Node *Link;
};
class QUEUE
{ Node *Rear, *Front;
public:
QUEUE( ) { Rear =NULL; Front= NULL;}
void INSERT ( );
void DELETE ( );
~QUEUE ( );
};
91. Define stackpop( ) to delete nodes, for a linked list implemented
stack having the following structure for each node:
struct Node
{ char name[20];
int age;
Node *Link;
};
class STACK
{ Node * Top;
public:
STACK( ) { TOP=NULL;}
voidstackpush( );
voidstackpop( );
~STACK( );
};
92. Write an algorithm to insert an element from a linked queue depending
upon user’s choice.

93. Write an algorithm to delete an element from a linked queue depending


upon user’s choice.

94. Write an algorithm that either pushes or pops an element in a linked


stack, depending upon user’s choice.

95. Write an algorithm to insert an element in a circular queue


implemented as an array.

96. Write an algorithm to deleting an element from a circular queue


implemented as an array.

You might also like