linked list - Can't get LinkedList implementation to work (Python) -


i felt should make question earlier today, because problem different before. , wanted leave other question reference. pretty cluttered. if problem, let me know.

from can tell, nothing added linked list. doesn't print or give me errors, , problem. it's supposed insert words alphabetically. seems logical me. redid of insert().

i feed files single words on each line. functions of list insert , print. example text (not including blank lines):

here code:

import sys, os, copy, fileinput class node:     def __init__(self, word):         self.data = word         self.next = none     def nextnode(self):         if self.next not none:             return self.next         else:             return none     def getdata(self):         return self.data     def setnext(self, node):         self.next = node     def hasnext(self):         if self.next == none:             return false         else:             return true  class linked_list:     def __init__(self):         self.head = node(none)         self.isempty = true     def insert(self, word):         newnode = node(word)         #look position insert:          #when empty         if self.isempty == true:             self.isempty = false             self.head = newnode         #when has more 2 nodes          else:             prev = none             current = self.head             nextfound = false #the next current when less node              while nextfound == false , current != none:                 if current.getdata() < newnode.getdata():                     prev = copy.copy(current)                     current = current.nextnode()                 else:                     nextfound = true              if prev == none:                 nextnode = copy.copy(current)                 self.head = newnode                 self.head.setnext(nextnode)             else:                 prev.setnext(newnode)                 newnode.setnext(current)      def printlinkedlist(self):         if self.head.getdata() == none:             print("the file empty.")         else:             prints = self.head             while prints.hasnext():                 sys.stdout.write(prints.getdata() + '\n')                 prints.setnext(prints.nextnode())  linkedlist = linked_list()  wordlist = ["hello", "jupiter", "albacore", "shrimp", "axe"] line in wordlist:     linkedlist.insert(line) linkedlist.printlinkedlist() 

the problem you're making copy of previous node here:

prev = copy.copy(current) 

so, when update copy in-place here:

prev.setnext(newnode) 

… doesn't affect original node that's linked list. (nor replace original node modified copy.) so, nothing ever gets changed.

to fix it, remove copy.copy.


when fix that, there's bug in code lead infinite loop of printing out "absolute", in printlinkedlist:

prints.setnext(prints.nextnode()) 

this doesn't useful—it sets prints.next prints.next. crucially, doesn't update variable prints point next node. this:

prints = prints.nextnode() 

and both of changes, output original example is:

absolute crisp daytona demand 

however, notice new example missing 1 value:

albacore axe hello jupiter 

i'll leave figure out shrimp went. (you can post new question if stuck.)


if you're wondering how found problem:

i added print statement after while loop dumps out bunch of information found previous node, including id, , print before , after setnext, see setting next member of first node each time through loop, different first node each time through.

then added print show me id of each nodes, , clear found previous node each time wasn't of nodes in list. @ point copy.copy jumped out @ me.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -