Circular Linked List Tutorial - Why Use a Circular List?

Tech With Tim · Beginner ·⚡ Algorithms & Data Structures ·6y ago

Key Takeaways

This video tutorial covers the basics of circular linked lists, including their structure, traversal, and operations such as adding and removing nodes, with a focus on the advantages of using circular lists over other data structures like doubly linked lists. The tutorial provides step-by-step instructions on how to implement a circular linked list, including modifying the while loop to handle the circular nature of the list and updating the tail node to add nodes to the end of the list.

Full Transcript

alright so in the previous video we talked about singly linked list now these are great and we just talked about the advantages of them over arrays and other types of data structures now I want to introduce the concept here of circular lists and this is going to apply to doubly linked lists which we'll talk about in the next video as well now I want you to think why we would use a circular list and what a circular list even is now what I mean by circular is that this last node here rather than pointing to null which it's been doing before because there's no next note after it it actually points back to the head node of our list so what actually happens here is that this next node is always pointing back to the head now before you continue on with this video I seriously urge you to just take a second and think why we would even want to do this can this offer speed be a thing can it make it easier to access things why would we do this and what way we need to modify even in the node or in the SSL clasp to make this worthwhile and then I'm gonna explain the answer in just a second all right so I hope you guys have thought about this now what I'm gonna do is just modify this sll class here and hopefully what I start doing will make sense to you guys but I want you to kind of figure it out on your own so this here is called the head of our list right and this is the node that right now we're storing in sll all we need is access to this head node and we can successfully traverse all of the different nodes until we reach the end node now immediately some of you might have started thinking well if we have this last node our traversal from the previous video isn't gonna work because in that traversal we had a while loop and we said something like wow current dot next doesn't equal no or while current so if you get rid of that dog next doesn't equal no but now we're never gonna have a null value because this last point here is always gonna be pointing to the head so this is why I'm gonna make a modification to my class and hopefully you guys can understand why in just a second and then I will we'll see how we can change this while loop to do a proper traversal of a circular list so what I'm gonna do now is rather than keeping track of what we called the head node I'm actually gonna keep track of the tail node in my SS l-class so what I want to do is I'm actually gonna have this tail node being pointed to by this tail attribute and rather than carrying it with the head which I don't care about anymore I'm just gonna carry with the tail now the reason I'm gonna do this is because if I have reference to the tail node that means that I actually have reference to the head node so by having reference to this this one here I also have reference to this now why is that good why wouldn't I just have reference to the head node well if I have reference to the front and the back of the list or the beginning or the end of the list that means I can perform operations on the beginning and the end of the list very quickly and that is something that is awesome to be able to do because previously if we wanted to say add something to the end of our list here well we we need to reverse the entire list to be able to do that and that would take o of n time but now if we have reference to this tail node since it points back to the head node if I want to add something at the beginning all I need to do and I'm just gonna erase a few things here to make this a little bit cleaner while I do this all I need to do if I want to add a new node at the beginning is make this pointer here I'm just going to erase it point to that new node so let's say I want to add a new node I'm just gonna draw it small so we can actually see it here say that's five and this is its next property well what I do is I set this node to be equal to tail dot next which is actually going to be equal to the head node so I pointed here so the next property points to there and then I change what we originally was pointing from there to point to this new node so I say you know what this next property here we're gonna point that to this new node and now I've successfully added something at the beginning of the list but now what if I want to add something to the end of the list so let's reverse all this and let's change and now see how you add something to the end so let's pretend this is circular right now and I want to add something to the end well I have reference this tail node I know where this tail node is so all I need to do is add a new node let's say we'll add it down here we'll call this one maybe value nine and I set its next property to the first node in my list so I set it to the head node or in this case the tail dot next right so tail dot next that's what I set it equal to the next property so this points over here to this and now all I do is I change this pointer on the end node to point to this new node which is gonna be the end node in our list so I know this looks kind of confusing now but anyways that goes to there so now this one here is now our new tail reference and we now point tail to here this new node and now since this one points to the head node it works the exact same and if I want to add something after it that's how I do it I simply just repeat that process if I want to add something before well I have access to this head node so I can add a new node pointing to that and then point the new node to this head node right and that's how we do it with a circular list now this offers a ton of advantages because that means that whenever we want to add something to the end of the list we no longer need to traverse the entire list and that is the reason that we use a circular lists are simply adding that one extra pointer that goes from the tail to the head and keeping reference of the tail instead of our head we're able to get that massive speed advantage while still using a singly linked list and not using a ton of memory in our computer which is one of the disadvantages of a doubly linked list which we'll talk about later so anyways now that we've done that let's kind of talk about how we can implement some of these methods with a little bit of pseudocode in case anyone's confused so I realized in the last video I was kind of cutting off some of the code on my webcam in the bottom right hand corner so I'll try to make sure it doesn't cut off here but if you haven't noticed my handwriting is absolutely horrible so anyways let's now try to write a method that could add something to the beginning of our list so let's say we want to add first that's the method we want to write now for our sll class well we have this tail node so what should we start by doing well we don't need to traverse the list because all we need to do is just change around a few pointers so what we're gonna start by doing is creating a new node so we'll say like N equals node and we'll say n dot value equals I don't know let's set it equal to a value of five and then all we can do is say n dot next is equal to in this case it's gonna be tail dot next right and I'll talk about that in a second but tail dot next because tail dot next is this head node and we're adding to the beginning we the next property of our new head node to be the the current head node right the first thing that's actually in our list right now so we said this next property of this node to go to here my name is hope that makes sense all right so now that we've done that what we can do is change this last node so this next property here on this one to point to our new node and then that's actually all we need to do and we don't even need to mess with the tail so now we're gonna say is tail dot next equals n and now what we've successfully done is we've slotted in a new node value v we pointed it over here to seven and that we've changed this pointer so scratch that off to go to this new node and this now will be our head node and since we keep track of the tail and tail dot next as this makes sense everything works as planned and that is how we write the pseudocode to add something to the beginning of the list all right so now what about the end and this is the advantage that we're gaining because we already knew kind of how to do that before but now we're gonna have to just change a little bit different things to add to the end here to make it all make sense so let me get this eraser out here and get rid of everything all right so let's erase I didn't want to do that let's get the stroke eraser okay let's remove actually now let's go back to the pointer okay so what we want to do now sorry about that is add a new node to the end so how do we do that well what we need to do is we need to change a few pointers around so firstly we need to make sure that we don't lose this head node so that's an important thing when we add this new node in and we point to it or we don't point to it we got to make sure we don't lose this head node so what I'm going to start by doing is creating a new node so I'm gonna say N equals node and then we'll say n dot value and maybe this time it equals negative one all right now the first thing we do so we've just created this new node I'll try to draw it so you guys can actually see it as a value of negative one and it has this next player we're gonna set its next pointer equal to the head node to start so that we don't lose this head node so we're gonna say n dot next equals tail dot next so that means now we have a pointer going here to the head node because tail dot next is that so this is pointing there now what we can do is well we can change the pointer of tail tail dot next to go to this new node so now we'll say tail dot next equals n all right so now let's go here let's go to stroke eraser and we'll erase this pointer and now what we've successfully done is we've changed the tail to point two here as our next node which is gonna be the last node in our list all right awesome and now what's the last step we need to do what we need to say that this is now gonna be the new tail because this has the pointer to the head and it's gonna be the last element so what we do for that is we say tail equals n and that's all we need to do to set this and add that to the end of the list and that is the massive advantage of circle circular lists is that you're able to access both the front and the back element which means you can apply operations at the front and end of the list in the same time complexity of constant time now the only thing to note here is that if removing an element from the end of the list it still takes o n time now the reason that happens is because if you think about this say we want to remove this last element here which is actually the tail element right this is our tail so I'll just write tail if I want to remove this what I need to do is I need to find the node before it that points to it and change its pointer to go to the head node so to do that I actually need to traverse the entire list and that's because we only have this linked forwards which means that I can't by having the tail element access the element before it which means I can't access that pointer so if I want to remove an element I don't know why that's happening anyways if I want to remove an element from the end of the list or I want to remove the tail element that still takes o n time but if I want to remove an element from the beginning of the list that isn't going to take Oh n time because all I need to do to remove that is change the pointer here to be the element that it's pointing to which we can do because we have access to that front element as well as this tail so anyways that has been it for circular lists I hope you guys learned a little bit in the next video we will talk about doubly linked lists which are more complex and they have their own advantages over singly linked lists you guys have any other data structures you'd like to hear below please let me know and with that being said make sure to hit the like button and subscribe to the channel for more videos

