SlideShare a Scribd company logo
Linked List (Introduction)
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at a contiguous
location; the elements are linked using pointers.
WhyLinked List?
Arrays can be used to store linear data of similar types, but arrays have the following limitations.
1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also, generally,
the allocated memory is equal to the upper limit irrespective of the usage.
2) Inserting a new element in an array of elements is expensive because the room has to be created for the new elements
and to create room existing elements have to be shifted.
For example, in a system, if we maintain a sorted list of IDs in an array id[].
id[] = [1000, 1010, 1050, 2000, 2040].
And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the elements after 1000
(excluding 1000).
Deletion is also expensive with arrays until unless some special techniques are used. For example, to delete 1010 in id[],
everything after 1010 has to be moved.
Advantagesover arrays
1) Dynamic size
2) Ease of insertion/deletion
Drawbacks:
1) Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do
binary search with linked lists efficiently with its default implementation. Read about it here.
2) Extra memory space for a pointer is required with each element of the list.
3) Not cache friendly. Since array elements are contiguous locations, there is locality of reference which is not there in case
of linked lists.
Representation:
A linked list is represented by a pointer to the first node of the linked list. The first node is called the head. If the linked list
is empty, then the value of the head is NULL.
Each node in a list consists of at least two parts:
1) data
2) Pointer (Or Reference) to the next node
In C, we can represent a node using structures. Below is an example of a linked list node with integer data.
// A linked list node
struct Node {
    int data;
    struct Node* next;
};
/
// A simple C program for traversal of a linked list
#include <stdio.h>
#include <stdlib.h>
struct Node {
    int data;
    struct Node* next;
};
 
// This function prints contents of linked list starting from
// the given node
void printList(struct Node* n)
{
    while (n != NULL) {
        printf(" %d ", n->data);
        n = n->next;
         }
}
 
