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.
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?
Objective
Program Specification:
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.
Your program will build a list for the data maintained in ascending order based on both name and weight via a doubly linked list.
This dll will use one pointer to keep weights in sorted order, and use the other link to keep names on sorted order.
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.)
For example after 3 elements are added for (Name – Weight): Michael – 275, Tom – 150, Abe – 200.
Output: Names & weights sorted(ascending) by name. : Abe – 200, Michael – 275, Tom - 150 Names & weights sorted(ascending) by weight. : Tom – 150, Abe – 200, Michael - 275
Small piece of code I'm going from
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)
My singly Linked list (sorted) program
#!/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 > data:
n = Node()
n.data = data
n.next = curr
self.head = n
return
while curr.next is not None:
if curr.next.data > 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
Data: Names.txt
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
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.
Thank you for any help in advance. I greatly appreciate it.
__slots__unnecessarily needs remedial instruction in the Zen.