0% found this document useful (0 votes)
16 views

Linked List Slide

The document discusses linked lists, which are linear data structures where each element points to the next. Linked lists have dynamic sizes and allow efficient insertion and deletion anywhere unlike arrays. The document explains different types of linked lists and their advantages over arrays.

Uploaded by

sijan4222
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Linked List Slide

The document discusses linked lists, which are linear data structures where each element points to the next. Linked lists have dynamic sizes and allow efficient insertion and deletion anywhere unlike arrays. The document explains different types of linked lists and their advantages over arrays.

Uploaded by

sijan4222
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Linked List

Dr. Md. Atiqur Rahman


Assistant Professor
Dept. of Computer Science & Engineering
Linked List
A linked list (also called self referential) is a linear data structure that consists of a sequence of elements, where each
element points to the next element in the sequence. Unlike arrays, linked lists do not have a fixed size, and the memory
for elements is allocated dynamically.

Each element in a linked list is called a "node," and each node has two components:

❖ Data: The actual data or value stored in the node.


❖ Next (or Link): A reference or pointer to the next node in the sequence.

The last node in a linked list typically has a reference of null or None to signify the end of the list.

There are various types of linked lists, including:

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.

No Need for Pre-allocation:


❖ Linked List: Elements can be added without the need to pre-allocate a specific size, making it suitable for scenarios with
unpredictable data sizes.
❖ Array: Requires pre-allocating a specific size, and resizing might be necessary if the size is exceeded.
Linked List
Node

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
}; };

Node of a linked list


Linked List
Suppose we want to store a list of numbers in a linked list, and the numbers are 10, 20, 30

Node-1 Node-2 Node-3

10 2000 20 3000 30 NULL


struct numbers {
int a;
1000 2000 3000 struct numbers *link
};
1000
Head Declaration
This is called a Single Linked List
Linked List
Array: Creating a single Linked list
10 20 30 #include <stdio.h>
#include <stdlib.h>
1000 1004 1008 struct node{
int a;
struct node *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

You might also like