A Detailed Study Of Linked List In C++ – A linked list is a data structure that stores data elements in a linear, dynamic manner. Arrays have already been discussed in prior fundamental C++ topics. Arrays are a linear data structure that stores data objects in contiguous regions, as well.

The linked list, unlike arrays, does not store data in contiguous memory locations.

A linked list is made up of elements called “Nodes,” each of which has two components. The first portion contains the data, while the second contains a pointer to the next node. This structure is commonly referred to as a “singly linked list.”

**Linked List In C++**

We will take a look at the singly linked list in detail in this tutorial.

**Operations**

The linked list, like other data structures, can be used to perform a variety of operations. We can’t achieve the same random access with a linked list as we can with an array because we can access the element using subscript directly even if it’s somewhere in the middle.

To get to any node, we must first traverse the linked list from the beginning, and only then can we get to the requested node. As a result, reading data from the linked list at random is costly.

A linked list can be used to do the following operations:

**InsertionÂ **

The linked list’s insertion action adds a new item to the list. Though it may appear straightforward, we know that everytime a data item is added to the linked list, we must alter the next pointers of the previous and next nodes of the new item that we have put.

The location where the new data item will be added is the second factor to consider.

In C++, we can declare a linked list as a structure or as a class. Declaring linked list as a structure is a traditional C-style declaration. A linked list as a class is used in modern C++, mostly while using standard template library.

In the following program, we have used structure to declare and create a linked list. It will have data and pointer to the next element as its members.

`#include <iostream>` `using` `namespace` `std;` `// A linked list node` `struct` `Node` `{` `Â Â Â ` `int` `data;` `Â Â Â ` `struct` `Node *next;` `};` `//insert a new node in front of the list` `void` `push(` `struct` `Node** head, ` `int` `node_data)` `{` `Â Â Â ` `/* 1. create and allocate node */` `Â Â Â ` `struct` `Node* newNode = ` `new` `Node;` `Â Â Â ` `/* 2. assign data to node */` `Â Â Â ` `newNode->data = node_data;` `Â Â Â ` `/* 3. set next of new node as head */` `Â Â Â ` `newNode->next = (*head);` `Â Â Â ` `/* 4. move the head to point to the new node */` `Â Â Â ` `(*head) = newNode;` `}` `//insert new node after a given node` `void` `insertAfter(` `struct` `Node* prev_node, ` `int` `node_data)` `{` `Â Â ` `/*1. check if the given prev_node is NULL */` `if` `(prev_node == NULL)` `{` `Â Â Â ` `cout<<` `"the given previous node is required,cannot be NULL"` `; ` `return` `; } ` `Â Â Â ` `/* 2. create and allocate new node */` `Â Â Â ` `struct` `Node* newNode =` `new` `Node; ` `Â Â Â ` `/* 3. assign data to the node */` `Â Â Â ` `newNode->data = node_data;` `Â Â Â ` `/* 4. Make next of new node as next of prev_node */` `Â Â Â ` `newNode->next = prev_node->next;` `Â Â Â Â ` `/* 5. move the next of prev_node as new_node */` `Â Â Â Â ` `prev_node->next = newNode;` `}` `/* insert new node at the end of the linked list */` `void` `append(` `struct` `Node** head, ` `int` `node_data)` `{` `/* 1. create and allocate node */` `struct` `Node* newNode = ` `new` `Node;` `struct` `Node *last = *head; ` `/* used in step 5*/` `/* 2. assign data to the node */` `newNode->data = node_data;` `/* 3. set next pointer of new node to null as its the last node*/` `newNode->next = NULL;` `/* 4. if list is empty, new node becomes first node */` `if` `(*head == NULL)` `{` `*head = newNode;` `return` `;` `}` `/* 5. Else traverse till the last node */` `while` `(last->next != NULL)` `last = last->next;` `/* 6. Change the next of last node */` `last->next = newNode;` `return` `;` `}` `// display linked list contents` `void` `displayList(` `struct` `Node *node)` `{` `Â Â Â ` `//traverse the list to display each node` `Â Â Â ` `while` `(node != NULL)` `Â Â Â ` `{` `Â Â Â Â Â Â ` `cout<<node->data<<` `"-->"` `;` `Â Â Â Â Â Â ` `node = node->next;` `Â Â Â ` `}` `if` `(node== NULL)` `cout<<` `"null"` `; ` `} ` `/* main program for linked list*/` `int` `main() ` `{ ` `/* empty list */` `struct` `Node* head = NULL; ` `// Insert 10.` `append(&head, 10); ` `// Insert 20 at the beginning. ` `push(&head, 20); ` `// Insert 30 at the beginning. ` `push(&head, 30); ` `// Insert 40 at the end. ` `append(&head, 40); ` `// ` `Insert 50, after 20. ` `insertAfter(head->next, 50);` `cout<<` `"Final linked list: "` `<<endl;` `displayList(head);` `return` `0;` `}` |

