title: Ordered Doubly Linked List
body: <p>Ok A question from assignment says to create an ordered doubly linked list...such that each object with lexicographically smaller name comes "Before" the other One...Like names in a Dictionary...also objects with same name can be arranged in any order...</p>
  
  <p>To link two objects I have <code>setBefore()</code> and <code>setAfter()</code> method...
  and I have done this much...but still don't know where I'm doing wrong..may be a little guidance from you guys can help me...</p>
  
  <p><code>atMe</code> is an object that is already present in the doubly linked list and <code>newFrob</code> is an object to be inserted...</p>
  
  <pre><code>def insert(atMe, newFrob):
      if newFrob.myName() &lt; atMe.myName():
          if atMe.getBefore() == None:
              atMe.setBefore(newFrob)
              newFrob.setAfter(atMe)
          elif atMe.getBefore().myName()&lt;newFrob.myName():
              atMe.getBefore().setAfter(newFrob)
              newFrob.setBefore(atMe.getBefore)
              atMe.setBefore(newFrob)
              newFrob.setAfter(atMe)
          else:
              insert(atMe.getBefore(),newFrob)
  
      elif newFrob.myName() &gt; atMe.myName():
          if atMe.getAfter() == None:
              atMe.setAfter(newFrob)
              newFrob.setBefore(atMe)
          elif atMe.getAfter().myName()&gt;newFrob.myName():
              atMe.getAfter().setBefore(newFrob)
              newFrob.setAfter(atMe.getAfter)
              atMe.setAfter(newFrob)
              newFrob.setBefore(atMe)
          else:
              insert(atMe.getAfter(),newFrob)
  
      elif newFrob.myName()==atMe.myName():
          if atMe.getAfter() != None:
              newFrob.setAfter(atMe.getAfter())
          newFrob.setBefore(atMe)
          if atMe.getAfter() != None:
              atMe.getAfter().setBefore(newFrob)
          atMe.setAfter(newFrob)
  </code></pre>
  
  <p>And this is the Frob Class to be used...</p>
  
  <pre><code>class Frob(object):
      def __init__(self, name):
          self.name = name
          self.before = None
          self.after = None
      def setBefore(self, before):
          self.before = before
      def setAfter(self, after):
          self.after = after
      def getBefore(self):
          return self.before
      def getAfter(self):
          return self.after
      def myName(self):
          return self.name
  </code></pre>
  
  <p>where Before and After are links to left and right objects in double linked list...
  objects from this class are to be inserted to double linked list...</p>
  
  <p>Example:</p>
  
  <pre><code>a=Frob('foo')
  b=Frob('bar')
  c=Frob('frob')
  d=Frob('code')
  
  code                             output
  insert(a,b)                   bar-&gt;foo
  insert(a,c)                   bar-&gt;foo-&gt;frob
  insert(b,d)                   bar-&gt;code-&gt;foo-&gt;frob
  </code></pre>
  
  <p>now suppose</p>
  
  <pre><code>code                             output
  insert(b,Frob('code'))        bar-&gt;code-&gt;code-&gt;foo-&gt;frob
  </code></pre>