Singly Linked List in C++

Singly Linked List in C++

What is a singly linked list in CPP programming?

Singly linked list in C++ are one of the simplest data structure and are further used for the implementation of advance data structures such as tree and graph. Singly linked list are being preferred over arrays because every node of the list have a pointer that points to the address of next node in the list hence performing majority of the operations in the list becomes easy.

Let us learn more about singly linked list in CPP programming in this article.

Why
Singly Linked List?

Performing operations on a list becomes easy

Singly linked list give us the flexibility to perform various operations such as insertion, deletion in an efficient manner as compared to arrays.

Efficient memory allocation

We need not tho allocate memory in advance to the singly linked list, dynamic memory is allocated in singly linked list hence it saves extra memory .

Implentation of advance data structure

Many advance data structures are implemented with the help of singly linked list hence they become very useful there.

STRUCTURE

STRUCTURE

Structure of singly linked list

Following set of code is used to struct the nodes of singly linked list. Here we have defined a singly linked list which will store integer type data in it.
class node  

    int data; 
   node *next; 
}; 
Singly Linked List in C++ programming

C++ programming code for creating a singly linked list

#include <iostream>
using namespace std;

struct node 
{
    int num;                
    node *nextptr;             
}*stnode; //node constructed


void createList(int n) //function to create linked list.
{
    struct node *frntNode, *tmp;
    int num, i;
 
    stnode = (struct node *)malloc(sizeof(struct node));
    
        cout<<"Enter the data for node 1: ";
        cin>>num;
        stnode-> num = num;      
        stnode-> nextptr = NULL; //Links the address field to NULL
        tmp = stnode;
 
        for(i=2; i<=n; i++)
        {
            frntNode = (struct node *)malloc(sizeof(struct node)); 
 

            if(frntNode == NULL) //If frntnode is null no memory cannot be allotted
            {
                cout<<" Memory can not be allocated";
                break;
            }
            else
            {
                cout<<"Enter the data for node "<<i<<": "; // Entering data in nodes.
                cin>>num;
                frntNode->num = num;         
                frntNode->nextptr = NULL;    
                tmp->nextptr = frntNode;     
                tmp = tmp->nextptr;
            }
        }
    
}

void display() //function to print linked list
{
    struct node *tmp;
    if(stnode == NULL)
    {
        cout<<" No data found in the list";
    }
    else
    {
        tmp = stnode;
        while(tmp != NULL)
        {
            cout<num<<"\t"; tmp = tmp->nextptr;         
        }
    }
} 
int main()
{
    int n;
		
    cout<<"Enter the number of nodes: ";
    cin>>n;
    createList(n);
    cout<<"\nLinked list data: \n";		
    display();
    return 0;
}
Output:
Enter the number of nodes: 5
Enter the data for node 1: 10
Enter the data for node 2: 20
Enter the data for node 3: 30
Enter the data for node 4: 40
Enter the data for node 5: 50

Linked list data: 
10	20	30	40	50

Disadvantages of singly linked list in CPP programming

  1. Members could be assigned anywhere in the memory.
  2. Each member shall include an address size member, hence it utilizes poor memory.
  3. Some operations like reversing a list is complicated when compared with arrays,
Linked List in C++ meme 2