int main()
{
    struct Node* head = NULL;
    struct Node* second = NULL;
    struct Node* third = NULL;
 
    // allocate 3 nodes in the heap
    head = (struct Node*)malloc(sizeof(struct Node));
    second = (struct Node*)malloc(sizeof(struct Node));
    third = (struct Node*)malloc(sizeof(struct Node));
 
    head->data = 1; // assign data in first node
    head->next = second; // Link first node with second
 
    second->data = 2; // assign data to second node
    second->next = third;
 
    third->data = 3; // assign data to third node
    third->next = NULL;
 
    printList(head);
 
    return 0;
    
    Output: 
 1  2  3
Difference between Singly linked list and
Doubly linked list
IntroductiontoSinglylinked list : A singly linked list is a set of nodes where each node has two fields ‘data’ and ‘link’.
The ‘data’ field stores actual piece of information and ‘link’ field is used to point to next node. Basically the ‘link’ field
stores the address of the next node.
IntroductiontoDoublylinked list : A DoublyLinked List (DLL) contains an extra pointer, typically called previous
pointer, together with next pointer and data which are there in singly linked list.
Singlylinked list vsDoublylinked list
Two-Way Header Lists:
The advantages of a two-way list a circular header list may be combined into a two-way circular header list as . The list is
circular because the two end nodes point back to the header node.Observe that such a two-way list requires only one list
pointer variable START, which points to the header node. This is because the two pointers in the header node point to the
two ends of the list.
What is a two way list?A two way list is a linear collection of data elements, called nodes, where each node N is divided into
three parts:- information field, Forward link- which points to the next node and Backward link-which points to the previous
node.
Two Way List
Singly linked list
(SLL)
Doubly linked list
(DLL)
D
SLL nodes
contains 2
field -data
field and next
link field.
DLL nodes
contains 3
fields -data
field, a
previous link
field and a
next link field.
In SLL, the
traversal can
be done using
the next node
link only. Thus
traversal is
possible in
one direction
only.
In DLL, the
traversal can
be done using
the previous
node link or
the next node
link. Thus
traversal is
possible in
both
directions
(forward and
backward).
The SLL
occupies less
memory than
DLL as it has
only 2 fields.
The DLL
occupies
more memory
than SLL as it
has 3 fields.
Complexity of
insertion and
deletion at a
given position
is O(n).
Complexity of
insertion and
deletion at a
given position
is O(1).
1
2
3
4
5
Doubly linked listDoubly linked list is a complex type of linked list in which a node contains a pointer to the previous as well
as the next node in the sequence. Therefore, in a doubly linked list, a node consists of three parts: node data, pointer to
the next node in sequence (next pointer) , pointer to the previous node (previous pointer). A sample node in a doubly
linked list is shown in the figure.
A doubly linked list containing three nodes having numbers from 1 to 3 in their data part, is shown in the following image.
In C, structure of a node in doubly linked list can be given as :
struct node  
{  
    struct node *prev;  
    int data;  
    struct node *next;  
}   
The prev part of the first node and the next part of the last node will always contain null indicating end in each direction.
In a singly linked list, we could traverse only in one direction, because each node contains address of the next node and it
doesn't have any record of its previous nodes. However, doubly linked list overcome this limitation of singly linked list.
Due to the fact that, each node of the list contains the address of its previous node, we can find all the details about the
previous node as well by using the previous address stored inside the previous part of each node.
Operations on doubly linked list
NodeCreation
1.
2.
3.
4.
5.
6.
7.
struct node
{
struct node *prev;
int data;
struct node *next;
};
struct node *head;
garbagecollectionand compactionindata structures
Garbagecollection: Marking unreachable memory blocks (garbage) as free. Compaction: Moving reachable memory
blocks close together, so that there are not free memory blocks between them. The garbagecollector will release any
garbage memory, without any need to actively free memory like C programmers do.
GarbageCollection. In computer science, garbagecollection is a type of memory management. It automatically cleans
up unused objects and pointers in memory, allowing the resources to be used again. Garbagecollection may also be
done at compile-time, when a program's source code is compiled into an executable program.
Waste compaction is the process of compacting waste, reducing it in size. Garbage compactors and waste collection
vehicles compress waste so that more of it can be stored in the same space. Waste is compacted again, more thoroughly, at
the landfill to conserve valuable airspace and to extend the landfill's life span.
What is garbage data?
Garbage is data that a software program stores in computer RAM (memory) that is no longer needed by that program.
This garbage data is often removed through a process called garbage collection to free up memory space.
Ad

More Related Content

What's hot (20)

Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and search
Estiak Khan
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Balwant Gorad
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
KristinaBorooah
 
Linked stacks and queues
Linked stacks and queuesLinked stacks and queues
Linked stacks and queues
Ramzi Alqrainy
 
Linked List
Linked ListLinked List
Linked List
Ashim Lamichhane
 
Singly link list
Singly link listSingly link list
Singly link list
Rojin Khadka
 
Link list
Link listLink list
Link list
Didar Rashad
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Link List
Link ListLink List
Link List
umiekalsum
 
linked list
linked listlinked list
linked list
Shaista Qadir
 
Link list
Link listLink list
Link list
Ravi Gautam
 
Linked Lists
Linked ListsLinked Lists
Linked Lists
Hafiz Umair
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
Reazul Islam
 
Linked list
Linked listLinked list
Linked list
Priyanka Rana
 
Linked lists
Linked listsLinked lists
Linked lists
SARITHA REDDY
 
Insertion in singly linked list
Insertion in singly linked listInsertion in singly linked list
Insertion in singly linked list
Keval Bhogayata
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
shameen khan
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
DurgaDeviCbit
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
Abdullah Al-hazmy
 
single linked list
single linked listsingle linked list
single linked list
Sathasivam Rangasamy
 
Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and search
Estiak Khan
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Balwant Gorad
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
KristinaBorooah
 
Linked stacks and queues
Linked stacks and queuesLinked stacks and queues
Linked stacks and queues
Ramzi Alqrainy
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
Reazul Islam
 
Insertion in singly linked list
Insertion in singly linked listInsertion in singly linked list
Insertion in singly linked list
Keval Bhogayata
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
shameen khan
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
DurgaDeviCbit
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
Abdullah Al-hazmy
 

Similar to Linked list (introduction) 1 (20)

1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
ssuserd2f031
 
linked list_MODULE 3.pptx ppt on the linked list
linked list_MODULE 3.pptx ppt on the linked listlinked list_MODULE 3.pptx ppt on the linked list
linked list_MODULE 3.pptx ppt on the linked list
AnuragKumar682871
 
link list.pptx complete notes detailed ans
link list.pptx complete notes detailed anslink list.pptx complete notes detailed ans
link list.pptx complete notes detailed ans
IqraHanif27
 
Linked List
Linked ListLinked List
Linked List
BHARATH KUMAR
 
Linked Lists, Single Linked list and its operations
Linked Lists, Single Linked list and its operationsLinked Lists, Single Linked list and its operations
Linked Lists, Single Linked list and its operations
BackiyalakshmiVenkat
 
Linked List
Linked ListLinked List
Linked List
CHANDAN KUMAR
 
Link list using array in Data structure amd algorithms
Link list using array in Data structure amd algorithmsLink list using array in Data structure amd algorithms
Link list using array in Data structure amd algorithms
pwstudent403
 
csc211_lecture_21.pptx
csc211_lecture_21.pptxcsc211_lecture_21.pptx
csc211_lecture_21.pptx
ASADAHMAD811380
 
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptxDSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
vaibhavkore8
 
Data Structure and Algorithms by Sabeen Memon03.pptx
Data Structure and Algorithms by Sabeen Memon03.pptxData Structure and Algorithms by Sabeen Memon03.pptx
Data Structure and Algorithms by Sabeen Memon03.pptx
msoomar8611
 
Linked List Representation of a Linked List.pptx
Linked List Representation of a Linked List.pptxLinked List Representation of a Linked List.pptx
Linked List Representation of a Linked List.pptx
AAUsH2
 
Data Structures-UNIT Four_Linked_List.pptx
Data Structures-UNIT Four_Linked_List.pptxData Structures-UNIT Four_Linked_List.pptx
Data Structures-UNIT Four_Linked_List.pptx
shilpar780389
 
Data Structures Introduction & Linear DS
Data Structures Introduction & Linear DSData Structures Introduction & Linear DS
Data Structures Introduction & Linear DS
sailaja156145
 
CS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdfCS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdf
raji175286
 
CS8391-DATA STRUCTURE.pdf1111111111111111
CS8391-DATA STRUCTURE.pdf1111111111111111CS8391-DATA STRUCTURE.pdf1111111111111111
CS8391-DATA STRUCTURE.pdf1111111111111111
kannanmeenu602
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
Sumathi Kv
 
ANOITO2341988888888888888888888885555.ppt
ANOITO2341988888888888888888888885555.pptANOITO2341988888888888888888888885555.ppt
ANOITO2341988888888888888888888885555.ppt
robertobula2
 
Data Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptxData Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptx
RushaliDeshmukh2
 
DSA-Linked-List-.. learning process.pptx
DSA-Linked-List-.. learning process.pptxDSA-Linked-List-.. learning process.pptx
DSA-Linked-List-.. learning process.pptx
ArgeeOnaler
 
Datastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptxDatastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptx
kaleeswaric3
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
ssuserd2f031
 
linked list_MODULE 3.pptx ppt on the linked list
linked list_MODULE 3.pptx ppt on the linked listlinked list_MODULE 3.pptx ppt on the linked list
linked list_MODULE 3.pptx ppt on the linked list
AnuragKumar682871
 
link list.pptx complete notes detailed ans
link list.pptx complete notes detailed anslink list.pptx complete notes detailed ans
link list.pptx complete notes detailed ans
IqraHanif27
 
Linked Lists, Single Linked list and its operations
Linked Lists, Single Linked list and its operationsLinked Lists, Single Linked list and its operations
Linked Lists, Single Linked list and its operations
BackiyalakshmiVenkat
 
Link list using array in Data structure amd algorithms
Link list using array in Data structure amd algorithmsLink list using array in Data structure amd algorithms
Link list using array in Data structure amd algorithms
pwstudent403
 
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptxDSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
vaibhavkore8
 
Data Structure and Algorithms by Sabeen Memon03.pptx
Data Structure and Algorithms by Sabeen Memon03.pptxData Structure and Algorithms by Sabeen Memon03.pptx
Data Structure and Algorithms by Sabeen Memon03.pptx
msoomar8611
 
Linked List Representation of a Linked List.pptx
Linked List Representation of a Linked List.pptxLinked List Representation of a Linked List.pptx
Linked List Representation of a Linked List.pptx
AAUsH2
 
Data Structures-UNIT Four_Linked_List.pptx
Data Structures-UNIT Four_Linked_List.pptxData Structures-UNIT Four_Linked_List.pptx
Data Structures-UNIT Four_Linked_List.pptx
shilpar780389
 
Data Structures Introduction & Linear DS
Data Structures Introduction & Linear DSData Structures Introduction & Linear DS
Data Structures Introduction & Linear DS
sailaja156145
 
CS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdfCS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdf
raji175286
 
CS8391-DATA STRUCTURE.pdf1111111111111111
CS8391-DATA STRUCTURE.pdf1111111111111111CS8391-DATA STRUCTURE.pdf1111111111111111
CS8391-DATA STRUCTURE.pdf1111111111111111
kannanmeenu602
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
Sumathi Kv
 
ANOITO2341988888888888888888888885555.ppt
ANOITO2341988888888888888888888885555.pptANOITO2341988888888888888888888885555.ppt
ANOITO2341988888888888888888888885555.ppt
robertobula2
 
Data Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptxData Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptx
RushaliDeshmukh2
 
DSA-Linked-List-.. learning process.pptx
DSA-Linked-List-.. learning process.pptxDSA-Linked-List-.. learning process.pptx
DSA-Linked-List-.. learning process.pptx
ArgeeOnaler
 
Datastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptxDatastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptx
kaleeswaric3
 
Ad

Recently uploaded (20)

Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
Building a research repository that works by Clare Cady
Building a research repository that works by Clare CadyBuilding a research repository that works by Clare Cady
Building a research repository that works by Clare Cady
UXPA Boston
 
Right to liberty and security of a person.pdf
Right to liberty and security of a person.pdfRight to liberty and security of a person.pdf
Right to liberty and security of a person.pdf
danielbraico197
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Middle East and Africa Cybersecurity Market Trends and Growth Analysis Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Preeti Jha
 
React Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for SuccessReact Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for Success
Amelia Swank
 
Top Hyper-Casual Game Studio Services
Top  Hyper-Casual  Game  Studio ServicesTop  Hyper-Casual  Game  Studio Services
Top Hyper-Casual Game Studio Services
Nova Carter
 
Google DeepMind’s New AI Coding Agent AlphaEvolve.pdf
Google DeepMind’s New AI Coding Agent AlphaEvolve.pdfGoogle DeepMind’s New AI Coding Agent AlphaEvolve.pdf
Google DeepMind’s New AI Coding Agent AlphaEvolve.pdf
derrickjswork
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
Computer Systems Quiz Presentation in Purple Bold Style (4).pdf
Computer Systems Quiz Presentation in Purple Bold Style (4).pdfComputer Systems Quiz Presentation in Purple Bold Style (4).pdf
Computer Systems Quiz Presentation in Purple Bold Style (4).pdf
fizarcse
 
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Alan Dix
 
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Vasileios Komianos
 
accessibility Considerations during Design by Rick Blair, Schneider Electric
accessibility Considerations during Design by Rick Blair, Schneider Electricaccessibility Considerations during Design by Rick Blair, Schneider Electric
accessibility Considerations during Design by Rick Blair, Schneider Electric
UXPA Boston
 
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
SOFTTECHHUB
 
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdfICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
Eryk Budi Pratama
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
Secondary Storage for a microcontroller system
Secondary Storage for a microcontroller systemSecondary Storage for a microcontroller system
Secondary Storage for a microcontroller system
fizarcse
 
Best 10 Free AI Character Chat Platforms
Best 10 Free AI Character Chat PlatformsBest 10 Free AI Character Chat Platforms
Best 10 Free AI Character Chat Platforms
Soulmaite
 
May Patch Tuesday
May Patch TuesdayMay Patch Tuesday
May Patch Tuesday
Ivanti
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
Building a research repository that works by Clare Cady
Building a research repository that works by Clare CadyBuilding a research repository that works by Clare Cady
Building a research repository that works by Clare Cady
UXPA Boston
 
Right to liberty and security of a person.pdf
Right to liberty and security of a person.pdfRight to liberty and security of a person.pdf
Right to liberty and security of a person.pdf
danielbraico197
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Middle East and Africa Cybersecurity Market Trends and Growth Analysis Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Middle East and Africa Cybersecurity Market Trends and Growth Analysis
Preeti Jha
 
React Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for SuccessReact Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for Success
Amelia Swank
 
Top Hyper-Casual Game Studio Services
Top  Hyper-Casual  Game  Studio ServicesTop  Hyper-Casual  Game  Studio Services
Top Hyper-Casual Game Studio Services
Nova Carter
 
Google DeepMind’s New AI Coding Agent AlphaEvolve.pdf
Google DeepMind’s New AI Coding Agent AlphaEvolve.pdfGoogle DeepMind’s New AI Coding Agent AlphaEvolve.pdf
Google DeepMind’s New AI Coding Agent AlphaEvolve.pdf
derrickjswork
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
Computer Systems Quiz Presentation in Purple Bold Style (4).pdf
Computer Systems Quiz Presentation in Purple Bold Style (4).pdfComputer Systems Quiz Presentation in Purple Bold Style (4).pdf
Computer Systems Quiz Presentation in Purple Bold Style (4).pdf
fizarcse
 
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Alan Dix
 
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Vasileios Komianos
 
accessibility Considerations during Design by Rick Blair, Schneider Electric
accessibility Considerations during Design by Rick Blair, Schneider Electricaccessibility Considerations during Design by Rick Blair, Schneider Electric
accessibility Considerations during Design by Rick Blair, Schneider Electric
UXPA Boston
 
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
SOFTTECHHUB
 
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdfICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
Eryk Budi Pratama
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
Secondary Storage for a microcontroller system
Secondary Storage for a microcontroller systemSecondary Storage for a microcontroller system
Secondary Storage for a microcontroller system
fizarcse
 
Best 10 Free AI Character Chat Platforms
Best 10 Free AI Character Chat PlatformsBest 10 Free AI Character Chat Platforms
Best 10 Free AI Character Chat Platforms
Soulmaite
 
May Patch Tuesday
May Patch TuesdayMay Patch Tuesday
May Patch Tuesday
Ivanti
 
Ad

Linked list (introduction) 1

  • 1. Linked List (Introduction) Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at a contiguous location; the elements are linked using pointers. WhyLinked List? Arrays can be used to store linear data of similar types, but arrays have the following limitations. 1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage. 2) Inserting a new element in an array of elements is expensive because the room has to be created for the new elements and to create room existing elements have to be shifted. For example, in a system, if we maintain a sorted list of IDs in an array id[]. id[] = [1000, 1010, 1050, 2000, 2040]. And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the elements after 1000 (excluding 1000). Deletion is also expensive with arrays until unless some special techniques are used. For example, to delete 1010 in id[], everything after 1010 has to be moved. Advantagesover arrays 1) Dynamic size 2) Ease of insertion/deletion Drawbacks: 1) Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do binary search with linked lists efficiently with its default implementation. Read about it here. 2) Extra memory space for a pointer is required with each element of the list. 3) Not cache friendly. Since array elements are contiguous locations, there is locality of reference which is not there in case of linked lists. Representation: A linked list is represented by a pointer to the first node of the linked list. The first node is called the head. If the linked list is empty, then the value of the head is NULL. Each node in a list consists of at least two parts: 1) data 2) Pointer (Or Reference) to the next node In C, we can represent a node using structures. Below is an example of a linked list node with integer data.
  • 2. // A linked list node struct Node {     int data;     struct Node* next; }; / // A simple C program for traversal of a linked list #include <stdio.h> #include <stdlib.h> struct Node {     int data;     struct Node* next; };   // This function prints contents of linked list starting from // the given node void printList(struct Node* n) {     while (n != NULL) {         printf(" %d ", n->data);         n = n->next;          } }   int main() {     struct Node* head = NULL;     struct Node* second = NULL;     struct Node* third = NULL;       // allocate 3 nodes in the heap
  • 3.     head = (struct Node*)malloc(sizeof(struct Node));     second = (struct Node*)malloc(sizeof(struct Node));     third = (struct Node*)malloc(sizeof(struct Node));       head->data = 1; // assign data in first node     head->next = second; // Link first node with second       second->data = 2; // assign data to second node     second->next = third;       third->data = 3; // assign data to third node     third->next = NULL;       printList(head);       return 0;          Output:   1  2  3 Difference between Singly linked list and Doubly linked list IntroductiontoSinglylinked list : A singly linked list is a set of nodes where each node has two fields ‘data’ and ‘link’. The ‘data’ field stores actual piece of information and ‘link’ field is used to point to next node. Basically the ‘link’ field stores the address of the next node. IntroductiontoDoublylinked list : A DoublyLinked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list.
  • 4. Singlylinked list vsDoublylinked list Two-Way Header Lists: The advantages of a two-way list a circular header list may be combined into a two-way circular header list as . The list is circular because the two end nodes point back to the header node.Observe that such a two-way list requires only one list pointer variable START, which points to the header node. This is because the two pointers in the header node point to the two ends of the list. What is a two way list?A two way list is a linear collection of data elements, called nodes, where each node N is divided into three parts:- information field, Forward link- which points to the next node and Backward link-which points to the previous node. Two Way List Singly linked list (SLL) Doubly linked list (DLL) D SLL nodes contains 2 field -data field and next link field. DLL nodes contains 3 fields -data field, a previous link field and a next link field. In SLL, the traversal can be done using the next node link only. Thus traversal is possible in one direction only. In DLL, the traversal can be done using the previous node link or the next node link. Thus traversal is possible in both directions (forward and backward). The SLL occupies less memory than DLL as it has only 2 fields. The DLL occupies more memory than SLL as it has 3 fields. Complexity of insertion and deletion at a given position is O(n). Complexity of insertion and deletion at a given position is O(1). 1 2 3 4 5
  • 5. Doubly linked listDoubly linked list is a complex type of linked list in which a node contains a pointer to the previous as well as the next node in the sequence. Therefore, in a doubly linked list, a node consists of three parts: node data, pointer to the next node in sequence (next pointer) , pointer to the previous node (previous pointer). A sample node in a doubly linked list is shown in the figure. A doubly linked list containing three nodes having numbers from 1 to 3 in their data part, is shown in the following image. In C, structure of a node in doubly linked list can be given as : struct node   {       struct node *prev;       int data;       struct node *next;   }    The prev part of the first node and the next part of the last node will always contain null indicating end in each direction. In a singly linked list, we could traverse only in one direction, because each node contains address of the next node and it doesn't have any record of its previous nodes. However, doubly linked list overcome this limitation of singly linked list. Due to the fact that, each node of the list contains the address of its previous node, we can find all the details about the previous node as well by using the previous address stored inside the previous part of each node. Operations on doubly linked list NodeCreation 1. 2. 3. 4. 5. 6. 7. struct node { struct node *prev; int data; struct node *next; }; struct node *head; garbagecollectionand compactionindata structures Garbagecollection: Marking unreachable memory blocks (garbage) as free. Compaction: Moving reachable memory blocks close together, so that there are not free memory blocks between them. The garbagecollector will release any garbage memory, without any need to actively free memory like C programmers do. GarbageCollection. In computer science, garbagecollection is a type of memory management. It automatically cleans up unused objects and pointers in memory, allowing the resources to be used again. Garbagecollection may also be done at compile-time, when a program's source code is compiled into an executable program.
  • 6. Waste compaction is the process of compacting waste, reducing it in size. Garbage compactors and waste collection vehicles compress waste so that more of it can be stored in the same space. Waste is compacted again, more thoroughly, at the landfill to conserve valuable airspace and to extend the landfill's life span. What is garbage data? Garbage is data that a software program stores in computer RAM (memory) that is no longer needed by that program. This garbage data is often removed through a process called garbage collection to free up memory space.
  翻译: