Contoh Jawapan Soalan 5 Dan 6 Stack Examination Dec 2019
Contoh Jawapan Soalan 5 Dan 6 Stack Examination Dec 2019
Question 5
A Stack in one of the basic data structures where insertion and deletion of items takes place at
one end called top of the stack. Assume the following code has no syntax error and can be
executed correctly.
class Stack{
public Stack() {…} //default constructor
public push(Object obj) {…} //insert new value to the Stack
public Object pop() {…} //remove value from the Stack
public boolean isEmpty() {…}
public String toString() {…}
}
public class testStack {
public static void main (String [] args) {
Stack stk1 = new Stack ();
Stack stk2 = new Stack ();
Stack temp = new Stack ();
int count = 0;
for (int n=100; n<110; n++) {
stk1.push(n + count);
count = count + 1;
if (count >= 3)
count = 1;
}
1
b) Write the missing code statetments at Line YY. The output of the program from the
statement System.out.println (stk2.toString()); has to be:
[ f, e, d, c, b, a ]
(4 marks)
Question 6
ii) E*(F–G*H)
Answer :-
(3 marks)
ii) P*Q/R$T
Answer :-
(3 marks)
2
c) Given the following mathematical expression, solve the equation and show all the
steps by using stack configuration. You must convert the expression into postfix
notation first
(1 + 2 ) * 4 / 3
(4 marks)