如何使用链表中存在的变量调用函数并在链表类外部的函数中接收参数

问题描述 投票:0回答:2

我正在使用两个python文件,一个文件中存在一个链表列表,另一个文件是我导入第一个文件的文件,这样我就可以使用我在第一个文件中构建的链表。第二个文件用于反向文件。我已经使用迭代部分完成了反向,现在尝试使用递归构建反向代码,并且我正在调用并在函数内部传递参数但是有些东西没有用完它显示TypeError就像这个函数没有参数一样。

请检查我的代码,然后输入错误

第二档

from code.linkedlist import *
llist=linkedlist()
llist.appendnodesatbegin(23)
llist.appendnodesatbegin(45)
llist.appendnodesatbegin(67)
llist.appendnodesatbegin(12)
llist.appendnodesatbegin(-11)
llist.appendnodesatbegin(0)
print ("Before reverse")
llist.display()
def reverseiterative():
    llist.current = llist.head
    llist.prev = None
    while (llist.current):
        llist.next = llist.current.next
        llist.current.next = llist.prev
        llist.prev = llist.current
        llist.current = llist.next
    llist.head = llist.prev
reverseiterative()
print("After the reverse of list using iterative method")
llist.display()
llist.p=llist.head
llist.prev=None
def reverserecursive(p,prev):
    next1=llist.p.next
    p.next=prev
    if llist.next1 is None:
        return
    else:
        reverserecursive(next1,p)


reverserecursive(llist.p,llist.prev)
print("After the reverse of list using recursive method")
llist.display()

first file:

class node:
    def __init__(self,data):
        self.data=data
        self.next=None
class linkedlist:
    def __init__(self):
        self.head=None
        self.last_pointer=None
    def appendnodesatbegin(self,data):
        newnode=node(data)
        if(self.head==None):
            self.head=newnode
            self.last_pointer=newnode
        else:
            self.last_pointer.next=newnode
            self.last_pointer=self.last_pointer.next
    def appendnodesatend(self,data):
        newnode=node(data)
        newnode.next=self.head
        self.head=newnode
    def appendatmid(self,prev,new):
        temp=self.head
        newnode=node(new)
        while(temp):
            if(temp.data==prev):
                newnode.next=temp.next
                temp.next=newnode
            temp=temp.next
    def display(self):
        temp=self.head
        while(temp):
            print(temp.data)
            temp=temp.next
    #def reversedisplay(self):

错误是

reverseiterative(llist.p,llist.prev)
TypeError: reverseiterative() takes no arguments (2 given)
python function pointers linked-list function-call
2个回答
1
投票

reverseiterative定义如下:

def reverseiterative():

不接受任何争论,你用2来称呼它。

考虑到你传递的参数和函数签名中的参数,你可能应该调用reverserecursive

def reverserecursive(p,prev):

0
投票

你的函数在减速时不带任何参数:

reverseiterative(foo, bar):

这(或您想要处理的任何值)将修复它。

© www.soinside.com 2019 - 2024. All rights reserved.