**Output:**

FinalÂ linkedÂ list:

30â€“>20â€“>50â€“>10â€“>40â€“>null

Next, we implement the linked list insert operation in Java. In Java language, the linked list is implemented as a class. The program below is similar in logic to the C++ program, the only difference is that we use a class for the linked list.

`class` `LinkedList` `{` `Â Â Â ` `Node head; ` `// head of list` `Â Â Â ` `//linked list node declaration` `Â Â Â ` `class` `Node` `{` `Â Â Â ` `int` `data;` `Â Â Â ` `Node next;` `Â Â Â ` `Node(` `int` `d) {data = d; next = null; }` `}` `/* Insert a new node at the front of the list */` `public` `void` `push(` `int` `new_data)` `{` `Â Â Â ` `//allocate and assign data to the node` `Â Â Â ` `Node newNode = ` `new` `Node(new_data);` `Â Â Â ` `//new node becomes head of linked list` `Â Â Â ` `newNode.next = head;` `Â Â Â ` `//head points to new node` `Â Â Â ` `head = newNode;` `}` `// Given a node,prev_node insert node after prev_node` `public` `void` `insertAfter(Node prev_node, ` `int` `new_data)` `{` `Â Â Â ` `//check if prev_node is null.` `Â Â Â ` `if` `(prev_node == null)` `Â Â Â ` `{` `Â Â Â Â Â Â ` `System.out.println(` `"The given node is required and cannot be null"` `);` `Â Â Â Â Â Â ` `return` `;` `Â Â Â ` `}` `//allocate node and assign data to it` `Node newNode = ` `new` `Node(new_data);` `//next of new Node is next of prev_node` `newNode.next = prev_node.next;` `//prev_node->next is the new node.` `prev_node.next = newNode;` `}` `Â Â Â Â ` `//inserts a new node at the end of the list` `public` `void` `append(intnew_data) ` `Â Â Â Â ` `{ ` `Â Â Â Â Â Â Â Â ` `//allocate the node and assign data` `Â Â Â Â Â Â Â Â ` `Node newNode = ` `new` `Node(new_data); ` `Â Â Â Â Â Â Â Â ` `//if linked list is empty, then new node will be the head` `if` `(head == null) ` `Â Â Â Â Â Â Â Â ` `{ ` `head = ` `new` `Node(new_data); ` `return` `; ` `Â Â Â Â Â Â Â Â ` `} ` `Â Â Â Â Â Â Â Â ` `//set next of new node to null as this is the last node` `newNode.next = null; ` `// if not the head node traverse the list and add it to the last` `Node last = head;` `while` `(last.next != null)` `last = last.next;` `//next of last becomes new node` `last.next = newNode;` `return` `;` `}` `//display contents of linked list` `public` `void` `displayList()` `{` `Â Â Â ` `Node pnode = head;` `Â Â Â ` `while` `(pnode != null)` `{` `Â Â Â ` `System.out.print(pnode.data+` `"-->"` `);` `Â Â Â ` `pnode = pnode.next;` `}` `if` `(pnode == null)` `System.out.print(` `"null"` `);` `}` `}` `//Main class to call linked list class functions and construct a linked list` `class` `Main{` `public` `static` `void` `main(String[] args)` `Â Â Â ` `{` `Â Â Â ` `/* create an empty list */` `Â Â Â ` `LinkedList lList = ` `new` `LinkedList();` `Â Â Â ` `// Insert 40.` `Â Â Â ` `lList.append(40);` `Â Â Â ` `// Insert 20 at the beginning.` `Â Â Â ` `lList.push(20);` `Â Â Â ` `// Insert 10 at the beginning.` `Â Â Â ` `lList.push(10);` `Â Â Â ` `// Insert 50 at the end.` `Â Â Â ` `lList.append(50);` `Â Â Â ` `// Insert 30, after 20.` `Â Â Â ` `lList.insertAfter(lList.head.next, 30);` `Â Â Â ` `System.out.println(` `"\nFinal linked list: "` `);` `Â Â Â ` `lList. displayList ();` `Â Â Â ` `}` `}` |

