0% found this document useful (0 votes)
83 views3 pages

Contoh Jawapan Soalan 5 Dan 6 Stack Examination Dec 2019

The document describes a question about stacks and postfix/prefix notation. It asks the reader to: 1) Draw a diagram representing the contents of stack stk1 after pushing values 100-109 onto it. 2) Write code to print the contents of stack stk2 in reverse order after pushing values 'a'-'f' onto it. 3) Convert two expressions to postfix notation and two to prefix notation. 4) Solve a mathematical expression by first converting it to postfix notation and showing the steps using a stack.

Uploaded by

Najihah Hassan
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)
83 views3 pages

Contoh Jawapan Soalan 5 Dan 6 Stack Examination Dec 2019

The document describes a question about stacks and postfix/prefix notation. It asks the reader to: 1) Draw a diagram representing the contents of stack stk1 after pushing values 100-109 onto it. 2) Write code to print the contents of stack stk2 in reverse order after pushing values 'a'-'f' onto it. 3) Convert two expressions to postfix notation and two to prefix notation. 4) Solve a mathematical expression by first converting it to postfix notation and showing the steps using a stack.

Uploaded by

Najihah Hassan
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/ 3

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;
}

//Line XX (answer question a)- draw stk1

stk2.push(‘a’); stk2.push(‘b’); stk2.push(‘c’);


stk2.push(‘d’); stk2.push(‘e’); stk2.push(‘f’);

//Line YY. Add code statements here.


System.out.println (stk2.toString());
}
a) Draw diagram of stk1 to represent the content in these data structures at Line XX.
Label the data structures properly.
(4 marks)

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

a) Convert the following expression into postfix notation.


i) A+B–C*D
Answer :-

ii) E*(F–G*H)
Answer :-

(3 marks)

b) Convert the following expression into prefix notation.


i) J+K*L–M
Answer :-

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)

You might also like