SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Linked list
Linked listLinked list
Linked list
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Double Linked List (Algorithm)
Double Linked List (Algorithm)Double Linked List (Algorithm)
Double Linked List (Algorithm)
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Queues
QueuesQueues
Queues
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Singly link list
Singly link listSingly link list
Singly link list
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
Linked List
Linked ListLinked List
Linked List
 
List in Python
List in PythonList in Python
List in Python
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 

Andere mochten auch (20)

Sorting
SortingSorting
Sorting
 
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
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 

Ähnlich wie Linked List

Ähnlich wie 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
 

Mehr von Ashim Lamichhane

Mehr von 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
 

Kürzlich hochgeladen

Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEaurabinda banchhor
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 

Kürzlich hochgeladen (20)

Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSE
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 

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