**Output:**

FinalÂ linkedÂ list:

10â€“>20â€“>30â€“>40â€“>50â€“>null

In both the program above, C++ as well as Java, we have separate functions to add a node in front of the list, end of the list and between the lists given in a node. In the end, we print the contents of the list created using all the three methods.

**Deletion**

Delete a node from a linked list, like insertion, requires a variety of points from which the node might be eliminated. From the linked list, we can delete the first, last, or kth node at random. To maintain the linked list intact after deletion, we must update the next pointer and the other pointers in the linked list correctly.

We’ve provided two deletion methods in the following C++ implementation: removing the first node in the list and deleting the final node in the list. We begin by adding nodes to the head of the list. The contents of the list are then displayed after each insertion and deletion.

`#include <iostream>` `using` `namespace` `std;` `/* Link list node */` `struct` `Node {` `Â Â Â ` `int` `data;` `Â Â Â ` `struct` `Node* next;` `Â Â Â ` `};` `//delete first node in the linked list` `Node* deleteFirstNode(` `struct` `Node* head)` `{` `Â Â Â ` `if` `(head == NULL)` `Â Â Â ` `return` `NULL;` `Â Â Â ` `// Move the head pointer to the next node` `Â Â Â ` `Node* tempNode = head;` `Â Â Â ` `head = head->next;` `Â Â Â ` `delete` `tempNode;` `Â Â Â ` `return` `head;` `}` `//delete last node from linked list` `Node* removeLastNode(` `struct` `Node* head)` `{` `Â Â Â ` `if` `(head == NULL)` `Â Â Â ` `return` `NULL;` `Â Â Â ` `if` `(head->next == NULL) {` `Â Â Â Â Â Â ` `delete` `head;` `Â Â Â Â Â Â ` `return` `NULL;` `Â Â Â ` `}` `// first find second last node` `Node* second_last = head;` `while` `(second_last->next->next != NULL)` `second_last = second_last->next;` `// Delete the last node` `delete` `(second_last->next);` `// set next of second_last to null` `second_last->next = NULL;` `return` `head;` `}` `// create linked list by adding nodes at head` `void` `push(` `struct` `Node** head, ` `int` `new_data)` `{` `Â Â Â ` `struct` `Node* newNode = ` `new` `Node;` `Â Â Â ` `newNode->data = new_data;` `Â Â Â ` `newNode->next = (*head);` `Â Â Â ` `(*head) = newNode;` `}` `// main function` `int` `main()` `{` `Â Â Â ` `/* Start with the empty list */` `Â Â Â ` `Node* head = NULL;` `Â Â Â ` `// create linked list` `Â Â Â ` `push(&head, 2);` `Â Â Â ` `push(&head, 4);` `Â Â Â ` `push(&head, 6);` `Â Â Â ` `push(&head, 8);` `Â Â Â ` `push(&head, 10);` `Â Â Â Â Â Â Â Â Â ` `Node* temp;` `Â Â Â ` `cout<<` `"Linked list created "` `<<endl; ` `for` `(temp = head; temp != NULL; temp = temp->next)` `Â Â Â ` `cout << temp->data << ` `"-->"` `;` `Â Â Â ` `if` `(temp == NULL)` `Â Â Â ` `cout<<` `"NULL"` `<<endl;` `Â Â Â Â Â Â Â ` `//delete first node` `Â Â Â ` `head = deleteFirstNode(head);` `Â Â Â ` `cout<<` `"Linked list after deleting head node"` `<<endl; ` `for` `(temp = head; temp != NULL; temp = temp->next)` `Â Â Â ` `cout << temp->data << ` `"-->"` `;` `Â Â Â ` `if` `(temp == NULL)` `Â Â Â ` `cout<<` `"NULL"` `<<endl;` `Â Â Â Â Â Â ` `//delete last node` `Â Â Â ` `head = removeLastNode(head);` `Â Â Â ` `cout<<` `"Linked list after deleting last node"` `<<endl; ` `for` `(temp = head; temp != NULL; temp = temp->next)` `Â Â Â ` `cout << temp->data << ` `"-->"` `;` `Â Â Â ` `if` `(temp == NULL)` `Â Â Â ` `cout<<` `"NULL"` `;` `Â Â Â ` `return` `0;` `}` |

