Demystifying Linked list Data Structure
A Beginner's Guide to Understanding the Fundamentals of Linked list
Introduction
A linked list is a data structure that is made up of a series of nodes. Each node has two main parts: a piece of data (also called an element or item) and a reference to the next node in the list. The reference to the next node is called a "link." The first node in the list is called the head, and the last node is called the tail.
A node in a linked list is a basic unit of data that stores a reference to an object and a reference to the next node in the list. A node has two main parts:
Data: This is the actual piece of data (e.g., a number, a string, etc.) that is stored in the node.
Link: This is a reference to the next node in the list. The link is like a pointer pointing to the next node's memory address.
Imagine that you have a bunch of boxes that are all connected in a line. Each box contains a small item (e.g., a toy, a piece of candy, etc.), and each box also has a string attached to it. The box itself is like a node in a linked list, and the string is like the link between nodes. The item inside the box is like the data stored in the node.
In a linked list, each node stores a reference to an object and a reference to the next node in the list. The reference to the next node is called a "link," allowing the nodes to be connected in a sequence. The first node in the list is called the head, and the last node is called the tail.
Linked lists are useful because they allow you to add and remove items from the list easily. If you want to add a new item to the list, you can simply create a new box and attach it to the end of the list using the string. If you want to remove an item from the list, you can simply cut the string that is connecting it to the rest of the list.
TYPES OF LINKED LIST (Singly-linked and Doubly-Linked)
A singly-linked list is a type of linked list where each node has a reference to the next node in the list, but not to the previous node. This means that you can only traverse a singly-linked list in one direction, starting from the head and moving toward the tail.
A doubly-linked list is a type of linked list where each node has references to both the next node and the previous node. This means that you can traverse a doubly-linked list in both directions, starting from the head and moving towards the tail, or starting from the tail and moving towards the head.
To visualize the difference between these two types of linked lists, imagine the same scenario with the boxes and strings that I described earlier. In a singly-linked list, each box would only have one string attached to it (the one that is connected to the next box in the list). In a doubly-linked list, each box would have two strings attached to it (one that is connected to the next box and one that is connected to the previous box).
Singly-linked lists are simpler and require less memory than doubly-linked lists, but they are not as flexible because you can only traverse them in one direction. Doubly-linked lists are more complex, but they offer more functionality because you can traverse them in both directions.
There are a few key differences between arrays and linked lists:
Size: Arrays have a fixed size, which means that you need to know how many items you are going to store in the array before you create it. Linked lists are dynamic, which means that they can grow or shrink as needed.
Insertion and deletion: Inserting or deleting an item from the middle of an array can be time-consuming, because you may need to shift a large number of items to make room for the new item or to close the gap left by the deleted item. Linked lists are more efficient at inserting and deleting items because you can simply update the references of a few nodes to add or remove an item.
Access time: Arrays offer fast access to any item using the index, but linked lists do not. To access a specific item in a linked list, you need to start at the head of the list and follow the links one by one until you reach the desired item. This can be slower than accessing an item in an array, especially if the item is near the end of the list.
Operations on Linked Lists
Insertion: Inserting an item into a linked list means adding a new node to the list that contains the item. To insert an item at the beginning of the list, you can create a new node and make it the head of the list. To insert an item at the end of the list, you can simply add the new node after the current tail of the list. To insert an item at a specific position in the list, you can create a new node and attach it to the node that comes before it and the node that comes after it.
Deletion: Deleting an item from a linked list means removing the node that contains the item from the list. To delete an item from the beginning of the list, you can simply make the second node the new head of the list. To delete an item from the end of the list, you can simply remove the link between the second-to-last node and the last node. To delete an item from a specific position in the list, you can simply remove the link between the node before and the node after the node that you want to delete.
Traversal: Traversing a linked list means visiting each node in the list and performing some action on the data contained in the node. To traverse a linked list, you can start at the head of the list and follow the links from one node to the next until you reach the end of the list. While traversing the list, you can perform any operation that you want on the data in each node, such as printing it out or adding it to a sum.
How Linked Lists can be used to solve specific problems
Storing a large amount of data: Linked lists can be used to store a large amount of data that does not fit in memory all at once. For example, a linked list can be used to store the data from a very large file that cannot be read into memory all at once. The linked list can be used to store the data from each chunk of the file as it is read, and the chunks can be processed one at a time.
Implementing a stack: A stack is a data structure that allows items to be added and removed in a last-in, first-out (LIFO) order. A linked list can be used to implement a stack because it allows items to be added and removed easily at the head of the list.
Implementing a queue: A queue is a data structure that allows items to be added and removed in a first-in, first-out (FIFO) order. A linked list can be used to implement a queue because it allows items to be added at the tail of the list and removed from the head of the list.
Implementing a symbol table: A symbol table is a data structure that is used to store and retrieve values based on a key. A linked list can be used to implement a symbol table by storing key-value pairs in the nodes of the list and using the keys to search for the corresponding values.
Conclusion
Here is a list of the key points from this post:
A linked list is a data structure made up of nodes that contain data and a reference to the next node in the list.
The first node in the list is called the head, and the last node is called the tail.
There are two types of linked lists: singly-linked lists and doubly-linked lists.
Singly-linked lists have a reference to the next node only, while doubly-linked lists have references to both the next and previous nodes.
Linked lists are dynamic and allow for efficient insertion and deletion of items.
Linked lists have slower access times compared to arrays, which have fixed sizes and offer fast access to items using an index.
Linked lists can be used in a variety of applications, including creating stacks and queues, implementing graphs, and representing sparse matrices.