python会内存泄漏吗_会不会内存泄漏?

视频里的代码有问题。多出需要更改。以下是根据教学视频更改过的代码。

class Stack():

def __init__(self, size):

self.stack = []

self.size = size

self.top = -1

def push(self, content):

if self.isFull():

print "Stack is full"

else:

self.stack.append(content)

self.top = 1

def pop(self):

if self.isEmpty():

print "Stack is empty"

else:

self.stack.pop()

self.top -= 1

def isFull(self):

if self.top 1 == self.size:

return True

else:

return False

def isEmpty(self):

if self.top == -1:

return True

else:

return False

def printStackInfo(aStack):

print "isEmpty:t{0}".format(aStack.isEmpty())

print "isFull:tt{0}".format(aStack.isFull())

print "top:tt{0}".format(aStack.top)

print "stack:tt{0}".format(aStack.stack)

print "1. Initialise a stack with a size of 2, i.e. store at most 2 elements"

s = Stack(2)

printStackInfo(s)

print "n2. Push 'a'"

s.push('a')

printStackInfo(s)

print "n3. push 'b'"

s.push('b')

printStackInfo(s)

print "n4. push 'c'"

s.push('c')

printStackInfo(s)

print "n5. pop the top element"

s.pop()

printStackInfo(s)

print "n6. pop the top element"

s.pop()

printStackInfo(s)

print "n7. pop the top element"

s.pop()

printStackInfo(s)

视频里的代码没有删除out()中指定的元素,元素依然在stack中。那么out过后再append就会出问题。把stack打印出来就能看到问题在哪儿了。

class Queue():

def __init__(self, size):

self.queue = []

self.size = size

self.head = 0

self.tail = 0

def isEmpty(self):

if self.head == self.tail:

return True

else:

return False

def isFull(self):

if self.tail - self.head == self.size: ####

return True

else:

return False

def inQueue(self, content):

if self.isFull():

print "Queue is full"

else:

self.queue.append(content)

self.tail = 1

def outQueue(self):

if self.isEmpty():

print "Queue is empty"

else:

self.queue.remove(self.queue[0])

self.tail -= 1

def printQueueInfo(aQueue):

print "isEmpty:t{0}".format(aQueue.isEmpty())

print "isFull:tt{0}".format(aQueue.isFull())

print "head:tt{0}".format(aQueue.head)

print "tail:tt{0}".format(aQueue.tail)

print "queue:tt{0}".format(aQueue.queue)

print "1. Initialise a stack with a size of 2, i.e. store at most 2 elements"

q = Queue(2)

printQueueInfo(q)

print "n2. Add 'a'"

q.inQueue('a')

printQueueInfo(q)

print "n3. Add 'b'"

q.inQueue('b')

printQueueInfo(q)

print "n4. Remove 'a'"

q.outQueue()

printQueueInfo(q)

print "n5. Add 'c'"

q.inQueue('c')

printQueueInfo(q)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值