For working professionals
For fresh graduates
More
Data Structure Tutorial: Every…
1. Data Structure
2. Types of Linked Lists
3. Array vs Linked Lists in Data Structure
4. Stack vs. Queue Explained
5. Singly Linked List
6. Circular doubly linked list
7. Circular Linked List
8. Stack Implementation Using Array
9. Circular Queue in Data Structure
10. Dequeue in Data Structures
11. Bubble Sort Algorithm
12. Insertion Sort Algorithm
13. Shell Sort Algorithm
14. Radix Sort
15. Counting Sort Algorithm
16. Trees in Data Structure
17. Tree Traversal in Data Structure
18. Inorder Traversal
19. Optimal Binary Search Trees
20. AVL Tree
21. Red-Black Tree
22. B+ Tree in Data Structure
23. Expression Tree
24. Adjacency Matrix
25. Spanning Tree in Data Structure
26. Kruskal Algorithm
27. Prim's Algorithm in Data Structure
28. Bellman Ford Algorithm
29. Ford-Fulkerson Algorithm
30. Trie Data Structure
31. Floyd Warshall Algorithm
32. Rabin Karp Algorithm
33. What Is Dynamic Programming?
34. Longest Common Subsequence
35. Fractional Knapsack Problem
36. Greedy Algorithm
37. Longest Increasing Subsequence
38. Matrix Chain Multiplication
39. Subset Sum Problem
40. Backtracking Algorithm
41. Huffman Coding Algorithm
42. Tower of Hanoi
43. Stack vs Heap
44. Asymptotic Analysis
45. Binomial Distribution
46. Coin Change Problem
47. Fibonacci Heap
48. Skip List in Data Structure
49. Sparse Matrix
50. Splay Tree
51. Queue in Data Structure
52. Stack in Data Structure
53. Time and Space Complexity
54. Linked List in Data Structure
Now Reading
55. Stack And Queue: Roles & Functions
56. Doubly Linked List
57. Strongly Connected Components
58. Bucket Sort Algorithm
A linked list is a fundamental data structure that stores elements in a linear order. Unlike arrays, a linked list in data structure stores elements in nodes that link to the next element. It offers dynamic memory allocation and flexibility in data management. Imagine a chain where each link connects to the next; that's how linked lists function. They are helpful in situations where the size of the data set changes over time. This tutorial will walk you through the basics and show why linked lists are important in programming.
The many forms of linked lists in data structure serve different needs. The simplest form is the singly linked list. It contains nodes that point to the next node in the sequence. For complex linking, doubly linked lists allow navigation both forward and backward. The other one, Circularly linked lists, forms a loop and connects the last node back to the first, which is great for circular queues or buffering.
Thus, we will explore these types, talk about the creation of programs for linked lists in languages like C and Java, and discuss their applications. By the end of this tutorial, you will have a solid grasp of how to implement and use linked lists in your projects and enhance your data management skills.
The different forms of the linked lists, which serve different programming needs, are a crucial part of the data structures.
Code
typedef struct Node {
int data;
struct Node* next;
} Node;
Code
class Node {
int data;
Node prev;
Node next;
}
Code
typedef struct Node {
int data;
struct Node* next;
} Node;
Results: The next pointer of the last node would be set to the head of the list.
Code
class Node {
int data;
Node prev;
Node next;
}
Result: The last node's next would point to the head, and the head's previous would point to the last node.
A basic understanding of the language is required before the creation of a singly linked list in data structure program in C language. You need to know about the basics of nodes, pointers, and dynamic memory allocation. However, if you are a complete beginner, you can follow these steps to build your program.
Let's build a simple program step by step and focus on key operations such as the creation of a list, insertion of nodes, and display of the list.
First, we need to define what a node is. The nodes store an integer value and a pointer to the next node.
Code
#include <stdio.h>
#include <stdlib.h>
// A structure to represent a node
typedef struct Node {
int data;
struct Node* next;
} Node;
We will write a function that creates a new node with a given value and appends it to the end of the list, to insert nodes into our singly linked list.
Code
// Function to insert a node at the end of the list
void insertEnd(Node** head, int newData) {
// Create a new node and allocate memory
Node* newNode = (Node*) malloc(sizeof(Node));
Node* last = *head; // Used to traverse the list
newNode->data = newData; // Assign data to the new node
newNode->next = NULL; // The new node is going to be the last node, so make its next NULL
// If the list is empty, then make the new node as head
if (*head == NULL) {
*head = newNode;
return;
}
// Else traverse till the last node
while (last->next != NULL) {
last = last->next;
}
// Change the next of the last node
last->next = newNode;
}
We will iterate through each node and print its value until we reach the end of the list to display it.
Code
// Function to print the linked list
void printList(Node* node) {
while (node != NULL) {
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}
Now, let's use our functions in the main function to create a list, insert some values, and display them.
Code
int main() {
Node* head = NULL; // Start with an empty list
insertEnd(&head, 1);
insertEnd(&head, 2);
insertEnd(&head, 3);
insertEnd(&head, 4);
printf("Created Linked List: ");
printList(head);
return 0;
}
Created Linked List: 1 -> 2 -> 3 -> 4 -> NULL
The implementation of a linked list in Java involves the creation of a custom class for the nodes of the list and another class for the linked list itself. Here's a basic example of how to create a singly linked list in data structure with operations to add, print, and delete nodes. This will give you a basic understanding of how to work with linked lists in Java.
First, we define a Node class, which will represent each element in the list. This class has two parts: the data you want to store and a reference to the next node.
Code
class Node {
int data;
Node next;
// Constructor to create a new node
// Next is by default initialized as null
Node(int d) {
data = d;
next = null;
}
}
Linked List Class
Next, we create a LinkedList class to manage the nodes. This class will have a head reference point at the start of the list and functions to add, delete, and display nodes.
public class LinkedList {
Node head; // head of the list
// Method to insert a new node
public void insert(int data) {
// Create a new node with given data
Node newNode = new Node(data);
newNode.next = null;
// If the Linked List is empty, then make the new node as head
if (head == null) {
head = newNode;
} else {
// Else traverse till the last node and insert the new_node there
Node last = head;
while (last.next != null) {
last = last.next;
}
// Insert the new_node at last node
last.next = newNode;
}
}
// Method to print the LinkedList.
public void printList() {
Node currentNode = head;
System.out.print("LinkedList: ");
// Traverse through the LinkedList
while (currentNode != null) {
// Print the data at current node
System.out.print(currentNode.data + " ");
// Go to next node
currentNode = currentNode.next;
}
System.out.println();
}
// Method to delete a node in the LinkedList by KEY
public void deleteByKey(int key) {
// Store head node
Node temp = head, prev = null;
// If head node itself holds the key to be deleted
if (temp != null && temp.data == key) {
head = temp.next; // Changed head
return;
}
// Search for the key to be deleted, keep track of the previous node as we need to change temp.next
while (temp != null && temp.data != key) {
prev = temp;
temp = temp.next;
}
// If key was not present in linked list
if (temp == null) return;
// Unlink the node from linked list
prev.next = temp.next;
}
}
Finally, you can test the linked list in data structure by the creation of a LinkedList object, using its methods to manipulate the list.
public class Main {iterate
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.insert(1);
list.insert(2);
list.insert(3);
list.insert(4);
System.out.println("Created linked list is: ");
list.printList();
list.deleteByKey(3); // Delete node with data 3
System.out.println("Linked List after Deletion of 3:");
list.printList();
}
}
This example shows the basics of how to implement a linked list in Java. You can also add more functionalities, like the insertion of a specific index, reversing the list, or search elements.
The creation of a circular linked list in data structure involves setting up a linked list where the last node points back to the first node, which further creates a loop. This setup is useful for applications that require cyclic traversals, like a round-robin scheduler or a carousel menu. Let's learn how to create a circular linked list with a simple example program.
We will build a basic circular linked list where each node contains an integer value. We will implement methods to add new nodes to the list and display the list contents.
Code
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class CircularLinkedList {
Node head = null;
Node tail = null;
// Method to add a new node
public void add(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
newNode.next = head;
} else {
tail.next = newNode;
tail = newNode;
tail.next = head;
}
}
// Method to display the list
public void display() {
Node current = head;
if(head != null) {
do {
System.out.print(current.data + " ");
current = current.next;
} while(current != head);
}
System.out.println();
}
}
public class Main {
public static void main(String[] args) {
CircularLinkedList clist = new CircularLinkedList();
// Adding data to the list
clist.add(1);
clist.add(2);
clist.add(3);
clist.add(4);
// Displaying the circular linked list
System.out.println("Circular Linked List: ");
clist.display();
}
}
Key Points in the Program:
Linked lists in data structure, with their flexibility, have many uses. Let's see some of the advanced applications of these structures.
Linked lists in data structure are a fundamental structure. They are used in several applications across computing and information technology. Their adaptability to dynamic data management scenarios—from simple lists to complex graph representations—makes them an invaluable tool in the programmer's toolkit. Linked lists offer the flexibility and efficiency needed to manage challenges like memory management schemes, blockchain technologies, undo functionalities in applications, circular buffers, and sparse matrices.
The four types of linked lists are
An example of a linked list would be a playlist, where each song (node) contains information about the song and a pointer to the next song in the list.
The syntax to create a linked list node in C would be
struct Node {
int data;
struct Node* next;
};
A linked list as an Abstract Data Type (ADT) is a collection of items where each item points to the next. It allows dynamic data organization, insertion, and deletion without fixed memory size.
Linked lists are used for efficient data management when the size of the dataset changes dynamically. They are suitable for implementing queues, stacks, graphs, and other dynamic data structures.
A linked list class in object-oriented programming languages like Java or C++ encapsulates the properties (data elements) and behaviors (functions or methods) needed to implement a linked list's functionality.
A simple (or singly) linked list is a data structure where each element (node) contains data and a reference (or pointer) to the next node in the sequence, which further forms a linear collection.
Linked lists are best for situations that need flexible memory allocation, efficient insertions, and deletions, and when the data size changes dynamically, as they don't require continuous memory space.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.