在Python中如何实现栈和队列?请举例说明。
class Stack(object):
def __init__(self):
self.stack=[]
def isEmpty(self):
return self.stack==[]
def push(self,item):
self.stack.append(item)
def pop(self):
if self.isEmpty():
raise IndexError,'pop from empty stack'
return self.stack.pop()
def peek(self):
return self.stack[-1]
def size(self):
return len(self.stack)
class Queue:
def __init__(self, head=None):
self.storage = [head]
def enqueue(self, new_element):
self.storage.append(new_element)
def peek(self):
return self.storage[0]
def dequeue(self):
return self.storage.pop(0)
暂无解析
执行以下程序,输入qp,输出结果是:
k=0
whileTrue:
s=input('请输入q退出:')
ifs=='q':
k+=1
continue
else:
k+=2
break
print(k)
以下程序的输出结果是:
ls=list({'shandong':200,'hebei':300,'beijing':400})
print(ls)