Original Description

This data structure tutorial focuses on the use of circular lists, especially circular linked lists. A circular structure allows for significant performance abilities and a very nice way to access both the front and end of the list. What is meant by a circular list is that the tail node does not point to null. Instead it points back to the head. Playlist: https://www.youtube.com/watch?v=1j2gWyY5CK4&list=PLzMcBGfZo4-la-N5JkwKenICUdu93X_eC&index=1 ◾◾◾◾◾ 💻 Enroll in The Fundamentals of Programming w/ Python https://tech-with-tim.teachable.com/p... 📸 Instagram: https://www.instagram.com/tech_with_tim 🌎 Website https://techwithtim.net 📱 Twitter: https://twitter.com/TechWithTimm ⭐ Discord: https://discord.gg/pr2k55t 📝 LinkedIn: https://www.linkedin.com/in/tim-rusci... 📂 GitHub: https://github.com/techwithtim 🔊 Podcast: https://anchor.fm/tech-with-tim 💵 One-Time Donations: https://www.paypal.com/donate/?token=... 💰 Patreon: https://www.patreon.com/techwithtim ◾◾◾◾◾◾ ⚡ Please leave a LIKE and SUBSCRIBE for more content! ⚡ Tags: - Tech With Tim - Linked List - Circular Linked List - Circular List - Circular List Tutorial - Why use a circular list #CircularList #DataStructures #LinkedList
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This tutorial teaches the basics of circular linked lists, including their structure, traversal, and operations, and provides step-by-step instructions on how to implement a circular linked list. By the end of this tutorial, viewers will be able to implement a circular linked list and perform common operations on it. The tutorial also discusses the advantages of using circular lists over other data structures like doubly linked lists.

