1、
Push 5 characters ooops
onto a stack. In how many different ways that we can pop these characters and still obtain ooops
?
A.1
B.3
C.5
D.6
stack是一种FILO ( First In Last Out ) 结构,故p和s两个字母必须在入栈后立即出栈,故不同的情况仅发生在前三个o上。
分析可知必须是3个in,3个out,且in的数量不得少于out,故有:
① in in in out out out
② in in out out in out
③ in in out in out out
④ in out in in out out
⑤ in out in out in out
共五种情况。
2、
Represent a queue by a singly linked list. Given the current status of the linked list as 1->2->3
where x->y
means y
is linked after x
. Now if 4
is enqueued and then a dequeue is done, the resulting status must be:
A.1->2->3
B.2->3->4
C.4->1->2
D.the solution is not unique
queue是FIFO ( First In First Out ) 结构,故enqueue了4,dequeue了1,只剩下2->3->4
3、
Suppose that an array of size 6 is used to store a circular queue, and the values of front
and rear
are 0 and 4, respectively. Now after 2 dequeues and 2 enqueues, what will the values of front
and rear
be?
A.2 and 0
B.2 and 2
C.2 and 4
D.2 and 6
两次入队两次出队,入队一次rear+1,出队一次front+1
0 + 2 = 2;
4 + 2 = 6,6 % 6 = 0;
故front和rear最终值分别为2,0
4、
Suppose that all the integer operands are stored in the stack S1, and all the operators in the other stack S2. The function F()
does the following operations sequentially:
- (1) Pop two operands
a
andb
from S1; - (2) Pop one operator
op
from S2; - (3) Calculate
b op a
; and - (4) Push the result back to S1.
Now given { 5, 8, 3, 2 } in S1 (where 2 is at the top), and { *
, -
, +
} in S2 (where +
is at the top). What is remained at the top of S1 after F()
is executed 3 times?
A.-15
B.15
C.-20
D.20
根据题述步骤:
① 从S1中pop两个数:2,3
② 从S2中pop一个运算符:+
③ 计算 3 + 2 = 5
④ push 5 到S1中,S1 = {5,5,8}
……
以此类推,5 - 8 = -3,5 * (-3) = -15,故选A