SlideShare uma empresa Scribd logo
1 de 59
Unit 4
LINKED LIST
Introduction to List and Linked Lists
• List is a term used to refer to a linear collection of data items. A List can be
implemented either by using arrays or linked lists.
• Usually, a large block of memory is occupied by an array which may not be
in use and it is difficult to increase the size of an array.
• Another way of storing a list is to have each element in a list contain a field
called a link or pointer, which contains the address of the next element in
the list.
• The successive elements in the list need not occupy adjacent space in
memory. This type of data structure is called a linked list.
Ashim Lamichhane 2
Linked List
• It is the most commonly used data structure used to store similar type
of data in memory.
• The elements of a linked list are not stored in adjacent memory
locations as in arrays.
• It is a linear collection of data elements, called nodes, where the
linear order is implemented by means of pointers.
Ashim Lamichhane 3
Linked List
• In a linear or single-linked list, a node is connected to the next node by
a single link.
• A node in this type of linked list contains two types of fields
• data: which holds a list element
• next: which stores a link (i.e. pointer) to the next node in the list.
Ashim Lamichhane 4
Linked List
• The structure defined for a single linked list is implemented as follows:
• The structure declared for linear linked list holds two members
• An integer type variable ‘data’ which holds the elements and
• Another type ‘node’, which has next, which stores the address of the next
node in the list.
struct Node{
int info;
struct Node * next;
}
Ashim Lamichhane 5
Figurative Representation
Ashim Lamichhane 6
Properties of Linked list
• The nodes in a linked list are not stored contiguously in the memory
• You don’t have to shift any element in the list
• Memory for each node can be allocated dynamically whenever the
need arises.
• The size of a linked list can grow or shrink dynamically
Ashim Lamichhane 7
Operations on Linked List
• Creation:
• This operation is used to create a linked list
• Insertion / Deletion
• At/From the beginning of the linked list
• At/From the end of the linked list
• At/From the specified position in a linked list
• Traversing:
• Traversing may be either forward or backward
• Searching:
• Finding an element in a linked list
• Concatenation:
• The process of appending second list to the end of the first list
Ashim Lamichhane 8
Types of Linked List
• Singly Linked List
• Doubly linked list
• Circular linked list
• Circular doubly linked list
Ashim Lamichhane 9
Singly Linked List
• A singly linked list is a dynamic data structure which may grow or
shrink, and growing and shrinking depends on the operation made.
• In this type of linked list each node contains two fields one is data field
which is used to store the data items and another is next field that is
used to point the next node in the list.
5 3 8 null
next next nextinfo info info
Ashim Lamichhane 10
Creating a Linked List
• The head pointer is used to create and access unnamed nodes.
• The above statement obtains memory to store a node and assigns its
address to head which is a pointer variable.
struct Node{
int info;
struct Node* next;
};
typedef struct Node NodeType;
NodeType* head;
head=(NodeType *) malloc (sizeof( NodeType ) );
Ashim Lamichhane 11
Creating a Node
• To create a new node, we use the malloc function to dynamically
allocate memory for the new node.
• After creating the node, we can store the new item in the node using a
pointer to that node.
• Note that p is not a node; instead it is a pointer to a node.
Nodetype *p;
p=(NodeType *) malloc (sizeof( NodeType ) );
p->info=50;
p->next = NULL;
Ashim Lamichhane 12
Creating an empty list
void createEmptyList(NodeType *head)
{
head=NULL;
}
OR SIMPLY
NodeType *head =Null;
Ashim Lamichhane 13
Inserting an Element
• While inserting an element or a node in a linked list, we have to do
following things:
• Allocate a node
• Assign a data to info field of the node.
• Adjust a pointer
• We can insert an element in following places
• At the beginning of the linked list
• At the end of the linked list
• At the specified position in a linked list
Ashim Lamichhane 14
An algorithm to insert a node at the beginning of the singly linked list
Let *head be the pointer to first node in the current list
1. Create a new node using malloc function
NewNode=(NodeType*)malloc(sizeof(NodeType));
2. Assign data to the info field of new node
NewNode->info=newItem;
3. Set next of new node to head
NewNode->next=head;
4. Set the head pointer to the new node
head=NewNode;
5. End
Ashim Lamichhane 15
Inserting a node at the beginning of the singly linked list
Ashim Lamichhane 16
An algorithm to insert a node at the end of the singly linked list
let *head be the pointer to first node in the current list
1. Create a new node using malloc function
NewNode=(NodeType*)malloc(sizeof(NodeType));
2. Assign data to the info field of new node
NewNode->info=newItem;
3. Set next of new node to NULL
NewNode->next=NULL;
4. if (head ==NULL) then
Set head =NewNode.and exit.
5. Set temp=head;
6. while(temp->next!=NULL)
temp=temp->next; //increment temp
7. Set temp->next=NewNode;
8. End
Ashim Lamichhane 17
Ashim Lamichhane 18
An algorithm to insert a node after the given node in singly linked list
let *head be the pointer to first node in the current list and *p be the
pointer to the node after which we want to insert a new node.
1. Create a new node using malloc function
NewNode=(NodeType*)malloc(sizeof(NodeType));
2. Assign data to the info field of new node
NewNode->info=newItem;
3. Set next of new node to next of p
NewNode->next=p->next;
4. Set next of p to NewNode
p->next =NewNode
5. End
Ashim Lamichhane 19
Ashim Lamichhane 20
An algorithm to insert a node at the specified position in a linked list
let *head be the pointer to first node in the current list
1. Create a new node using malloc function
NewNode=(NodeType*)malloc(sizeof(NodeType));
2. Assign data to the info field of new node
NewNode->info=newItem;
3. Enter position of a node at which you want to insert a new node. Let this position is pos.
4. Set temp=head;
5. if (head ==NULL)then
printf(“void insertion”); and exit(1).
6. for(i=1; i<pos; i++)
temp=temp->next;
7. Set NewNode->next=temp->next;
set temp->next =NewNode..
8. End
Ashim Lamichhane 21
Deleting Nodes
• A node may be deleted:
• From the beginning of the linked list
• From the end of the linked list
• From the specified position in a linked list
Ashim Lamichhane 22
Ashim Lamichhane 23
Deleting first node of the linked list
Ashim Lamichhane 24
Ashim Lamichhane 25
Ashim Lamichhane 26
Ashim Lamichhane 27
Searching an item in a linked list
• Let *head be the pointer to first node in the current list
1. If head==Null
Print “Empty List”
2. Else, enter an item to be searched as key
3. Set temp==head
4. While temp!=Null
If (temp->info == key)
Print “search success”
temp=temp->next;
5. If temp==Null
Print “Unsuccessful search”
Ashim Lamichhane 28
Ashim Lamichhane 29
Linked list implementation of Stack
Ashim Lamichhane 30
Ashim Lamichhane 31
Linked list implementation of queue
Ashim Lamichhane 32
Ashim Lamichhane 33
Circular Linked List
• A circular linked list is a list where the link field of last node points to
the very first node of the list.
• Complicated linked data structure.
• A circular list is very similar to the linear list where in the circular list
pointer of the last node points not Null but the first node.
Ashim Lamichhane 34
C representation of circular linked list
• We declare structure for the circular linked list in the same way as
linear linked list.
Ashim Lamichhane 35
Algorithms to insert a node in a circular linked list
Ashim Lamichhane 36
Algorithm to insert a node at the end of a circular linked list
Ashim Lamichhane 37
Algorithms to delete a node from a circular linked list
Ashim Lamichhane 38
Ashim Lamichhane 39
Stack as a circular List
• To implement a stack in a circular linked list, let pstack be a pointer to
the last node of a circular list.
• There is no any end of a list but for convention let us assume that the
first node(rightmost node of a list) is the top of the stack.
• An empty stack is represented by a null list.
Ashim Lamichhane 40
Stack as a circular List
Ashim Lamichhane 41
Ashim Lamichhane 42
Ashim Lamichhane 43
Ashim Lamichhane 44
Queue as a circular List
• It is easier to represent a queue as a circular list than as a linear list.
• As a linear list a queue is specified by two pointers, one to the front of
the list and the other to its rear.
• However, by using a circular list, a queue may be specified by a single
pointer pq to that list.
• node(pq) is the rear of the queue and the following node is its front.
Ashim Lamichhane 45
Queue as a circular List
Ashim Lamichhane 46
Queue as a circular List
Ashim Lamichhane 47
Queue as a circular List : Deletion function
Ashim Lamichhane 48
Doubly Linked List
• A linked list in which all nodes are linked together by multiple number
of links i.e. each node contains three fields (two pointer fields and one
data field) rather than two fields is called doubly linked list.
• It provides bidirectional traversal.
Ashim Lamichhane 49
Ashim Lamichhane 50
Ashim Lamichhane 51
Ashim Lamichhane 52
Ashim Lamichhane 53
Ashim Lamichhane 54
Circular Doubly Linked List
• A circular doubly linked list is one which has the successor and predecessor
pointer in circular manner.
• It is a doubly linked list where the next link of last node points to the first node and
previous link of first node points to last node of the list.
• The main objective of considering circular doubly linked list is to simplify the
insertion and deletion operations performed on doubly linked list.
Ashim Lamichhane 55
Ashim Lamichhane 56
Ashim Lamichhane 57
Assignments
• Slides at Slideshare
http://www.slideshare.net/AshimLamichhane/linked-list-63790316
• Assignments at github
https://github.com/ashim888/dataStructureAndAlgorithm
Ashim Lamichhane 58
Reference
• http://www.learn-c.org/en/Linked_lists
• http://www.tutorialspoint.com/data_structures_algorithms/linked_list
_algorithms.htm
• http://www.studytonight.com/data-structures/introduction-to-linked-
list
• http://www.tutorialspoint.com/data_structures_algorithms/circular_li
nked_list_algorithm.htm
• http://www.studytonight.com/data-structures/circular-linked-list
• http://geekswithblogs.net/MarkPearl/archive/2010/02/20/linked-lists-
in-c.aspx
Ashim Lamichhane 59

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Stack
StackStack
Stack
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
Selection sort
Selection sortSelection sort
Selection sort
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
stack presentation
stack presentationstack presentation
stack presentation
 
