title: Doubly Linked list in Python
body: <p>Hey guys I'm really lost. I am writing a Doubly Linked List program for my Data Structures class and I just can't figure it out.</p>
  
  <p>UPDATE: So I have my singly linked list assignment done. How can I convert it to a doubly linked list and use the data provided to load it in and print it?</p>
  
  <h2>Objective</h2>
  
  <blockquote>
    <p>Program Specification:</p>
    
    <ol>
    <li><p>Read data for names and weights for 15 people from the console where there is a name on a line followed by a weight on the next line,
    like in names.txt.</p></li>
    <li><p>Your program will build a list for the data maintained in ascending order based on both name and weight via a doubly linked list.</p></li>
    <li><p>This dll will use one pointer to keep weights in sorted order, and use the other link to keep names on sorted order.</p></li>
    <li><p>You need to build the list as you go maintaining this ordering, so at any time a print method was called it would print the related field
    in order. (This means nodes are added to the list in sorted order,
    elements are not added to the list followed by a sort called on the
    list.)</p></li>
    </ol>
    
    <p>For example after 3 elements are added for (Name – Weight): Michael –
    275, Tom – 150, Abe – 200.</p>
    
    <p>Output: Names &amp; weights sorted(ascending) by name. : Abe – 200,
    Michael – 275, Tom - 150 Names &amp; weights sorted(ascending) by weight.
    : Tom – 150, Abe – 200, Michael - 275</p>
  </blockquote>
  
  <h2>Small piece of code I'm going from</h2>
  
  <pre><code>class LinkedList(object):
      __slots__ = 'prev', 'next', 'value'
  
  ll1 = LinkedList()
  ll2 = LinkedList()
  
  if __name__=="__main__":
  
    f = open("Names.txt","r")
  
    ll1.value = f.readline()
    ll1.next = ll2
    ll1.prev = None
  
  
    ll2.value = f.readline()
    ll2.next = None
    ll2.prev = ll1
  
    f.close()
  
    print("Linearly: \n")
    print(ll1.value)
    print(ll1.next.value)
  
    print("Reversely: \n")
    print(ll2.value)
    print(ll2.prev.value)
  </code></pre>
  
  <h2>My singly Linked list (sorted) program</h2>
  
  <pre><code>#!/usr/bin/env python
  
  class Node:
    def __init__(self):
      self.data = None
      self.next = None
  
  class LinkedList:
    def __init__(self):
      self.head = None
  
    def addNode(self, data):
      curr = self.head
      if curr is None:
        n = Node()
        n.data = data
        self.head = n
        return
  
      if curr.data &gt; data:
        n = Node()
        n.data = data
        n.next = curr
        self.head = n
        return
  
      while curr.next is not None:
        if curr.next.data &gt; data:
          break
        curr = curr.next
      n = Node()
      n.data = data
      n.next = curr.next
      curr.next = n
      return
  
    def __str__(self):
      data = []
      curr = self.head
      while curr is not None:
        data.append(curr.data)
        curr = curr.next
      return "[%s]" %(', '.join(str(i) for i in data))
  
    def __repr__(self):
      return self.__str__()
  
  if __name__=="__main__":
    ll = LinkedList()
    num = int(input("Enter a number: "))
    while num != -1:
      ll.addNode(num)
      num = int(input("Enter a number: "))
    c = ll.head
    while c is not None:
      print(c.data)
      c = c.next
  </code></pre>
  
  <h2>Data: Names.txt</h2>
  
  <pre><code>Jim
  150
  Tom
  212
  Michael
  174
  Abe
  199
  Richard
  200
  April
  117
  Claire
  124
  Bobby
  109
  Bob
  156
  Kevin
  145
  Jason
  182
  Brian
  150
  Chris
  175
  Steven
  164
  Annabelle
  99
  </code></pre>
  
  <p>As you can see I haven't done much. I am not sure how to load the data in properly and I'm just overall lost. I'm not sure where to start. I've looked at a couple examples online but they are just cryptic to me.</p>
  
  <p>Thank you for any help in advance. I greatly appreciate it.</p>