list insertion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Randy Bush

    #1

    list insertion

    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

  • Jordan Rastrick

    #2
    Re: list insertion

    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:[color=blue]
    > 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[/color]

    Comment

    • Sybren Stuvel

      #3
      Re: list insertion

      Randy Bush enlightened us with:[color=blue]
      > hold = self.next
      > self.next = DaClass(value)
      > self.next.next = hold[/color]

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

      What did you expect, and what did you ovserve?

      Sybren
      --
      The problem with the world is stupidity. Not saying there should be a
      capital punishment for stupidity, but why don't we just take the
      safety labels off of everything and let the problem solve itself?
      Frank Zappa

      Comment

      • Ross Wilson

        #4
        Re: list insertion

        On Tue, 23 Aug 2005 20:58:11 -0700, Randy Bush wrote:
        [color=blue]
        > 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[/color]

        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

        Comment

        • Randy Bush

          #5
          Re: list insertion

          >> hold = self.next[color=blue][color=green]
          >> self.next = DaClass(value)
          >> self.next.next = hold[/color]
          > shouldn't that last line be this?
          > self.next.prev = hold[/color]

          single threaded list
          [color=blue]
          > What did you expect, and what did you ovserve?[/color]

          i will try to distill a case

          randy

          Comment

          • Bengt Richter

            #6
            Re: list insertion

            On Tue, 23 Aug 2005 20:58:11 -0700, Randy Bush <randy@psg.co m> wrote:
            [color=blue]
            >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.
            >[/color]
            I think you're fine. Here's some possible context for your code:
            [color=blue][color=green][color=darkred]
            >>> class DaClass(object) :[/color][/color][/color]
            ... 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())) )
            ...
            ...[color=blue][color=green][color=darkred]
            >>> sll = DaClass(1)
            >>> sll[/color][/color][/color]
            <DaClass 1>[color=blue][color=green][color=darkred]
            >>> sll.insert(2)[/color][/color][/color]
            <DaClass 1 -> 2>[color=blue][color=green][color=darkred]
            >>> sll[/color][/color][/color]
            <DaClass 1 -> 2>[color=blue][color=green][color=darkred]
            >>> sll.insert('a')[/color][/color][/color]
            <DaClass 1 -> 'a' -> 2>[color=blue][color=green][color=darkred]
            >>> sll.next[/color][/color][/color]
            <DaClass 'a' -> 2>[color=blue][color=green][color=darkred]
            >>> sll.next.insert ('b')[/color][/color][/color]
            <DaClass 'a' -> 'b' -> 2>[color=blue][color=green][color=darkred]
            >>> sll[/color][/color][/color]
            <DaClass 1 -> 'a' -> 'b' -> 2>

            Regards,
            Bengt Richter

            Comment

            • Randy Bush

              #7
              Re: list insertion

              >> hold = self.next[color=blue][color=green]
              >> 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.[/color]
              > I think you're fine.[/color]

              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

              Comment

              Working...