Lab Exercise (1)
Lab Exercise (1)
QUESTION 2
i) Describe how stack is different than other data structures and give TWO examples of
stack application in computer system.
(5 marks)
ii) RS Entertainment has launched a new online music player for Android users as
illustrated in Figure 1. The application will automatically add any trending songs in
Malaysia to the playlist so that it can be listened by users. The Stack concept was
applied to the application's playlist.
RS Music Player / /x
UiTM DiHatiku
UiTM Choir
O M - o
0®
_ _ . 227 4» -a
(£) G4. UiTM DiHatiku - UiTM Choir 2.35
Figure 1
The following class Song was used for storing information about songs that contains
data such as song's title, singer's name, and length of the song.
Assume that all data have been inserted into a s t a c k object named p l a y L i s t .
}
public class Stack
{
public Stack () { ... }
public void push (Song ...) { ... }
public Song pop () { ... }
public Song peek() { ... }
public boolean isEmptyO { ... }
J
Write a program segment to compute and display the total length of all songs
performed by the uiTM c h o i r . (Note: use a temporary stack to store the information
to avoid data loss.)
(5 marks)
iii) Write a program segment to display information of all songs where the title starts with
T and length of the song is greater than 3 minutes. (Note: use a temporary stack to
store the information to avoid data loss.)
(5 marks)
A - P + A / T + H * ( E - T $ I + C)
Convert the given expression into the following notation along with the steps.
i) Prefix
(5 marks)
ii) Postfix
(5 marks)
c) Given the following infix expression:
Z-(E$A*L + 0)*U/S