FDS作业记录 · 栈和队列

文章探讨了栈和队列的基本概念,包括栈的FIFO和FILO特性,以及如何通过数组实现循环队列。还涉及了算术表达式的执行过程,通过递归操作展示了如何在给定的栈和运算符栈中计算结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 and b 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值