Doubly Linked List Code

WilliamFiset · Beginner ·⚡ Algorithms & Data Structures ·9y ago

Key Takeaways

Implementation of a doubly linked list in Java, covering methods such as add, remove, and peek, with explanations and example code.

Full Transcript

all right time to look at some doubly linked list source code this is part two of two in the linked list Series so the link for the source code can be found on GitHub github.com William SL data- structures please star this repository if you find the source code helpful so that others may also find it and also make sure you watch the first part of the link list series before continuing here we are in the source code we're looking at the implementation of a doubly linked list in Java so first notice that I have declared a few instance variables so we are keeping track of the size of the link list as well as what the head and the tail currently are to begin with the head and the tail are null meaning link list is empty furthermore we will be using this internal node class quite excessively because it contains the data for our nodes and it also contains previous and next pointers for each node since this is a doubly linked list so we have one Constructor for the node namely the data and the previous and the next pointers themselves we need both otherwise we can't do much so this first method I have declared here here is to clear the list it does so in linear Time by going through all the elements and deallocating them one at a time it deallocates them by setting them equal to null so we start a traverser at the head we Loop while the the traverser is not equal to null meaning there are still elements in the list and then we do our deallocation business and at the end we set the size equal to zero and reset the head and the tail perfect these size and Mt methods are pretty self-explanatory get the size and check if uh the size of our link list is empty okay so here I have a public method to add an element by default we're going to append to the end of the link list or at the tail but I also support adding to the beginning of the link list so how we do this if this is the first element aka the list is empty then we set the head and the tail to be equal to the new node which has if you can see here both previous and next pointers set to null otherwise if the list is not empty then we say the head's previous point is equal to this new node and then we set the head the the head pointer to be whatever heads previous is so we back up the pointer in a sense and we also don't forget to increment the size a very similar thing is done when we want to add to the tail of the length list except we're moving the tail pointer around okay let's move to Peak so peing is just looking at either the element at the beginning of the link list or at the end of the link list and we throw a runtime exception if the list is empty because doesn't make sense to Peak an empty list okay now we get to the first more complex method which is remove first so this is if we want to remove the head of the link list so we can't do much if list is empty otherwise we get the data we extract the data at the head and then move the head pointer forward we decrease the size by one so if the list is empty we set the tail to be null as well so both the head and the tail are now null otherwise we uh deallocate the memory of the previous node that we just removed this is a especially important if you're in C or C++ make sure to free or delete your pointers then at the end we return the data a very similar thing is done for last except we're using the tail this time to uh remove from the tail of the link list and not the head okay and here's a me a generic method to remove an arbitrary node remark that I set this to private because the node class itself is private so the user shouldn't have access to the node that's just something we're using internally inside the link list data structure to manage the list so if the node that we're removing is either at the head of the tail detect that and call our methods either remove first or remove last other wise we know we're somewhere in the middle of the length list and if we are we make the pointers adjacent to the to our current node equal to each other so we're effectively skipping over the current node and then of course don't forget to clean up your memory and return the data so we have to temporarily store the data of course before we delete the node otherwise we've deleted the node and the data is already gone now suppose we want to remove a note at a particular index in our linked list yes we can do this even though our nodes are not explicitly indexed we can pretend that they are so first check that the index is valid otherwise throw in a legal argument exception so here we are trying to be a bit smarter than just naively going through the link list either we're going start searching from the front of the link list to find our index or from the back depending on if the index is closer to the front or to the back although this method remains linear so for the remove method we want to be able to remove an arbitrary value from our linked list which is object so we're going to also support searching for n values in case someone decided that the value of the node should be null that's fine so we check that special case otherwise we Traverse through the link list until we find a null element and then remove that node and return true we return true if we actually found the element we want to remove otherwise we return false down here uh in this lse statement we search for the element we want to remove we use the equals method to check if we found the element if so remove that node and return true okay here we have a related method which is index of so this is not remove at an index or remove a value but get whatever Index this object is at Again Sport searching for null so even if our value is null just return the first place we find a null element so again Traverse the link last otherwise search for a non-null element and also increment the index as we go we can use the index of for our method contains to check if an element is contained within a link list because we return minus one if the element is not found something that's useful sometimes is to have an iterator for length list this is also trivial to implement just start a pointer Traverse at the head and Traverse until you reach the end notice I'm not checking for concurrent modification error but if you want to uh it's pretty easy to do that and lastly at the very bottom we have um two the two string method to print a string or to get rather a string representation of our linked list and that's it for the link list so pretty straightforward stuff but there's a lot of pointers flying around so if you're confused about a particular part of the algorithm look at it in more details or comment below I'll get to you when I have time

Original Description

Related videos: Linked list intro: https://youtu.be/-Yn5DU0_-lw Linked list code: https://youtu.be/m-8ZBO2ywaU Data Structures Source Code: https://github.com/williamfiset/algorithms My website: http://www.williamfiset.com
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from WilliamFiset · WilliamFiset · 33 of 60

