so I've been trying to insert an item to the front of a linked list, and it sort of works, but not fully. Here is what I have so far (there are more methods in the LinkedList class, but I omitted them because they weren't a problem):
class _Node():
def __init__(self, data=None, link=None):
self.data = data
self.link = link
class LinkedList():
def __init__(self):
self.first = None
self.size = 0
def insert(self, ind, item):
if self.first is None:
self.first = _Node(item)
elif ind == 0: # this is where the problem is. When I try to
temp = self.first # insert a node to the front, it seems to
temp.link = self.first.link # forget about the rest of the nodes.
self.first = _Node(item)
self.first.link = temp
else:
count = 0
while count != ind - 1:
count += 1
self.first = self.first.link
self.first.link = _Node(item, self.first.link)
self.size += 1
say I have this in shell:
>>> L = LinkedList()
>>> L.insert(0, 5)
>>> L.insert(1, 10)
>>> L.insert(0, 20)
>>> L[0]
20
>>> L[1]
5
>>> L[2]
# and here is an error message, it says NoneType object has no attribute 'data'
so in my code above, what I'm trying to do is create a temporary node object that is identical to the first node object, I link that temporary node to the first nodes link, I create the new node, and I link that new node to the temporary one, but this doesn't work. Any help would be great, thanks!
It seems as though those functions that you left out because they "weren't the problem", might actually be your problem...
Your code works if you ask for the data in each node in this way:
>>> L = LinkedList()
>>> L.insert(0,5)
>>> L.insert(1,10)
>>> L.insert(0,20)
>>> print L.first.data
20
>>> print L.first.link.data
5
>>> print L.first.link.link.data
10
You probably have an issue where you are defining __getitem__
. Additionally, the part that you commented on can be rewritten in one line which may be more Pythonic.
temp = self.first
temp.link = self.first.link
self.first = _Node(item)
self.first.link = temp
The first two lines do nothing because temp
is self.first
so all you are saying is self.first.link = self.first.link
. The next two can be written:
self.first = _Node(item, self.first)
Firstly, note you actually don't need to special-case the empty list here:
def insert(self, ind, item):
if ind == 0:
newnode = _Node(item, self.first)
self.first = newnode
Secondly, that isn't the problem. This code:
else:
count = 0
while count != ind - 1:
count += 1
self.first = self.first.link
self.first.link = _Node(item, self.first.link)
self.size += 1
changes self.first in place, so it forgets what the first node was before. The smallest change to fix this would be:
else:
count = 0
insert_point = self.first # use a local variable to walk the list
while count != ind - 1:
count += 1
insert_point = insert_point.link
insert_point.link = _Node(item, insert_point.link)
self.size += 1
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.