0

Just to start, this is homework, so I'm merely looking for a hint here. I'm pretty new to Python and programming in general. I'm supposed to implement a cursor-based list that is doubly linked. I'm having some trouble with inserting onto the list. My instructor provided a simple Node class a a Node2Way class. He also provided the init method:

from node import Node
class Node2Way(Node):
 def __init__(self,initdata):
 Node.__init__(self,initdata)
 self.previous = None
 def getPrevious(self):
 return self.previous
 def setPrevious(self,newprevious):
 self.previous = newprevious

Here is what I have so far (just the pertinent methods):

from node2way import Node2Way
class CursorBasedList(object):
 """ Linked implementation of a positional list."""
 def __init__(self):
 """ Creates an empty cursor-based list."""
 self._header = Node2Way(None)
 self._trailer = Node2Way(None)
 self._trailer.setPrevious(self._header)
 self._header.setNext(self._trailer)
 self._current = None
 self._size = 0
def insertAfter(self, item):
 """Inserts item after the current item, or
 as the only item if the list is empty. The new item is the
 current item."""
 temp = Node2Way(item)
 if self.isEmpty():
 self._header.setNext(temp)
 self._trailer.setPrevious(temp)
 else:
 temp.setNext(self._current.getNext())
 self._current.setNext(temp)
 temp.setPrevious(self._current)
 self._current = temp
 self._size+=1

When I test the insertAfter method out, it works for adding the first item, but when I try to add the second item, it says that self._current is None type and can't use the getNext method. I don't know if there is another way to get temp to reference the node after the current one. I'm not sure what I'm doing wrong, or even if anything I'm doing is right. I think once I get the insertAfter method right, I'll be fine with the insertBefore method.

Any hints would be appreciated. Thank you in advance! :)

asked Feb 23, 2013 at 20:31
1
  • Hey, can you post the code where you actually insert the two items into CursorBasedList(), as well as the Error message? Commented Feb 23, 2013 at 23:12

1 Answer 1

1

In the case of

 if self.isEmpty():
 self._header.setNext(temp)
 self._trailer.setPrevious(temp)

You never set the previous and next nodes of temp.

answered Feb 23, 2013 at 20:47

1 Comment

Thanks for that tip. I'm just having some trouble understanding linked lists. :\

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.