Java Linked List Tutorial | Edureka
Java Linked List Tutorial |  EdurekaJava Linked List Tutorial |  Edureka
Java Linked List Tutorial | Edureka
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Linked list
Linked listLinked list
Linked list
 
Data structures using c
Data structures using cData structures using c
Data structures using c
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Circular queue
Circular queueCircular queue
Circular queue
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
Singly link list
Singly link listSingly link list
Singly link list
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 

Destaque (20)

Sorting
SortingSorting
Sorting
 
Queues
QueuesQueues
Queues
 
Searching
SearchingSearching
Searching
 
Linked lists
Linked listsLinked lists
Linked lists
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
Unit 11. Graphics
Unit 11. GraphicsUnit 11. Graphics
Unit 11. Graphics
 
Introduction to data_structure
Introduction to data_structureIntroduction to data_structure
Introduction to data_structure
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
 
Algorithm Introduction
Algorithm IntroductionAlgorithm Introduction
Algorithm Introduction
 
Link List
Link ListLink List
Link List
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
Linked lists
Linked listsLinked lists
Linked lists
 
linked list
linked list linked list
linked list
 
Linked list
Linked listLinked list
Linked list
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Queue
QueueQueue
Queue
 
Friedman two way analysis of variance by
Friedman two way analysis of  variance byFriedman two way analysis of  variance by
Friedman two way analysis of variance by
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
 

