What is singly link list?

Singly link list is defined as:

singly link list is the type link list of data structure which move in forward direction because it does not move in backward direction. Singly link list is consists of no. of nodes, and each node having two parts which are given below:

1: Information part

2: Address part

Singly link list is whole packet which contains nodes and all data move only in forward direction.

Parts of singly Link List:

Singly link list consist of 3 parts:

1: Head part 

2: Node 

3: Tail part

Head part is also node but it is initial part for starting the every link list. It contain the address of first node and so no . when we have no node in our link list or no data in link list then head part is  equal to NULL otherwise it contain the address of first node.

Tail is last part of link list which is equal to NULL.

Structure of Singly link list:


simple structure of singly link list


Singly Link list with code in C++: 

First we define the structure for link list and structure contain two things one is data types and other is pointer which link one node to another.

struct node
{
	int data;
	node*link;
};

Initialization of head pointer:
Now we make a head pointer which contain the address of first node. Mostly Head pointer in initialize globally.

node*head=Null;


Logical Question:    Why we take head pointer with node name?

Reason: because we take name of structure is node. An node is variable which contain different data types. so Head is data type which called node.

And first Head pointer is equal to NULL when no node is define.


Insertion of first node:
now we define a function named as "insert_node( int value)" for insert a first node which call later in main function.
int insert_node (int value)
{
	node*ptr=new node;
	ptr->data=value;
	ptr->link=NULL;
	head=ptr;
	}
Explanation of this code:

Insert a function name as insert_node and pass a parameter in it and the first we make a new node with help of new keyword (new node;) and then make a pointer ptr and equalize it to new node so it contain the address of new node. And then insert the value in data part with help of pointer ptr and then link prt of new node equalize to NULL with help of ptr because ptr contain the address and data part of new node so we can work on new node with pointer ptr. And at last we equalize the head pointer to ptr so with ptr head connect with new node. this is for insertion of one node.

if we have mode then one node then we do code as:



if(head==NULL)
	{
		head=ptr;
	}
	else
	{
		node*temp=head;
		while(temp->link!=NULL)
		{
			temp=temp->link;
		}
		temp->link=ptr;
	}
First we check is HEAD is equal to NULL then equalize the head to ptr. if not then first we make a temporary pointer which helps to insert node and firstly we equal the temp ptr to head and then start a while condition for checking NULL condition. whenever temporary pointer is not equal to NULL then we  pass address of every node to temporary pointer in while condition. if temporary pointer is equal to NULL then while condition stop and equal the pointer ptr to link part of temporary pointer because new node address is pass to ptr pointer so that's why we equalize the temporary pointer to ptr for connecting new node through address.

Complete code of insertion of node in singly link list:
#include <iostream>
using namespace std;
struct node
{
	int data;
	node*link;
};
node*head=NULL;
int insert(int value)
{
	node*ptr=new node;
	ptr->data=value;
	ptr->link=NULL;
	if(head==NULL)
	{
		head=ptr;
	}
	else
	{
		node*temp=head;
		while(temp->link!=NULL)
		{
			temp=temp->link;
		}
		temp->link=ptr;
	}
}
int main()
{
	int a,value;
loop:	cout<<endl<<"1:insert node"<<endl<<"enter choice : ";
	cin>>a;
	    system("cls");
	    switch(a)
	    {
	      case 1:
                  cout<<"enter value of node : ";
                  cin>>value;
                  insert(value);
                  break;
	      default:
	          cout<<"Invalid Choice..!";
	    }
	    goto loop;
}