**Output:**

LinkedÂ listÂ created

10â€“>8â€“>6â€“>4â€“>2â€“

>NULL

LinkedÂ listÂ afterÂ deletingÂ headÂ node

8â€“>6â€“>4â€“>2â€“

>NULL

LinkedÂ listÂ afterÂ deletingÂ lastÂ node

8â€“>6â€“>4â€“>NULL

Next is the Java implementation for deleting nodes from the linked list. The implementation logic is the same as used in the C++ program. The only difference is that the linked list is declared as a class.

`class` `Main {` `// Linked list node /` `static` `class` `Node {` `Â Â Â Â Â Â ` `int` `data;` `Â Â Â Â Â Â ` `Node next;` `Â Â Â ` `};` `// delete first node of linked list` `static` `Node deleteFirstNode(Node head)` `{` `Â Â Â ` `if` `(head == null)` `Â Â Â ` `return` `null;` `Â Â Â ` `// Move the head pointer to the next node` `Â Â Â ` `Node temp = head;` `Â Â Â ` `head = head.next;` `Â Â Â ` `return` `head;` `}` `// Delete the last node in linked list` `static` `Node deleteLastNode(Node head)` `{` `Â Â Â ` `if` `(head == null)` `Â Â Â ` `return` `null;` `Â Â Â ` `if` `(head.next == null) {` `Â Â Â ` `return` `null;` `}` `// search for second last node` `Node second_last = head;` `while` `(second_last.next.next != null)` `second_last = second_last.next;` `// set next of second last to null` `second_last.next = null;` `return` `head;` `}` `// Add nodes to the head and create linked list` `static` `Node push(Node head, ` `int` `new_data)` `{` `Â Â Â ` `Node newNode = ` `new` `Node();` `Â Â Â ` `newNode.data = new_data;` `Â Â Â ` `newNode.next = (head);` `Â Â Â ` `(head) = newNode;` `Â Â Â ` `return` `head;` `}` `//main function` `public` `static` `void` `main(String args[])` `{` `Â Â Â ` `// Start with the empty list /` `Â Â Â ` `Node head = null;` `Â Â Â Â Â Â Â Â ` `//create linked list` `Â Â Â ` `head = push(head, 1);` `Â Â Â ` `head = push(head, 3);` `Â Â Â ` `head = push(head, 5);` `Â Â Â ` `head = push(head, 7);` `Â Â Â ` `head = push(head, 9);` `Â Â Â Â Â Â Â Â ` `Node temp;` `Â Â Â ` `System.out.println(` `"Linked list created :"` `);` `Â Â Â ` `for` `(temp = head; temp != null; temp = temp.next)` `Â Â Â ` `System.out.print(temp.data + ` `"-->"` `);` `Â Â Â ` `if` `(temp == null)` `Â Â Â ` `System.out.println(` `"null"` `);` `Â Â Â ` `head = deleteFirstNode(head);` `Â Â Â ` `System.out.println(` `"Linked list after deleting head node :"` `);` `Â Â Â ` `for` `(temp = head; temp != null; temp = temp.next)` `Â Â Â ` `System.out.print(temp.data + ` `"-->"` `);` `Â Â Â ` `if` `(temp == null)` `Â Â Â ` `System.out.println(` `"null"` `);` `Â Â Â ` `head = deleteLastNode(head);` `Â Â Â ` `System.out.println(` `"Linked list after deleting last node :"` `);` `Â Â Â ` `for` `(temp = head; temp != null; temp = temp.next)` `Â Â Â ` `System.out.print(temp.data + ` `"-->"` `);` `Â Â Â ` `if` `(temp == null)` `Â Â Â ` `System.out.println(` `"null"` `);` `Â Â Â ` `}` `}` |

