Linked List-> How to Implements Singly Linked list in C++ Language. And usage of Linked list in Real-life.
What is Linked-List:- Link list is a Linear data structure. Which are Connected to Each other’s via node’s. Last
node have a null Pointer . Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list is the second most-used data structure after array. Following are the important terms to understand the concept of Linked List.
Important terms of Link list :-
- Link: Each link of a linked list can store a data called an element.
- Next: Each link of a linked list contains a link to the next link called Next.
Linked List Representation:
Linked list can be visualized as a chain of nodes, where every node points to the next node.
. Link list Contain head which have address of first node.
Each link is linked with its next link using its next link.
Last link carries a link as null to mark the end of the list.
Types of Linked List
Singly Link list : Item navigation is forward only.
Doubly Linked List: Items can be navigated forward and backward.
Circular Linked List: Last item contains link of the first element as next and the first element has a link to the last element as previous.
Basic Operations of Linked List.
Following are the basic operations supported by a list.
- Insertion − Adds an element at the beginning of the list.
- Deletion − Deletes an element at the beginning of the list.
- Display − Displays the complete list.
- Search − Searches an element using the given key.
- Delete − Deletes an element using the given key.
Insertion Operation:
Insertion operation in Linked list is Three types.
- Insertion at First/ Beginning .
- Insertion at Middle.
- Insertion at Last / Tail.
Basic Program of insertion and Printing of Singly Link list at beginning or First using C++.
#include<iostream>
using namespace std;
class node{
public:
int data;
node*next;
node(int d)
{
data=d;
next=NULL;
}
};
// insertion new node into first…
void insertbegining(node* &head, int data)
{
node* newnode= new node(data);
if(head==NULL)
{
head=newnode;
return;
}
newnode->next=head;
head=newnode;
}
// this method for show the data of the linklist.
void printnode(node* head)
{
node* temp=head;
while(temp!=NULL)
{
cout<<temp->data<<”->”;
temp= temp->next;
}
cout<<”Null”<<endl;
}
int main()
{
node* head =NULL;
cout<<”Enter your number of nodes”<<endl;
int node;
cin>>node;
int datas;
for(int i=0;i<node;i++)
{
cin>>datas;
insertbegining(head,datas);
}
printnode(head);
return 0;
}
Uses of Linked list in our Real Life.
- Image viewer — Previous and next images are linked, hence can be accessed by next and previous button.
- Previous and next page in web browser — We can access previous and next url searched in web browser by pressing back and next button since, they are linked as linked list.
- Music Player — Songs in music player are linked to previous and next song. you can play songs either from starting or ending of the list.
Next part coming soon. Please follow me .