Linked list is one of the data structure that used to overcome the limitation of array. The implementation of a linked list in C++ is done using pointers. Each element in the linked list is called as node. The node contains two different fields. The first field holds the data and the second field holds the address of the next node.
Types of Linked list:
There are 3 types of linked list used in the programs
1)Singly Linked List 2)Doubly Linked List 3)Circular Linked List
Singly Linked List:
In this linked list, Each node contains the data and address of the next node. Only the reference to the first list node is required to access the whole linked list. This is known as the head. The last node in the list points to nothing so it stores NULL in that part.
Singly Linked List
Doubly Linked List:
In this linked list, Each node contains the data ,address of the both previous node and next node.We can either go forward or backward direction based on the address of the node.
Circular Linked List:
A circular linked list is a variation of linked list in which the
last element is linked to the first element. This forms a circular loop.
Example:
#include
using namespace std;
//Declare Node
struct Node{
int num;
Node *next;
};
//Declare starting (Head) node
struct Node *head=NULL;
//Insert node at start
void insertNode(int n){
struct Node *newNode=new Node;
newNode->num=n;
newNode->next=head;
head=newNode;
}
//Traverse/ display all nodes (print items)
void display(){
if(head==NULL){
cout<<"List is empty!"<num<<" ";
temp=temp->next;
}
cout<num<<" is removed."<next;
}
int main(){
cout<<"<---- Singly Linked List ----->" <" <" <" <" <