**Output:**

LinkedÂ listÂ createdÂ :

9â€“>7â€“>5â€“>3â€“>1â€“

>null

LinkedÂ listÂ afterÂ deletingÂ headÂ nodeÂ :

7â€“>5â€“>3â€“>1â€“

>null

LinkedÂ listÂ afterÂ deletingÂ lastÂ nodeÂ :

7â€“>5â€“>3â€“>null

**Count The Number Of Nodes**

While traversing the linked list, the operation to count the number of nodes can be done. As we saw in the previous approach, we must traverse the linked list from the beginning anytime we wish to insert/delete a node or display the contents of the linked list.

We may count the number of nodes in the linked list by keeping a counter and incrementing it as we explore each node. This programme will be implemented by the readers.

**Arrays And Linked Lists**

Let’s analyse how arrays and linked lists stack up against each other now that we’ve seen the operations and implementation of the linked list.

Arrays | Linked lists |
---|---|

Arrays have fixed size | Linked list size is dynamic |

Insertion of new element is expensive | Insertion/deletion is easier |

Random access is allowed | Random access not possible |

Elements are at contiguous location | Elements have non-contiguous location |

No extra space is required for the next pointer | Extra memory space required for next pointer |

**Applications**

Because arrays and linked lists are both linear data structures that are used to hold objects, they can be utilised in comparable ways in most applications.

The following are some examples of linked list applications:

- Stacks and queues can be implemented using a linked list.
- When we need to express graphs as adjacency lists, we can use a linked list to implement them.
- A linked list can be used to hold a mathematical polynomial.
- The buckets utilised in hashing are implemented using linked lists in the case of hashing technique.
- We can use a linked list whenever a programme requires dynamic memory allocation because linked lists are more efficient in this scenario.

**Conclusion**

Linked lists are data structures that are used to hold data items in a sequential but non-contiguous manner. A linked list is a set of nodes that each have a data component and a next pointer that points to the memory address of the list’s next member.

The next pointer of the last entry in the list is set to NULL, signifying the end of the list. The Head is the first element in the list. Insertion, deletion, traversal, and other actions are supported by the linked list. Linked lists are preferable over arrays when dynamic memory allocation is required.

Because we can’t access the elements randomly as we do with arrays, traversing linked lists is expensive. When compared to arrays, however, insertion-deletion procedures are less expensive.

In this tutorial, we learned everything there is to know about linear linked lists. Circular or doubly linked lists are also possible. In our forthcoming tutorials, we’ll take a closer look at these lists.