在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)
暂无解析
已知id(ls1)=4404896968,以下程序的输出结果是:
ls1=[1,2,3,4,5]
ls2=ls1
ls3=ls1.copy()
print(id(ls2),id(ls3))
以下程序的输出结果是:
defhub(ss,x=2.0,y=4.0):
ss+=x*y
ss=10
print(ss,hub(ss,3))