内容正文:
栈和队列选择题综合
班级 姓名
1.【202304杭二模】有1个栈,从栈顶到栈底依次为元素a、b、c,并且已知元素d已入栈并出栈,则这四个元素的入栈顺序可能为( )
A.a,b,c,d B.b,d,c,a C.c,d,b,a D.d,a,b,c
2.【202304杭二模】有如下 Python 程序段:
import random
q=[0]*5 ; head=tail=0
for i in range(5):
if random.randint(0,1)== 0:
q[tail]=random.randint(1,9); tail+=1
elif head != tail and q[tail - 1] < q[head]:
q[tail]=q[head]; head+=1 ; tail+=1
执行该程序段后,q 的值不可能是( )
A.[0, 0, 0, 0, 0] B.[5, 4, 3, 2, 1] C.[5, 8, 3, 0, 0] D.[0, 5, 6, 0, 0]
3.【202304金丽衢】约定:T操作是指在队列中1个元素出队后再入队,E操作是指将1个元素入队,P操作是指队列中1个元素出队,队首指针head和队尾指针tail初始值均为 0。则经过EETPETE系列操作后,队首指针head和队尾指针tail的值分别为( )
A.3 4 B.3 5 C.4 5 D.4 6
4.【202304金丽衢】有如下 Python 程序段:
import random
s=[3,2,7,6,9] ; st=[0]*len(s) ; top=-1 ; i=0
while i<len(s):
op=random.randint(0,1)
if top==-1 or op==0 and s[i]>st[top]:
top+=1 ; st[top]=s[i]
elif top>=1 and op==1 and s[i]>st[top-1]:
st[top]=s[i]
i+=1
while top!=-1:
print(st[top] ,end=" ")
top-=1
执行该程序段后,输出的结果不可能是( )
A.3 B.9 6 2 C.9 6 3 D.9 7 3
5.【202304嘉兴二模】有如下Python程序段:
lst=[5,9,2,6,4,7,3,0]
que=[0]*len(lst) ; head=tail=0 ; i=0
while i<len(lst):
if lst[i]%2==0:
que[tail]=lst.pop(i); tail+=1 #lst.pop(i) 删除列表lst中索引为i的元素,返回删除的元素
else:
i+=1
while head!=tail:
lst.append(que[head])
head+=1
执行该程序段后,lst的值为( )
A.[5,9,7,3,2,6,4,0] B.[5,9,7,3,0,4,6,2]
C.[2,6,4,0,5,9,7,3] D.[3,7,9,5,0,4,6,2]
6.【202304嘉兴二模】有如下Python程序段:
def sym(d1, d2):
s1=d1.split(",")
s2=d2.split(",") #以“,”将字符串分割成列表
if len(s1)!=len(s2):
return False
stk=[] ; i=0 ; j=0
while i<len(s1):
stk.append(s1[i]) ; i+=1
while stk!=[] and stk[-1]==s2[j]:
stk.pop() ; j+=1 #stk.pop()删除列表stk中的最后一个元素
return s