Semelhante a Linked List

Semelhante a Linked List (20)

Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
 
Data Structures 3
Data Structures 3Data Structures 3
Data Structures 3
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
 
single linked list
single linked listsingle linked list
single linked list
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
 
Linked list
Linked listLinked list
Linked list
 
Link list assi
Link list assiLink list assi
Link list assi
 
data structures lists operation of lists
data structures lists operation of listsdata structures lists operation of lists
data structures lists operation of lists
 
VCE Unit 02 (1).pptx
VCE Unit 02 (1).pptxVCE Unit 02 (1).pptx
VCE Unit 02 (1).pptx
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
 
mbit_Unit-2_Linked List.pptx
mbit_Unit-2_Linked List.pptxmbit_Unit-2_Linked List.pptx
mbit_Unit-2_Linked List.pptx
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
 
Linked List Presentation in data structurepptx
Linked List Presentation in data structurepptxLinked List Presentation in data structurepptx
Linked List Presentation in data structurepptx
 
LinkedList Presentation.pptx
LinkedList Presentation.pptxLinkedList Presentation.pptx
LinkedList Presentation.pptx
 

Mais de Ashim Lamichhane

Mais de Ashim Lamichhane (10)

Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
 
Unit 8. Pointers
Unit 8. PointersUnit 8. Pointers
Unit 8. Pointers
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
Unit 5. Control Statement
Unit 5. Control StatementUnit 5. Control Statement
Unit 5. Control Statement
 
Unit 4. Operators and Expression
Unit 4. Operators and Expression  Unit 4. Operators and Expression
Unit 4. Operators and Expression
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer   Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer
 

Último

Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 

Último (20)

Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 

