Data Structures in Python: Circular Linked Lists -- Append and Prepend

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

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

  • @cristianocostaqueiroz2989
    @cristianocostaqueiroz2989 2 หลายเดือนก่อน

    Very didatic, clear and essential; You are at least 1000 x better than my data structutre teachers all added together. Keep up the excelent work

  • @ravitanwar9537
    @ravitanwar9537 6 ปีที่แล้ว +14

    the quality of these videos is really good man . you deserve more views.

    • @LucidProgramming
      @LucidProgramming  6 ปีที่แล้ว +3

      Thank you, Ravi. I appreciate your comment, and here's hoping other viewers have the same outlook as you. Cheers!

  • @Dharmaraj-s9f
    @Dharmaraj-s9f 11 หลายเดือนก่อน +1

    Very nice explanation sir

    • @LucidProgramming
      @LucidProgramming  11 หลายเดือนก่อน +1

      Cheers, thank you for watching!

  • @manoganesydwell1785
    @manoganesydwell1785 4 ปีที่แล้ว

    If I can sound as calm as you do while talking about code, I'll know I've made it in life.
    Thank you for the content!!

  • @vayunandu
    @vayunandu 6 ปีที่แล้ว +2

    Amazing videos. The best service in the world is to share your knowledge with others and you're doing it well. Thank you very much. So, the prepend is exactly like append except you set the self.head to the new node. I felt that it would be better to not repeat the same code at multiple places. The below should work fine too.
    def append(self, data):
    if self.head is None:
    new_node = Node(data)
    self.head = new_node
    self.head.next = self.head
    else:
    new_node = Node(data)
    cur = self.head
    while cur.next != self.head:
    cur = cur.next
    cur.next = new_node
    new_node.next = self.head
    return new_node
    def prepend(self, data):
    new_node = self.append(data)
    self.head = new_node

    • @LucidProgramming
      @LucidProgramming  6 ปีที่แล้ว

      But the data is added to the end of the list, not the beginning, so your prepend function isn't correct.

    • @vayunandu
      @vayunandu 6 ปีที่แล้ว

      @@LucidProgramming ah Thank you!

  • @mateuszsmendowski2677
    @mateuszsmendowski2677 5 ปีที่แล้ว +3

    Amazing series! My goal is to realize all your materials about programming in Python. Thank You!

    • @LucidProgramming
      @LucidProgramming  5 ปีที่แล้ว

      A great goal :). Cheers, and thanks for watching!
      If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing!
      I really appreciate the support, and if you want to support the channel, I do have a PayPal link (www.paypal.me/VincentRusso1) for donations that go directly to the creation of content on this channel.
      I hope that the content I provide there will enhance the videos on my TH-cam page.
      bit.ly/lp_email

    • @qc4257
      @qc4257 4 ปีที่แล้ว

      the student will never become the master hahaha

  • @rahulmaurya597
    @rahulmaurya597 4 ปีที่แล้ว +1

    Thank you LucidProgramming for these insightful content. Please make more video on DSA and more algorithm

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!

    • @rahulmaurya597
      @rahulmaurya597 4 ปีที่แล้ว +1

      @@LucidProgramming I already did, even I refer your content with my junior and colleagues.

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      @@rahulmaurya597 That is awesome, I sincerely appreciate all of that support. Thank you!

  • @rhythmsaparia8788
    @rhythmsaparia8788 5 ปีที่แล้ว +1

    Nice video, I was struggling to understand circular linked list just by looking at the code, your video helped me understand the code part. Thanks!

    • @LucidProgramming
      @LucidProgramming  5 ปีที่แล้ว

      Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing!
      I really appreciate the support, and if you want to support the channel, I do have a PayPal link (www.paypal.me/VincentRusso1) for donations that go directly to the creation of content on this channel.
      I hope that the content I provide there will enhance the videos on my TH-cam page.
      bit.ly/lp_email

  • @dishanmadhuranga8032
    @dishanmadhuranga8032 5 ปีที่แล้ว +1

    Please continue your good work... Amazing channel.. You do every step by coding not only lecturing... that's why this channel is very good.. I would like to suggest you to do some database topics too.. good luck... good channel.. your channel is underrated, you deserve 1 million subs. definitely One day you can achieve that... !!!!

    • @LucidProgramming
      @LucidProgramming  5 ปีที่แล้ว

      Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing!
      I really appreciate the support, and if you want to support the channel, I do have a PayPal link
      paypal.me/VincentRusso1
      for donations that go directly to the creation of content on this channel.
      I hope that the content I provide there will enhance the videos on my TH-cam page.
      bit.ly/lp_email

  • @srimanmishra487
    @srimanmishra487 4 ปีที่แล้ว +1

    Hi.... I am from India... I really like your videos and I a watch them frequently. They are just great. Please upload videos on exponential tree data structure also in your data structure series.

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      It's on my list, just need to find the time to record them!

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

    Very Informative man. Keep going..!!

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

      Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!

  • @vijaikumar2010
    @vijaikumar2010 11 หลายเดือนก่อน

    nicely explained thanks for the clear explanation.

  • @muraliperi7669
    @muraliperi7669 4 ปีที่แล้ว

    Hi Lucid , I have paid for some course(s) in udemy to get depth knowledge on datastructure and algorithm in Python . But i was eventually end up with confusions in the mid of course(s) and thought of negative thinking like i can't learn data structures and alogorithm anymore . Later i got notification for your videos on youtube and i really loved the way you explain each and every topic(s) . Actually your teaching method is pretty awesome and it gave me confidence in programming . Let me come to the point here . I have managed to create prepend function in circular linked list and this also works . Kindly validate and let me know if i am missing anything here . Thank you very much all you do for the students like me ( beginner and intermediate ) .
    def prepend(self,data):
    new_node = Node(data)
    p = self.head
    q = self.head
    if not self.head:
    new_node.next = new_node
    self.head = new_node
    while p.next != self.head:
    p = p.next

    new_node.next = q
    self.head = new_node
    p.next = new_node

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว +1

      Hi Murali. That's great to hear. In fact, I would recommend staying away from Udemy. They tend to steal and misattribute content from creators. If you would like more of a course form for the type of work I did, I worked with Educative to put together a data structures and algorithms course:
      www.educative.io/courses/ds-and-algorithms-in-python
      Regarding your code, I took a quick look and don't see anything terribly wrong with it. Hope that helps!

    • @muraliperi7669
      @muraliperi7669 4 ปีที่แล้ว +1

      @@LucidProgramming Thank you Lucid 😊♥️

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว +1

      @@muraliperi7669 Of course!

  • @ZhuYiting612
    @ZhuYiting612 4 ปีที่แล้ว

    Hi Lucid, it seems to me that we can reuse the same code of append for prepend except that we move the head to the new node at the end in prepend(), correct?

  • @HoanNguyen-fc8vb
    @HoanNguyen-fc8vb 4 ปีที่แล้ว +1

    Very nice tutorial. Thank you very much!

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!

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

    In the prepend step, why didn't we make the new_node to point the self.head before making the new node into the self head? Like,
    cur.nex=new_node
    new_node.next= self.head
    self.head=new_node

  • @rayj1232
    @rayj1232 4 ปีที่แล้ว +1

    Excellent video 10/10

  • @deni9264
    @deni9264 4 ปีที่แล้ว

    To maintain O(1) for prepending, I initialise with a "tail_node" variable that stores the address of the original head of the list. By doing so, each time I push a new node - which becomes the new head- that "tail_node" will point to the new node. Thus, the list does not require traversal

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว +1

      Nice. You lose a little on space but gain in time. Thanks for sharing!

    • @deni9264
      @deni9264 4 ปีที่แล้ว +2

      @@LucidProgramming Thanks. Yeah i think the trade-off is worth it for a large lists. I have also learnt alot from your videos

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      @@deni9264 Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!

  • @jashjasani5045
    @jashjasani5045 5 ปีที่แล้ว

    How is Node class accessed if it is written outside of the Circular Linked list class? Please explain.

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

    during the execution of WHILE loop I am getting an error 'NoneType' object has no attribute 'ref'

  • @Momo-bb2fn
    @Momo-bb2fn 4 ปีที่แล้ว +1

    Why would we make a prepend and append with a circular linked list? The fact that it's only a singly linked list means that iterating from the tail will actually make it go through one more iteration then it has to, no?

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      Your first statement contradicts your second. A circular linked list is not a singly linked list.

    • @Momo-bb2fn
      @Momo-bb2fn 4 ปีที่แล้ว +1

      LucidProgramming firstly, thanks for answering! Secondly, I believe your implementation is singly linked. At 1:23 and 1:45 you mention that the classes are identical to singly linked list, in other words, a ‘previous’ reference was never made, meaning it can only iterate forward. Even so, is there a difference between prepend and append in a doubly linked circular list? It would only shift the head by 1, but, since its doubly linked, we can get to either node next to the head with .next or .previous.

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      @@Momo-bb2fn But a circular linked list is not a singly linked list. If you look at the classes I have in both videos for both separate topics, you'll see that their core implementation is different as they are different objects.

  • @aayanmalhotra4157
    @aayanmalhotra4157 4 ปีที่แล้ว

    Heyyy thanks for all these videos but can you tell which programming language is best for implementation and efficiency of pointers as there is no term as pointers in python and what would be better to learn for placement exams from the university.

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว +1

      Python has pointers.

    • @aayanmalhotra4157
      @aayanmalhotra4157 4 ปีที่แล้ว

      @@LucidProgramming realpython.com/pointers-in-python/ I referred this can you please elaborate

  • @muhammadsalisabdulsalam4016
    @muhammadsalisabdulsalam4016 5 ปีที่แล้ว +1

    This was really helpful. Could you please add a delete function video?. Thanks

    • @LucidProgramming
      @LucidProgramming  5 ปีที่แล้ว +1

      I do take requests on my patreon page, so if there is a topic or video you would like to see made, be sure to put in a request. Thanks again!

  • @bishaladhikari8691
    @bishaladhikari8691 4 ปีที่แล้ว

    I can’t even begin to explain how this video meant to me.
    But I have a little problem in 4:23:
    self.head.next =self.head
    why we need to point self.head to itself in circular linked list

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      Because that's the way in which we define the data structure. The head must point to the tail to give it the "circular" structure.

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

    Why do prepend in O(n) by creating a new node in front of self.head? instead you can create the new node between self.head and self.head.next and then simply swap the data. This allows the operation to be done in O(1) because the 'tail' node's .next does not need to be swapped

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

    Hello sir please bring a video on merging of two circular linked lists in python I really need your help . I need this for my project in next 2 days I have to submit

  • @oldardinugraha8107
    @oldardinugraha8107 5 ปีที่แล้ว

    what is the different of append in linked list and circular linked list?

    • @LucidProgramming
      @LucidProgramming  5 ปีที่แล้ว

      They both add an element to the end of the list.

  • @qc4257
    @qc4257 4 ปีที่แล้ว

    nice vim skills :)

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

    getting attribute error at -------> while cur.next != self.head

  • @abhishekgarg7181
    @abhishekgarg7181 4 ปีที่แล้ว

    def prepend(self,data):
    newnode=Node(data)
    if not self.head:
    self.head=newnode
    self.head.next=self.head
    else:
    newnode.next=self.head
    self.head=newnode
    why it is going to infinite loop??

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      Did you make sure you code matches mine?

  • @sudharshan3863
    @sudharshan3863 5 ปีที่แล้ว

    While cur.next!= self.head:
    Error: Nontype object has no attribute 'next'

    • @LucidProgramming
      @LucidProgramming  5 ปีที่แล้ว

      Did you check the GitHub account to compare your code against mine?

    • @Worldnme
      @Worldnme 5 ปีที่แล้ว

      did you fix this error yet ?

    • @LucidProgramming
      @LucidProgramming  5 ปีที่แล้ว

      @@Worldnme Did you do what I suggested in the above comment?

  • @matrixcode4347
    @matrixcode4347 4 ปีที่แล้ว

    if we add "self.head=new_node" in append function we will get code for prepend:)

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว

      Is that true? I"m not sure that's quite right.

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

    There's a bug in your code.
    you need to return here
    def append(self, data):
    if not self.head:
    self.head = Node(data)
    self.head.next = self.head
    return
    otherwise, it'll be stuck in infinite loop

  • @tejaskhandare9270
    @tejaskhandare9270 4 ปีที่แล้ว

    (self.data = data_) Why this _ is used
    Anyone?

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว +1

      That is just vim being vim. There's no character actually there.

    • @tejaskhandare9270
      @tejaskhandare9270 4 ปีที่แล้ว +1

      @@LucidProgramming Thanks sir!
      Much appreciate ur efforts n support

    • @LucidProgramming
      @LucidProgramming  4 ปีที่แล้ว +1

      @@tejaskhandare9270 Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!

  • @malikcampbell4758
    @malikcampbell4758 6 ปีที่แล้ว

    TOO FAST

    • @LucidProgramming
      @LucidProgramming  6 ปีที่แล้ว

      Hi Malik. Sorry to hear it was too fast. Indeed, I will try to factor this into future videos. You can always try to use TH-cam's built-in feature to slow down the playback speed. Hope that helps, cheers!

    • @malikcampbell4758
      @malikcampbell4758 6 ปีที่แล้ว +1

      Thanks for the reply, and sorry for the use of caps (late night homework can be mentally taxing). Looking forward to future videos.

    • @LucidProgramming
      @LucidProgramming  6 ปีที่แล้ว +2

      No problem, Malik. Trust me, I've been there plenty of times! I hope my other videos can help with your current homework, and feel free to reach out to my email in the "About" section if you're still struggling. I do offer tutoring via Google Hangouts, and we discuss that if you think that would be helpful to you. In any case, thanks for your comment, and I still do appreciate the feedback, positive or negative. I'm all about trying to make the content better, so it's helpful in any case. Cheers.