Implementation of Linked List in C++

Linked List

Hey Shouters!! Today we will learn implementation of linked list in C++.

In this blog, we will be discussing about one of the most important data structure i.e. Linked List. So you can imagine linked list as a container which has some partitions. The number of partitions may vary depending on the type of linked list.

What is Linked List?

For the time being, we will be discussing the singly linked list in which there are only two partitions. one containing data and one containing pointer to next container. These containers are known as nodes.

Basic structure of node

So this linked list contains 2 partitions, one for data and other for storing the address of the next node. In this way, all the nodes are connected to each other in the memory in any location.

linked list in c++

For data structures, I will suggest you all concentrate on the figures of operations. These figures are the only way to get you to understand the data structure efficiently. Understand the figure and then try to implement that in the code. For your clarification, codes in c++ are already given. But you can implement these data structure in any language. You just have to understand the figures.

There are some basic functions that we perform on Linked list –

  1. Insertion
  2. Deletion
  3. Traversal
  4. Searching

We will discuss the only insertion in this blog. There will be more blogs for other functions.

So the insertion of node can happen at 3 positions

  1. In the front
  2. At the end
  3. In between 2 nodes.

We will discuss each of these 3 cases with their implementations in c++.

First, we will first see how to create a node.

Implementation of Linked List in C++

In this code, we will see a code for the creation of node. We will create class named ‘Node’ and in this class, we will declare 2 variables. One ‘data’ variable of type int to store the data to be put into he node and one pointer variable ‘next’ which points to next node in the list.

Creation of Node

Insertion in the beginning –

In this code, we will see the code for inserting the node in the beginning. We will create a method named ‘push’ with 2 arguments, one reference to head ‘head_ref’ and one ‘new_data’ for the data to be put in the node.

Instertion

Insertion at the end

In this code, we will create a function named append with arguments as ‘head_ref’ which is reference to head pointer and new_data which is data to be inserted and then we follow the operation as present in the given fig.

linked list in c++

Insertion between 2 nodes

In this code, we will create a function named insertAfter with arguments prev_node and new_data. In this function, we will perform the operations as in the figure.

linked list in c++

Printing the list

The Compelete Code

The compelete code for this insertion implementation in c++ is given below.

So in this blog, we learned the insertion operation of the linked list. In the next blog, we will go for the deletion in the linked list.

We daily add Multiple Choice Questions for placement preparation questions and technical interview preparation on the Instagram page. Check the Instagram account: Shout Coders

Happy learnings!!!!!

Recommended Posts –

  1. Introduction to C language
  2. Structure of C program
  3. C Program to add two numbers
  4. C Program to reverse a number
  5. Difference between C and C ++.

Leave a Reply

Your email address will not be published. Required fields are marked *

Close Bitnami banner
Bitnami