Linked List is a linear data structure in which there is a node having data and pointer that points to an address of the next node.
Let's see a figure of Structure of the Linked list from which we can easily understand the structure of a Linked list.
From this figure, We can see that each node, having data and pointer that points to the next address of next node.
Suppose, an address of the second node is 2024 and that address is pointed by the pointer of the first node.
This is the main advantage of LINKED LIST and main difference from an ARRAY. In an array, next element is always in continuation of memory(depending on data types).
In Linked list, there are mainly three operations to perform:-
Let's see a figure of Structure of the Linked list from which we can easily understand the structure of a Linked list.
From this figure, We can see that each node, having data and pointer that points to the next address of next node.
Suppose, an address of the second node is 2024 and that address is pointed by the pointer of the first node.
This is the main advantage of LINKED LIST and main difference from an ARRAY. In an array, next element is always in continuation of memory(depending on data types).
In Linked list, there are mainly three operations to perform:-
- Insertion
- Deletion
- Traversing
In C, we can represent a node using structures. Below is an example of a linked list node.
struct node
{
int data;
struct node *next;
};
NOTE: There is a node called Start node that stores the starting address of the node and if the list is empty then it has a Null address.
A node can be added in three different ways
1. Insertion at first node
2. Insertion at middle node
3. Insertion at last node.
We are initially learning the simplest method of insertion that is "Insertion at last node".
Here is the method for inserting a new node at the very last node in the existing linked list.
void insertNodeLast()
{
struct Node *temp,*t;
temp=(struct node *)malloc(sizeof(struct node));
printf("enter data:");
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
t=start;
while(t->next!=NULL)
t=t->next;
t->next=temp;
}
}
We can have another method which will add a new node at a very first element in the list.
void insertNodeFirst()
{
struct Node *temp,*t;
temp=(struct node *)malloc(sizeof(struct node));
printf("enter data:");
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
temp->next=start;
start=temp;
}
}
For more operations of the linked list, please wait for a while.
It's loading......
click here:- Deletion Operation
void insertNodeLast()
{
struct Node *temp,*t;
temp=(struct node *)malloc(sizeof(struct node));
printf("enter data:");
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
t=start;
while(t->next!=NULL)
t=t->next;
t->next=temp;
}
}
We can have another method which will add a new node at a very first element in the list.
void insertNodeFirst()
{
struct Node *temp,*t;
temp=(struct node *)malloc(sizeof(struct node));
printf("enter data:");
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
temp->next=start;
start=temp;
}
}
For more operations of the linked list, please wait for a while.
It's loading......
click here:- Deletion Operation
No comments:
Post a Comment