Linked List Slide
Linked List Slide
Each element in a linked list is called a "node," and each node has two components:
The last node in a linked list typically has a reference of null or None to signify the end of the list.
1. Singly Linked List: Each node points only to the next node in the sequence.
2. Doubly Linked List: Each node points to both the next and the previous nodes, allowing for traversal in both
directions.
3. Circular Linked List: The last node in the list points back to the first node, forming a loop.
Linked List
Q. What are the advantages of linked list over array?
Dynamic Size:
❖ Linked List: Linked lists can dynamically grow or shrink in size without the need to predefine the size. Elements can be easily
added or removed from any position in the list.
❖ Array: Arrays have a fixed size, and resizing them often involves creating a new array and copying elements, which can be
inefficient.
Efficient Insertions and Deletions:
❖ Linked List: Insertions and deletions at any position in the linked list are more efficient since it only involves updating
pointers. No need to shift or resize elements.
❖ Array: Insertions or deletions in the middle of an array require shifting elements to accommodate the change, which can be
time-consuming.
Memory Utilization:
❖ Linked List: Memory is allocated dynamically for each node, allowing efficient utilization of memory. It avoids the need for a
fixed-size contiguous block of memory.
❖ Array: Requires a contiguous block of memory with a fixed size, leading to potential wasted memory if not fully utilized.
Data Link
struct structure_name {
struct structure_name { data_type member_name1;
data_type member_name1;
=
data_type member_name1;
data_type member_name1; struct structure_name *link ....
.... ....
.... struct structure_name *link
}; };
int main()
{
struct node * firstNode =NULL;
firstNode =malloc(sizeof(struct node));
firstNode ->a=10;
firstNode ->link=NULL;
printf("%d", firstNode ->a);
return 0;
}
Linked List
Creating a single Linked list
(Add second node) // Creating next node
#include <stdio.h> struct node *secondNode=malloc(sizeof(struct node));
#include <stdlib.h> secondNode->a=20;
struct node{ secondNode->link=NULL;
int a; printf("%d", secondNode->a);
struct node *link;
}; // Connecting the secondNode with the first node (head)
firstNode ->link = secondNode;
int main()
{ return 0;
struct node * firstNode =NULL; }
firstNode =malloc(sizeof(struct node));
firstNode ->a=10;
firstNode ->link=NULL;
printf("%d ", firstNode->a);
Linked List
Creating a single Linked list // Creating second node
struct node *secondNode=malloc(sizeof(struct node));
(Add third node) secondNode->a=20;
#include <stdio.h> secondNode->link=NULL;
#include <stdlib.h> printf("%d ", secondNode->a);
struct node{ // Connecting the secondNode with the first node (head)
int a; firstNode ->link = secondNode;
struct node *link;
}; // Creating third node
struct node *thirdNode=malloc(sizeof(struct node));
int main() thirdNode->a=30;
{ thirdNode->link=NULL;
struct node *firstNode=NULL; printf("%d ", thirdNode->a);
firstNode =malloc(sizeof(struct node)); // Connecting the thirNode with the second node (secondNode)
firstNode ->a=10;
firstNode ->link=NULL; // Two ways for connecting
printf("%d ", firstNode ->a); secondNode->link = thirdNode;
// firstNode ->link->link= thirdNode;
return 0;
}
Linked List
Creates a linked list for N elements and prints the elements
#include <stdio.h>
#include <stdlib.h> #Traversing the linked list
struct node{ struct node *currentNode;
int a; currentNode=head;
struct node *link; }; while(currentNode!=NULL)
int main(){ {
int N;
printf("%d ", currentNode->a);
scanf("%d",&N);
struct node *head=NULL;
currentNode=currentNode->link;
struct node *current=NULL; }
for(int i=0;i<N;i++){
#Creating Node return 0;
struct node *NewNode=malloc(sizeof(struct node)); }
NewNode->a=rand()%100; //scanf("%d",&NewNode->a);
NewNode->link=NULL;
#Creating Linkedlist
if(head==NULL) {
head=NewNode;
current=head; }
else {
current->link = NewNode;
current=NewNode; }
}
Linked List
Array
Thank You