Hello there! Today, we will explore Linked Lists, a core data structure crucial for organized data management and establishing relationships between data.
We will mark some essential milestones: an introduction to Linked Lists, their real-world applications, their implementation in C#
, and the different operations you can perform on them.
By the end of this lesson, you will be well-equipped to implement and operate Linked Lists using C#
. Let's get started!
A Linked List is a linear data structure similar to arrays. But, unlike arrays, they are not stored in contiguous memory locations. Each element in a Linked List is part of a node
. A node
comprises data and a reference (or link) to the next node
in the sequence. This structure facilitates efficient insertions and deletions.
The head
is also an essential concept in Linked Lists. It is the first node
in the list and serves as a reference to the entire list. The head
is a null
reference if the Linked List is empty.
Singly linked lists come up quite often in coding interviews. Interviewers from tech giants, start-ups, and just about every company testing your coding abilities will pose challenges based on this concept.
Here's another interesting point: While singly linked lists might not be extensively used in real-world applications, they form the foundational knowledge for understanding doubly linked lists, which are indeed quite common. A singly linked list contains nodes with a single link pointing to the next node, whereas a doubly linked list has nodes with links to both the next and the previous nodes.
To begin implementing Linked Lists, we first need to understand the structure of a node
, the building block of a Linked List. In C#
, we'll create a class to serve as a blueprint for a node
.
A Node
class mainly consists of data
(the data you want to store) and next
(the reference to the next node
). In our case, we'll create a Node
class to store integer data, with a constructor to initialize the node.
C#1class Node 2{ 3 public int Data; 4 public Node Next; 5 6 public Node(int data) 7 { 8 Data = data; 9 Next = null; 10 } 11}
Fantastic! You now know how to set up a node
in a Linked List.
In this section, we'll learn how to add a new node at the end of our Linked List. We'll implement an Append
method in our LinkedList
class for this.
C#1class Node 2{ 3 public int Data; 4 public Node Next; 5 6 public Node(int data) 7 { 8 Data = data; 9 Next = null; 10 } 11} 12 13public class LinkedList 14{ 15 private Node _head; 16 17 public void Append(int data) 18 { 19 Node node = new Node(data); 20 21 if (_head == null) 22 { 23 _head = node; 24 } 25 else 26 { 27 Node last = _head; 28 while (last.Next != null) 29 { 30 last = last.Next; 31 } 32 last.Next = node; 33 } 34 } 35}
The code checks if _head
is null
, which would be the case for an empty list. If that's true, _head
is set to the new node, meaning this new node is the first and only node in the list. If the linked list is not empty (_head
is not null
), the code enters a while
loop, which runs as long as the Next
attribute of the current node it's examining is not null
(i.e., there are still nodes in the list). This loop is used to navigate to the end of the list. The new node is then appended after the last node in the list.
Now, what if we want to add a new node at the beginning of our list? We'll write a method AddFirst
to achieve this operation.
C#1public void AddFirst(int data) 2{ 3 Node node = new Node(data); 4 5 if (_head != null) 6 { 7 node.Next = _head; 8 } 9 _head = node; 10}
It simply reassigns the head
.
Removing a node from a Linked List is also an essential functionality. We will add a Delete
method to our LinkedList
class to remove a node with a particular data value.
C#1public void Delete(int data) 2{ 3 if (_head == null) return; 4 5 if (_head.Data == data) 6 { 7 _head = _head.Next; 8 return; 9 } 10 11 Node current = _head; 12 while (current.Next != null) 13 { 14 if (current.Next.Data == data) 15 { 16 current.Next = current.Next.Next; 17 return; 18 } 19 current = current.Next; 20 } 21}
We traverse the list like in the Append
method, searching for a node with specific data. If found, it is removed from the list by retargeting the previous node to the node after the target.
While understanding the implementation of Linked Lists is great, it's equally crucial to comprehend the performance of our operations. This understanding generally comes through complexity analysis, examining the time (number of operations) and space (memory used) needed for an operation.
Here's a summary of the performance of particular operations in Linked Lists:
- Accessing an element: It has
O(n)
time complexity because, in the worst-case scenario, we'd have to traverse through the entire list. - Inserting or deleting a node: It's
O(1)
if we're adding or removing from the front of the list. However, if it's at the end or in the middle, it would beO(n)
because we'd have to traverse the list to find the position.
Great job sticking with it throughout this intriguing journey, from understanding the concept of Linked Lists to implementing them in C#
, exploring critical operations, and understanding their performance!
Up ahead, we have lined up some practice sessions. These sessions will provide you with real-time experience implementing and manipulating Linked Lists using C#
.