【发布时间】:2015-10-21 00:38:01
【问题描述】:
我正在做一个 Python 程序,它实现了链表来支持一些功能,我需要做的一个功能是反转堆栈。我已经制作了一个 Node、LinkedList 和 Stack 类,这是我目前的代码:
class ListNode:
def __init__(self, Object):
self.Object = Object
self.next = None
class LinkedList:
def __init__(self):
self.head = None # first node
self.tail = None # last node
def addLast(self, Object):
newNode = ListNode(Object)
if self.head == None:
self.head = newNode
self.tail = newNode
else:
self.tail.next = newNode
self.tail = newNode
def removeFirst(self):
if self.head == None:
return
self.head = self.head.next
if self.head == None:
self.tail = None
def removeLast(self, Object):
if self.head == None:
return
current = self.head
prev = None
while current.next != None:
prev = current
current = current.next
if prev == None:
self.head = None
self.tail = None
else:
prev.next = None
self.tail = prev
def get(self, index):
current = self.head
i = 0
while i < index and current != None:
current = current.next
i = i + 1
if current != None and index >= 0:
return current.Object
else:
return None
def size(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.next
return count
def isEmpty(self):
return self.head == None
def printList(self):
if self.head != None:
current = self.head
while current != None:
print(current.Object, end = ' ')
current = current.next
print()
# -------------------- STACK ---------------------------------------------------
class Stack:
# constructor implementation
def __init__(self):
self.llist = LinkedList()
def front(self):
return self.llist.get(0)
def dequeue(self):
self.llist.removeFirst()
def queue(self, Object):
self.llist(Object)
def push(self, Object):
self.llist.addLast(Object)
def pop(self, Object):
self.llist.removeLast(Object)
def printStack(self):
self.llist.printList()
def size(self):
return self.llist.size()
def isEmpty(self):
return self.llist.isEmpty()
# ----------------------- Reverse LIST ------------------------------
def Reverse(S):
# S is a Stack Object
这是我对这个问题的尝试:
def rRecursive( self ) :
self._rRecursive( self.head )
def _reverseRecursive( self, n ) :
if None != n:
right = n.next
if self.head != n:
n.next = self.head
self.head = n
else:
n.next = None
self._rRecursive( right )
def Reverse(S):
s.rRecursive()
如果这是一个普通列表,我可以使用 [::-1] 轻松反转它,但我不能,因为链接列表是一个对象。我在想也许我可以使用临时值,这样我就可以将堆栈的开头附加到列表中,然后以某种方式将其转换回堆栈对象。
重复编辑:我的程序的目标是使用现有堆栈并将其反转。链接的帖子处理已经采用列表格式的链接列表。
def get(self, index):
current = self.head
i = 0
while i < index and current != None:
current = current.next
i = i + 1
if current != None and index >= 0:
return current.Object
else:
return None
编辑 2:添加了我的 get 函数。
class Stack:
# constructor implementation
def __init__(self):
self.llist = LinkedList()
def front(self):
return self.llist.get(0)
# push method implementation
def push(self, Object):
self.llist.addLast(Object)
def pop1(self):
self.llist.removeLast1()
def Reverse(S):
new_stack = Stack()
while not S.isEmpty():
new_stack.push(S.front())
S.pop1()
return new_stack
# Current Stack after Push: 12 14 40 13
# Stack after Pop: 12 14 40
# Stack after Reversal: 12 12 12
编辑 3:添加了我的代码的返工,它一遍又一遍地返回与第一个元素的错误反转。
【问题讨论】:
-
如果你实现了
__getitem__,你可以用[::1]反转它并让它接受val作为slice。我猜这是你的家庭作业? -
是的,这是我的家庭作业,这是我需要做的最后一个功能。我已经编辑了我之前工作的获取项目功能的问题。我遇到的问题是我无法反转我的 Stack 因为它已经是一个 Stack 对象。
标签: python list linked-list