1 JES Image Manipulation - 2 - Installation
JES Image Manipulation - 2 - Installation
WilliamFiset
2 JES Image Manipulation - 3 - User Interface
JES Image Manipulation - 3 - User Interface
WilliamFiset
3 JES Image Manipulation - 5 - Negative
JES Image Manipulation - 5 - Negative
WilliamFiset
4 JES Image Manipulation - 6 - Black & White
JES Image Manipulation - 6 - Black & White
WilliamFiset
5 JES Image Manipulation - 4 - Grayscale
JES Image Manipulation - 4 - Grayscale
WilliamFiset
6 JES Image Manipulation - 8 - Blur
JES Image Manipulation - 8 - Blur
WilliamFiset
7 JES Image Manipulation - 7 - Edge Detection
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
8 JES Image Manipulation - 9 - Blend
JES Image Manipulation - 9 - Blend
WilliamFiset
9 JES Image Manipulation - 10 - Matte
JES Image Manipulation - 10 - Matte
WilliamFiset
10 JES Image Manipulation - 13 - Rotate90
JES Image Manipulation - 13 - Rotate90
WilliamFiset
11 JES Image Manipulation - 12 - Mirroring Picture
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
12 JES Image Manipulation - 11  - Crop Image
JES Image Manipulation - 11 - Crop Image
WilliamFiset
13 JES Image Manipulation - 14 - Stretch picture
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
14 Java Fractal Explorer [6/8]
Java Fractal Explorer [6/8]
WilliamFiset
15 Java Fractal Explorer [4/8]
Java Fractal Explorer [4/8]
WilliamFiset
16 Java Fractal Explorer [8/8]
Java Fractal Explorer [8/8]
WilliamFiset
17 Java Fractal Explorer [5/8]
Java Fractal Explorer [5/8]
WilliamFiset
18 Java Fractal Explorer [2/8]
Java Fractal Explorer [2/8]
WilliamFiset
19 Java Fractal Explorer [7/8]
Java Fractal Explorer [7/8]
WilliamFiset
20 Java Fractal Explorer [1/8]
Java Fractal Explorer [1/8]
WilliamFiset
21 Java Fractal Explorer [3/8]
Java Fractal Explorer [3/8]
WilliamFiset
22 Introduction [Programming Competition Problems]
Introduction [Programming Competition Problems]
WilliamFiset
23 String Manipulation 1 [Programming Competition Problems]
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
24 String Manipulation 2 [Programming Competition Problems]
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
25 Graph Theory 1 [Programming Competition Problems]
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
26 Logic 1 [Programming Competition Problems]
Logic 1 [Programming Competition Problems]
WilliamFiset
27 Grid Problems 1 [Programming Competition Problems]
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
28 Dynamic Programming 1 [Programming Competition Problems]
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
29 Introduction to Big-O
Introduction to Big-O
WilliamFiset
30 Dynamic and Static Arrays
Dynamic and Static Arrays
WilliamFiset
31 Dynamic Array Code
Dynamic Array Code
WilliamFiset
32 Linked Lists Introduction
Linked Lists Introduction
WilliamFiset
Doubly Linked List Code
Doubly Linked List Code
WilliamFiset
34 Stack Introduction
Stack Introduction
WilliamFiset
35 Stack Implementation
Stack Implementation
WilliamFiset
36 Stack Code
Stack Code
WilliamFiset
37 Queue Introduction
Queue Introduction
WilliamFiset
38 Queue Implementation
Queue Implementation
WilliamFiset
39 Queue Code
Queue Code
WilliamFiset
40 Priority Queue Introduction
Priority Queue Introduction
WilliamFiset
41 Priority Queue Min Heaps and Max Heaps
Priority Queue Min Heaps and Max Heaps
WilliamFiset
42 Priority Queue Inserting Elements
Priority Queue Inserting Elements
WilliamFiset
43 Priority Queue Removing Elements
Priority Queue Removing Elements
WilliamFiset
44 Priority Queue Code
Priority Queue Code
WilliamFiset
45 Union Find Introduction
Union Find Introduction
WilliamFiset
46 Union Find Kruskal's Algorithm
Union Find Kruskal's Algorithm
WilliamFiset
47 Union Find - Union and Find Operations
Union Find - Union and Find Operations
WilliamFiset
48 Union Find Path Compression
Union Find Path Compression
WilliamFiset
49 Union Find Code
Union Find Code
WilliamFiset
50 Binary Search Tree Introduction
Binary Search Tree Introduction
WilliamFiset
51 Binary Search Tree Insertion
Binary Search Tree Insertion
WilliamFiset
52 Binary Search Tree Removal
Binary Search Tree Removal
WilliamFiset
53 Binary Search Tree Traversals
Binary Search Tree Traversals
WilliamFiset
54 Binary Search Tree Code
Binary Search Tree Code
WilliamFiset
55 Fenwick Tree range queries
Fenwick Tree range queries
WilliamFiset
56 Fenwick Tree point updates
Fenwick Tree point updates
WilliamFiset
57 Fenwick Tree construction
Fenwick Tree construction
WilliamFiset
58 Fenwick tree source code
Fenwick tree source code
WilliamFiset
59 Hash table hash function
Hash table hash function
WilliamFiset
60 Hash table separate chaining
Hash table separate chaining
WilliamFiset

This video implements a doubly linked list in Java, covering various methods and their implementations. It provides a clear understanding of how a doubly linked list works and how to use it in programming.

Key Takeaways
  1. Create a Node class to represent each element in the linked list
  2. Implement the add method to add elements to the list
  3. Implement the remove method to remove elements from the list
  4. Implement the peek method to view elements at the beginning or end of the list
  5. Use the provided GitHub repository for reference
💡 Understanding how to implement a doubly linked list in Java can help improve programming skills and data structure knowledge.

Related AI Lessons

Bloom Filters, Explained Properly
Learn how Bloom filters work and their benefits, including tiny memory and blazing speed, in exchange for potential false positives.
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Learn how prefix sums enable instant range queries in arrays, boosting performance in various applications
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
A simple math question can destroy a developer's interview, highlighting the importance of being prepared for unexpected questions
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Learn to remove duplicates from a sorted array using the two pointers technique, improving from brute force to optimized solutions
Medium · Python
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →