list insertion

R

Randy Bush

i am trying to insert into a singly linked list

hold = self.next
self.next = DaClass(value)
self.next.next = hold

but i suspect (from print statement insertions) that the result
is not as i expect. as the concept and code should be very
common, as i am too old for pride, i thought i would ask.

mahalo,
randy
 
J

Jordan Rastrick

What you've posted looks right, more or less.

To get any sort of meaningful feedback, you really need to post a full
version of your code, including the print statements, what's being
printed, and why you think this shows the code is not working. What
you've shown is far too little for anybody else to work with.

Regards,
Jordan


Randy Bush:
 
S

Sybren Stuvel

Randy Bush enlightened us with:
hold = self.next
self.next = DaClass(value)
self.next.next = hold

shouldn't that last line be this?
self.next.prev = hold
but i suspect (from print statement insertions) that the result is
not as i expect.

What did you expect, and what did you ovserve?

Sybren
 
R

Ross Wilson

i am trying to insert into a singly linked list

hold = self.next
self.next = DaClass(value)
self.next.next = hold

but i suspect (from print statement insertions) that the result
is not as i expect. as the concept and code should be very
common, as i am too old for pride, i thought i would ask.

mahalo,
randy

The example above looks like it would work, as long as other
stuff you _don't_ show is fine. Specifically, how do you
handle the case when the list is empty?

Better to show a more complete example with output and how that
is not what you expect.

Ross
 
B

Bengt Richter

i am trying to insert into a singly linked list

hold = self.next
self.next = DaClass(value)
self.next.next = hold

but i suspect (from print statement insertions) that the result
is not as i expect. as the concept and code should be very
common, as i am too old for pride, i thought i would ask.
I think you're fine. Here's some possible context for your code:
... def __init__(self, value):
... self.value = value
... self.next = None
... def values(self):
... while True:
... yield self.value
... if self.next is None: break
... self = self.next
... def insert(self, value):
... hold = self.next
... self.next = DaClass(value)
... self.next.next = hold
... return self
... def __repr__(self):
... return '<DaClass %s>'%(' -> '.join(map(repr, self.values())))
...
... <DaClass 1 -> 'a' -> 'b' -> 2>

Regards,
Bengt Richter
 
R

Randy Bush

hold = self.next
I think you're fine.

indeed. the bug was elsewhere. i got confused and tried to
look-ahead too far when i could have just recursed. i threw
away the too-clever code and replaced it with one line. i
love it when that happens.

randy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top