SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

Tendances (20)

Linked lists
Linked listsLinked lists
Linked lists
 
Singly link list
Singly link listSingly link list
Singly link list
 
Linked list
Linked listLinked list
Linked list
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Linked list
Linked listLinked list
Linked list
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Linked list
Linked listLinked list
Linked list
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
Double Linked List (Algorithm)
Double Linked List (Algorithm)Double Linked List (Algorithm)
Double Linked List (Algorithm)
 
stack presentation
stack presentationstack presentation
stack presentation
 

En vedette

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
Iybaro Reyes
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
dchuynh
 

En vedette (20)

Sorting
SortingSorting
Sorting
 
Queues
QueuesQueues
Queues
 
Searching
SearchingSearching
Searching
 
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
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
 

Similaire à Linked List

Similaire à 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
 
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
 
Linked list
Linked listLinked list
Linked list
 

Plus de Ashim Lamichhane

Plus de Ashim Lamichhane (9)

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
 

Dernier

Dernier (20)

NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
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
 
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...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
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)
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
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...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
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_...
 

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