Data Structures and Algorithms in Ruby: Linked Lists #2

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ธ.ค. 2024

ความคิดเห็น • 22

  • @connerjensen8170
    @connerjensen8170  3 ปีที่แล้ว

    More videos on ROR and Ruby to come! Please let me know if you have any video suggestions. Thanks for watching!

    • @yasinowen
      @yasinowen 3 ปีที่แล้ว

      Create a top level crud api and tricks

  • @chantalwobben7518
    @chantalwobben7518 3 ปีที่แล้ว +1

    Maybe a idea for video 3 how to reverse a linkedList ?

  • @RR-et6zp
    @RR-et6zp 2 ปีที่แล้ว

    Can you explain what a node is? and what an edge is ? is it like the title of the data structure ? how stuff interacts with each other

  • @santaclause1988ify
    @santaclause1988ify 3 ปีที่แล้ว

    More data structure and algorithm videos please

  • @VictorBStan
    @VictorBStan 3 ปีที่แล้ว +1

    Also, if you're working with adding and removing values, you could potentially add the same value multiple times, then remove one of them, but still find that you have a node with the same value. maybe that's a feature, maybe that's a bug, depends on how you might use your linked list. another options is to use 'indexes'. so you can insert values in between nodes as well. just some thoughts. There are many ways to implement linked lists :)

    • @connerjensen8170
      @connerjensen8170  3 ปีที่แล้ว

      Awesome suggestions and thoughts Victor! I appreciate your feedback.

  • @jetspray3
    @jetspray3 3 ปีที่แล้ว

    And just a few months ago I learn there is an implicit self created if you used an instance method in another method. It makes me reflect on the implicit return created at the end of a method.

  • @yasinowen
    @yasinowen 3 ปีที่แล้ว +1

    next video RoR tricks.

  • @sidduh5607
    @sidduh5607 3 ปีที่แล้ว

    I think in remove method, we need to make self.head = node.nextNode if we are trying to remove first node.

  • @GokuSan389
    @GokuSan389 3 ปีที่แล้ว +2

    Hello, nice videos. Just a small tip :) I believe you could, instead of always calling Node.new(value, nil), assign nextNode = nil in initialize method as default value. Also a question why do you have class in class ??

  • @ritikagakher9688
    @ritikagakher9688 ปีที่แล้ว

    More DS videos please 🙏

    • @connerjensen8170
      @connerjensen8170  ปีที่แล้ว

      Long time off, but working on more content now!

  • @ulyssebuonomo4148
    @ulyssebuonomo4148 3 ปีที่แล้ว +1

    Prepend could really be a one liner, no need for nil check.
    self.head = Node.new value, head

  • @jazthomson1171
    @jazthomson1171 3 ปีที่แล้ว +1

    Sorry but I think there might be a bug in your remove method - if you remove the head when there are more nodes after it, how will you manage to traverse to those nodes? Perhaps I am misunderstanding. Nice video regardless :)

    • @GokuSan389
      @GokuSan389 3 ปีที่แล้ว +2

      I second that. He has a bug in the remove method. I have implemented a print_list method and it stops due to the return if head.nil?
      It should be return self.head = node.next_node if node.value.eql?( value )
      So first you set node = head than prev_node = nil and than the return and than the until loop....

    • @connerjensen8170
      @connerjensen8170  3 ปีที่แล้ว

      Yes, you are right. Sorry about this error

  • @VictorBStan
    @VictorBStan 3 ปีที่แล้ว +1

    Also curious, maybe someone already mentioned, but it's common to use @val (instance variables), self.val usually used for class variables. attr_* gives you the methods to get/set instance variables on an object, so in initialize just use @val and @next_node; then you can just say object.val/object.next_node and object.val = 'val', object.next_node = 'val'.

  • @MoeNegm
    @MoeNegm 2 ปีที่แล้ว

    so i tried this and i think there is a bug in your remove method.
    here is my test:
    ll = LinkedList.new
    ll.add(10)
    ll.add(20)
    ll.prepend(5)
    puts ll.head.value #5
    puts ll.head.nextNode.value #10
    puts ll.head.nextNode.nextNode.value #20
    puts ll.find(10) #true
    puts ll.find(50) #false
    puts ll.find(20) #true
    puts ll.find(50) #false
    puts ll.find(10) #true
    ll.remove(5)
    puts ll.head.value #undefined method `value' for nil:NilClass
    puts ll.head.nextNode.value
    this is not working for when you remove the first node in the list, it does work if i remove the second or third. but not the first

    • @MoeNegm
      @MoeNegm 2 ปีที่แล้ว

      here is how i solved it:
      def remove(value)
      return if head.nil?
      node = self.head
      prev_node = nil
      return self.head = self.head.nextNode if node.value == value
      while node && value != self.head
      if node.value == value
      if prev_node
      prev_node.nextNode = node.nextNode
      return
      else
      self.head = nil
      return
      end
      end
      prev_node = node
      node = node.nextNode
      end
      end

    • @MoeNegm
      @MoeNegm 2 ปีที่แล้ว

      thanks to your two videos, i feel comfortable enough with nodes now where as they were a complete mystery before. Keep up the great work, I'll be subscribing.