Пожалуйста, помогите мне правильно распечатать мой стек A класс? Вот токовый выход. Кроме того, как бы я перевернуть стек A?

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()
-1
Eli B 15 Ноя 2019 в 08:37

1 ответ

Вашему классу Node нужен метод __repr__, если вы хотите, чтобы repr(curr) в списке __repr__ вашего списка дал что-то полезное:

def __repr__(self):
    return repr(self.data)

В методе reverseStack есть по крайней мере одна базовая синтаксическая ошибка, которую вам нужно исправить, прежде чем вы сможете добиться прогресса в этой части проблемы.

0
Samwise 15 Ноя 2019 в 09:20