Key Takeaways
  1. Modify the SLL class to keep track of the tail node instead of the head node
  2. Change the while loop to traverse the circular linked list
  3. Add a new node at the beginning of the list by making the head node point to the new node
  4. Create a new node and set its next property to the current head node
  5. Change the next property of the last node to point to the new node
  6. Update the tail to point to the new node to update the last element in the list
  7. Traverse the entire list to remove an element from the end of the list
💡 Circular linked lists have a significant performance advantage over doubly linked lists, especially when it comes to adding and removing nodes from the beginning and end of the list.

Related Reads

📰
O(N) Manacher's Algorithm with Mirror Boundary Optimization
Learn to optimize palindrome detection using Manacher's Algorithm with mirror boundary optimization, reducing time complexity to O(N)
Dev.to · Dipaditya Das
📰
Building a Power Grid Inside Minecraft with BFS Algorithms
Learn to build a power grid inside Minecraft using BFS algorithms and understand its relevance to cloud services
Dev.to · Carlos Cortez 🇵🇪 [AWS Hero]
📰
The Run-Length Encoding Trick: How Simple Strings Get Compressed
Learn how Run-Length Encoding (RLE) compresses simple strings, a fundamental technique in programming and data compression, and apply it to optimize storage and transmission of data
Medium · Programming
📰
75 Days of Leetcode — Day 4: #238 — Product of Array Except Self
Solve the Product of Array Except Self problem on LeetCode to improve coding skills and learn array manipulation techniques
Medium · AI
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →