Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 使用链表的堆栈表示堆栈没有作为头的属性_Python 2.7_Linked List_Stack - Fatal编程技术网

Python 2.7 使用链表的堆栈表示堆栈没有作为头的属性

Python 2.7 使用链表的堆栈表示堆栈没有作为头的属性,python-2.7,linked-list,stack,Python 2.7,Linked List,Stack,我试图在Python中使用LinkedList实现堆栈。我编写了以下代码: class Node: def __init__(self): self.data = None self.next = None def getData(self): return self.data def getNext(self): return self.next def s

我试图在Python中使用LinkedList实现堆栈。我编写了以下代码:

class Node:


    def __init__(self):
            self.data = None
            self.next = None

    def getData(self):
            return self.data

    def getNext(self):
            return self.next

    def setData(self,newdata):
            self.data = newdata



    def setNext(self,newnext):
            self.next = newnext



class Stack :


    def _init_(self):
        self.head=None


    def push(self,data):

        temp=Node()
        temp.setData(data)
        temp.setNext(self.head)
        self.head=temp

    def pop(self):

        temp=Node()
        temp=self.getData()
        self.head=self.head.getNext()
        return temp
但当我尝试将数据项推送到堆栈时,会收到一条错误消息


AttributeError:堆栈实例没有“head”属性。请帮助理解出了什么问题。

\uuuu init\uuuu()
用两个前导和尾随下划线拼写,而不是一个。非常感谢!这很有帮助。