Please help me properly print my stackA class? Here is the current output. Also, how would I reverse the stackA?
C:\Users\Eli\PycharmProjects\GuessingGameReview\venv\Scripts\python.exe C:/Users/Eli/.PyCharmCE2019.2/config/scratches/Assignment_3.py
5
0
1
2
3
4
stackA is Empty!
[<__main__.Node object at 0x000001E54DEB0CC0>, <__main__.Node object at 0x000001E54DEB0C88>, <__main__.Node object at 0x000001E54DEB0C18>, <__main__.Node object at 0x000001E54DEB0BE0>, <__main__.Node object at 0x000001E54DEB0C50>]
[<__main__.Node object at 0x000001E54DEB0CC0>, <__main__.Node object at 0x000001E54DEB0C88>, <__main__.Node object at 0x000001E54DEB0C18>, <__main__.Node object at 0x000001E54DEB0BE0>, <__main__.Node object at 0x000001E54DEB0C50>]
[<__main__.Node object at 0x000001E54DEB0CC0>, <__main__.Node object at 0x000001E54DEB0C88>, <__main__.Node object at 0x000001E54DEB0C18>, <__main__.Node object at 0x000001E54DEB0BE0>, <__main__.Node object at 0x000001E54DEB0C50>]
Process finished with exit code 0
Process finished with exit code 0
class Node:
def __init__(self, data=None, next=None):
self.data = data
self.next = next
self.prev = None
class doublelyLinkedList:
def __init__(self,head = None, tail = None,size = 0):
self.head = head
self.tail = tail
self.size = size
def push(self, e):
self.__data.append(e)
def pop(self):
if self.empty():
# raise Empty(stack is empty)
print("stack is empty")
return
return self.__data.pop()
def empty(self):
return len(self.__data) == 0
def removeHead(self, head):
temp = head
obj = head.data
head = head.next
temp.next = None
return obj
def removetail(self):
temp = self.head
while temp.next:
R = temp
temp = temp.next
self.tail = temp.prev
R.next = None
return temp
def append(self,data):
if self.head is None:
new_node = Node(data)
new_node.prev = None
self.head = new_node
else:
new_node = Node(data)
cur = self.head
while cur.next:
cur = cur.next
cur.next = new_node
new_node.prev = cur
new_node.next = None
def prepend(self,data):
if self.head is None:
new_node = Node(data)
new_node.prev = None
self.head = new_node
else:
new_node = Node(data)
self.head.prev = new_node
new_node.next = self.head
self.head = new_node
def __repr__(self):
nodes = []
curr = self.head
while curr:
nodes.append(repr(curr))
curr = curr.next
return '[' + ', '.join(nodes) + ']'
def isempty(self):
return self.head == None and self.tail == None
def print_list(self):
cur = self.head
while cur:
print(cur.data)
cur = cur.next
class deque:
def __init__(self):
self.dlist = doublelyLinkedList
def front_pop(self,e):
self.dlist.prepend(e)
def front_push(self):
if self.dlist.isempty():
print("deque is empty")
return
return self.dlist.removeHead()
def back_pop(self,e):
self.dlist.prepend(e)
def back_push(self,e):
if self.dlist.isempty():
print("deque is empty")
return
return self.dlist.removetail()
def __repr__(self):
return doublelyLinkedList. __repr__(self.dlist)
class stackA:
def __init__(self):
self.dlist = doublelyLinkedList()
def push(self, e):
self.dlist.prepend(e)
def pop(self):
if self.dlist.isempty():
print("stackA is Empty!")
return
return self.dlist.removeHead
def reverse_stack(self):
rev = Queue
curr = self.dlist.tail
while curr:
rev.enqueue(curr)
curr = curr.prev
self.list = rev.dlist
def __repr__(self):
return doublelyLinkedList.__repr__(self.dlist)
class Queue:
def __init__(self):
self.dlist = doublelyLinkedList()
def enqueue(self, e):
self.dlist.append(e)
def dequeue(self):
if self.dlist.isempty():
print("Queue is Empty!")
return
return self.dlist.removeHead()
def __repr__(self):
return doublelyLinkedList.__repr__(self.dlist)
def ReverseStack(t):
e = Queue()
while t.dlist.head != None:
e.enqueue(stackA.pop(t))
print("!!!!")
if __name__ == "__main__":
dlist = doublelyLinkedList()
dlist.prepend(0)
dlist.append(1)
dlist.append(2)
dlist.append(3)
dlist.append(4)
dlist.prepend(5)
dlist.print_list()
deque = deque()
stackA = stackA()
Queue = Queue()
# stack TEST
stackA.pop()
stackA.push("Obama")
stackA.push("Donald")
stackA.push("Bush")
stackA.push("Lincoln")
stackA.push("Roosevelt")
print(stackA)
stackA.pop()
print(stackA)
stackA.reverse_stack()
print(stackA)
print()
Your Node
class needs a __repr__
method if you want repr(curr)
in your dlist's __repr__
to yield something useful:
def __repr__(self):
return repr(self.data)
The reverseStack
method has at least one basic syntax error that you'll need to fix before you can make any progress on that part of the problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.