Linked List

  • 2. Introduction to List and Linked Lists • List is a term used to refer to a linear collection of data items. A List can be implemented either by using arrays or linked lists. • Usually, a large block of memory is occupied by an array which may not be in use and it is difficult to increase the size of an array. • Another way of storing a list is to have each element in a list contain a field called a link or pointer, which contains the address of the next element in the list. • The successive elements in the list need not occupy adjacent space in memory. This type of data structure is called a linked list. Ashim Lamichhane 2
  • 3. Linked List • It is the most commonly used data structure used to store similar type of data in memory. • The elements of a linked list are not stored in adjacent memory locations as in arrays. • It is a linear collection of data elements, called nodes, where the linear order is implemented by means of pointers. Ashim Lamichhane 3
  • 4. Linked List • In a linear or single-linked list, a node is connected to the next node by a single link. • A node in this type of linked list contains two types of fields • data: which holds a list element • next: which stores a link (i.e. pointer) to the next node in the list. Ashim Lamichhane 4
  • 5. Linked List • The structure defined for a single linked list is implemented as follows: • The structure declared for linear linked list holds two members • An integer type variable ‘data’ which holds the elements and • Another type ‘node’, which has next, which stores the address of the next node in the list. struct Node{ int info; struct Node * next; } Ashim Lamichhane 5
  • 7. Properties of Linked list • The nodes in a linked list are not stored contiguously in the memory • You don’t have to shift any element in the list • Memory for each node can be allocated dynamically whenever the need arises. • The size of a linked list can grow or shrink dynamically Ashim Lamichhane 7
  • 8. Operations on Linked List • Creation: • This operation is used to create a linked list • Insertion / Deletion • At/From the beginning of the linked list • At/From the end of the linked list • At/From the specified position in a linked list • Traversing: • Traversing may be either forward or backward • Searching: • Finding an element in a linked list • Concatenation: • The process of appending second list to the end of the first list Ashim Lamichhane 8
  • 9. Types of Linked List • Singly Linked List • Doubly linked list • Circular linked list • Circular doubly linked list Ashim Lamichhane 9
  • 10. Singly Linked List • A singly linked list is a dynamic data structure which may grow or shrink, and growing and shrinking depends on the operation made. • In this type of linked list each node contains two fields one is data field which is used to store the data items and another is next field that is used to point the next node in the list. 5 3 8 null next next nextinfo info info Ashim Lamichhane 10
  • 11. Creating a Linked List • The head pointer is used to create and access unnamed nodes. • The above statement obtains memory to store a node and assigns its address to head which is a pointer variable. struct Node{ int info; struct Node* next; }; typedef struct Node NodeType; NodeType* head; head=(NodeType *) malloc (sizeof( NodeType ) ); Ashim Lamichhane 11
  • 12. Creating a Node • To create a new node, we use the malloc function to dynamically allocate memory for the new node. • After creating the node, we can store the new item in the node using a pointer to that node. • Note that p is not a node; instead it is a pointer to a node. Nodetype *p; p=(NodeType *) malloc (sizeof( NodeType ) ); p->info=50; p->next = NULL; Ashim Lamichhane 12
  • 13. Creating an empty list void createEmptyList(NodeType *head) { head=NULL; } OR SIMPLY NodeType *head =Null; Ashim Lamichhane 13
  • 14. Inserting an Element • While inserting an element or a node in a linked list, we have to do following things: • Allocate a node • Assign a data to info field of the node. • Adjust a pointer • We can insert an element in following places • At the beginning of the linked list • At the end of the linked list • At the specified position in a linked list Ashim Lamichhane 14
  • 15. An algorithm to insert a node at the beginning of the singly linked list Let *head be the pointer to first node in the current list 1. Create a new node using malloc function NewNode=(NodeType*)malloc(sizeof(NodeType)); 2. Assign data to the info field of new node NewNode->info=newItem; 3. Set next of new node to head NewNode->next=head; 4. Set the head pointer to the new node head=NewNode; 5. End Ashim Lamichhane 15
  • 16. Inserting a node at the beginning of the singly linked list Ashim Lamichhane 16
  • 17. An algorithm to insert a node at the end of the singly linked list let *head be the pointer to first node in the current list 1. Create a new node using malloc function NewNode=(NodeType*)malloc(sizeof(NodeType)); 2. Assign data to the info field of new node NewNode->info=newItem; 3. Set next of new node to NULL NewNode->next=NULL; 4. if (head ==NULL) then Set head =NewNode.and exit. 5. Set temp=head; 6. while(temp->next!=NULL) temp=temp->next; //increment temp 7. Set temp->next=NewNode; 8. End Ashim Lamichhane 17
  • 19. An algorithm to insert a node after the given node in singly linked list let *head be the pointer to first node in the current list and *p be the pointer to the node after which we want to insert a new node. 1. Create a new node using malloc function NewNode=(NodeType*)malloc(sizeof(NodeType)); 2. Assign data to the info field of new node NewNode->info=newItem; 3. Set next of new node to next of p NewNode->next=p->next; 4. Set next of p to NewNode p->next =NewNode 5. End Ashim Lamichhane 19
  • 21. An algorithm to insert a node at the specified position in a linked list let *head be the pointer to first node in the current list 1. Create a new node using malloc function NewNode=(NodeType*)malloc(sizeof(NodeType)); 2. Assign data to the info field of new node NewNode->info=newItem; 3. Enter position of a node at which you want to insert a new node. Let this position is pos. 4. Set temp=head; 5. if (head ==NULL)then printf(“void insertion”); and exit(1). 6. for(i=1; i<pos; i++) temp=temp->next; 7. Set NewNode->next=temp->next; set temp->next =NewNode.. 8. End Ashim Lamichhane 21
  • 22. Deleting Nodes • A node may be deleted: • From the beginning of the linked list • From the end of the linked list • From the specified position in a linked list Ashim Lamichhane 22
  • 24. Deleting first node of the linked list Ashim Lamichhane 24
  • 28. Searching an item in a linked list • Let *head be the pointer to first node in the current list 1. If head==Null Print “Empty List” 2. Else, enter an item to be searched as key 3. Set temp==head 4. While temp!=Null If (temp->info == key) Print “search success” temp=temp->next; 5. If temp==Null Print “Unsuccessful search” Ashim Lamichhane 28
  • 30. Linked list implementation of Stack Ashim Lamichhane 30
  • 32. Linked list implementation of queue Ashim Lamichhane 32
  • 34. Circular Linked List • A circular linked list is a list where the link field of last node points to the very first node of the list. • Complicated linked data structure. • A circular list is very similar to the linear list where in the circular list pointer of the last node points not Null but the first node. Ashim Lamichhane 34
  • 35. C representation of circular linked list • We declare structure for the circular linked list in the same way as linear linked list. Ashim Lamichhane 35
  • 36. Algorithms to insert a node in a circular linked list Ashim Lamichhane 36
  • 37. Algorithm to insert a node at the end of a circular linked list Ashim Lamichhane 37
  • 38. Algorithms to delete a node from a circular linked list Ashim Lamichhane 38
  • 40. Stack as a circular List • To implement a stack in a circular linked list, let pstack be a pointer to the last node of a circular list. • There is no any end of a list but for convention let us assume that the first node(rightmost node of a list) is the top of the stack. • An empty stack is represented by a null list. Ashim Lamichhane 40
  • 41. Stack as a circular List Ashim Lamichhane 41
  • 45. Queue as a circular List • It is easier to represent a queue as a circular list than as a linear list. • As a linear list a queue is specified by two pointers, one to the front of the list and the other to its rear. • However, by using a circular list, a queue may be specified by a single pointer pq to that list. • node(pq) is the rear of the queue and the following node is its front. Ashim Lamichhane 45
  • 46. Queue as a circular List Ashim Lamichhane 46
  • 47. Queue as a circular List Ashim Lamichhane 47
  • 48. Queue as a circular List : Deletion function Ashim Lamichhane 48
  • 49. Doubly Linked List • A linked list in which all nodes are linked together by multiple number of links i.e. each node contains three fields (two pointer fields and one data field) rather than two fields is called doubly linked list. • It provides bidirectional traversal. Ashim Lamichhane 49
  • 55. Circular Doubly Linked List • A circular doubly linked list is one which has the successor and predecessor pointer in circular manner. • It is a doubly linked list where the next link of last node points to the first node and previous link of first node points to last node of the list. • The main objective of considering circular doubly linked list is to simplify the insertion and deletion operations performed on doubly linked list. Ashim Lamichhane 55
  • 58. Assignments • Slides at Slideshare http://www.slideshare.net/AshimLamichhane/linked-list-63790316 • Assignments at github https://github.com/ashim888/dataStructureAndAlgorithm Ashim Lamichhane 58
  • 59. Reference • http://www.learn-c.org/en/Linked_lists • http://www.tutorialspoint.com/data_structures_algorithms/linked_list _algorithms.htm • http://www.studytonight.com/data-structures/introduction-to-linked- list • http://www.tutorialspoint.com/data_structures_algorithms/circular_li nked_list_algorithm.htm • http://www.studytonight.com/data-structures/circular-linked-list • http://geekswithblogs.net/MarkPearl/archive/2010/02/20/linked-lists- in-c.aspx Ashim Lamichhane 59