Python链接列表问题

时间:2016-04-13 05:44:24

标签: python linked-list

我遇到了链接列表中的类。我的代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

class Item(object):
    def __init__(self, data, next_item = None):
        self.data = data
        self.next_item = next_item

    def get_item(self):
        return self.data

    def set_next(self, setnext):
        self.next_item = setnext

    def get_next(self):
        return self.next_item

class LinkedList(object):
    def __init__(self):
        self.head = None

    def add(self,item):
        temp = Item(item)
        temp.set_next(self.head)
        self.head = temp

    def find(self, item):
        current = self.head
        while current != None:
            if current == item:
                print "Found It!"
            else:
                current = current.get_next()

    def print_list(self):
        node = self.head
        while node:
            print node.get_item()
            node = node.get_next()

    def size(self):
        counter = 0
        current = self.head
        while current != None:
            counter += 1
            current = current.get_next()
        print counter

    def insert(self,item,lpos):
        current = self.head
        while current != lpos:
            current = current.get_next()
            if current == None:
                return None
            else:
                item_insert = Item(item, lpos.next_item())
                lpos.set_next(item_insert)

myList = LinkedList()
myList.add(1)
myList.add(2)
myList.add(3)
myList.insert(8,2)

当我运行此代码时,方法(插入)失败,并出现以下错误:

Traceback (most recent call last):
  File "main.py", line 72, in <module>
    myList.insert(8,2)
  File "main.py", line 56, in insert
    item_insert = Item(item, lpos.Item.next_item())
  AttributeError: 'int' object has no attribute 'Item'

insert方法允许您在指定的位置向链接列表添加节点,并考虑插入执行重新排列正确的指针。

请指教!

2 个答案:

答案 0 :(得分:1)

你还没有注意到&#39; item&#39;之间的区别。和&#39;指数&#39;索引是列表中项目位置的无符号数字,但该项目是列表中的节点。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

class Item(object):
    def __init__(self, data, next_item = None):
        self.data = data
        self.next_item = next_item

    def get_item(self):
        return self.data

    def set_next(self, setnext):
        self.next_item = setnext

    def get_next(self):
        return self.next_item

class LinkedList(object):
    def __init__(self):
        self.head = None

    def add(self,item):
        temp = Item(item)
        temp.set_next(self.head)
        self.head = temp

    def find(self, item):
        current = self.head
        while current != None:
            if current == item:
                print "Found It!"
            else:
                current = current.get_next()

    def print_list(self):
        node = self.head
        while node:
            print node.get_item()
            node = node.get_next()

    def size(self):
        counter = 0
        current = self.head
        while current != None:
            counter += 1
            current = current.get_next()
        print counter

    def insert(self,item,lpos):
        if lpos == 0:
            item_insert = Item(item, self.head)
            self.head = item_insert
            return
        current = self.head.get_next()
        previous = self.head
        index = 1
        while index != lpos:
            index += 1
            previous = current
            current = current.get_next()
            if current == None:
                return None

        item_insert = Item(item, current)
        previous.set_next(item_insert)

myList = LinkedList()
myList.add(1)
myList.add(2)
myList.add(3)
myList.insert(8,0)
myList.print_list()

答案 1 :(得分:0)

lpos是一个索引,其类型为int。但是你要设置的是Item.next_item(),当然它不起作用。变化:

# lpos is Int
item_insert = Item(item, lpos.next_item())

为:

# use Item instance to do your stuff
item_insert = Item(item, current.next_item())

无论如何,你的插入实现应该是正确的。