Javatpoint Logo
Javatpoint Logo

Create Linked List In C++

What is a linked list?

A linked list is a linear data structure that consists of a sequence of nodes, where each node stores a piece of data and a reference (pointer) to the next node in the List. Linked lists are useful for storing data collections when the collection size is not known in advance or when the data is frequently inserted or deleted.

In C++, we can create a linked list by defining a node class and a linked list class. The node class will represent a single node in the List and contain a data field and a pointer to the next node. The linked list class will contain a head pointer to the first node in the List and various methods for inserting, deleting, and traversing the nodes in the List.

Here is an example of a node class in C++:

This node class has a public data field data of type int and a public pointer next to the next node in the List. It also has a constructor that initializes the data field and sets the next pointer to nullptr.

Here is an example of how to use the node and linked list classes to create and manipulate a linked list in C++:

Output:

Create Linked List In C++

Explanation:

This linked list class has a private field head that points to the first node in the List and various public methods for inserting and deleting nodes at the beginning and end of the List and for printing the List to the console. This program will create a linked list, insert some nodes at the beginning and end of the List, delete a node at the beginning and end of the List, and print the List to the console.

Here is an example of how to create a linked list in C++ using a template class:

Output:

Create Linked List In C++

Explanation:

In the above code, we have defined a template class Node that represents a single node in a linked list. The Node class has a public data field data of type T (where T is a template parameter) and a public pointer next to the next node in the List. It also has a constructor that initializes the data field and sets the next pointer to nullptr.

We have also defined a template class, LinkedList, that represents a linked list and contains a private field head that points to the first node in the List. The LinkedList class has various public methods for inserting and deleting nodes at the beginning and end of the List and for printing the List to the console.

The insertAtBeginning method creates a new node with the given data and inserts it at the beginning of the List by updating the head pointer. The insertAtEnd method creates a new node with the given data and inserts it at the end of the List by traversing it and updating the last node's next pointer. The deleteAtBeginning method deletes the first node in the List by updating the head pointer and freeing up the memory used by the node. The deleteAtEnd method deletes the last node in the List by traversing the List and updating the next pointer of the second to the last node. The printList method traverses the List and prints the data of each node to the console.

In the main function, we create an instance of the LinkedList class and call the various methods to insert and delete nodes and print the List. The output shows the original List, the List after deleting a node at the beginning, and the List after deleting a node at the end.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA