SlideShare a Scribd company logo
Linked Lists
Why linked lists
• Disadvantages of arrays as storage data structures:
– slow insertion in ordered array
– Expensive to insert or delete elements
– Fixed size
Linked lists
•A linked list, or one way list, is a linear collection of data elements, called
nodes, where the linear order is given by means of pointers.
•Each node is divided into two parts:
•The first part contains the information of the element, and
•The second part, called the link field or next pointer field, contains the
address of the next node in the list.
•The pointer of the last node contains a special value,called the null pointer.
•A pointer variable – called START which contains the address of the first
node.
•A special case is the list that has no nodes, such a list is called the null list
or empty list and is denoted by the null pointer in the variable START.
Linked list with 3 nodes
Start
Data Next
node node
Data
Data Next
node
node
A
data pointer
linked lists
•A linked list organizes a collection of data items (elements ) such that elements
can easily be added to and deleted from any position in the list.
•Only references To next elements are updated in insertion and deletion
operations.
•There is no need to copy or move large blocks of data to facilitate insertion and
deletion of elements.
•Lists grow dynamically.
Representation of Linked lists in memory
1
2
3
4
5
6
7
8
67
45
80
75
90
3
5
2
7
4
0
START START=3, INFO[3]=45
LINK[3]=2, INFO[2]=67
LINK[2]=5, INFO[5]=75
LINK[5]=4, INFO[4]=80
LINK[4]=7, INFO[7]=90
LINK[7]=0, NULL value, So the list has ended
INFO LINK
Traversing a linked lists
LIST be a linked list in memory stored in linear arrays INFO and LINK with START
pointing to the first element and NULL indicating the end of LIST.
We want to traverse LIST in order to process each node exactly once.
Pointer variable PTR points to the node that is currently being processed.
LINK[PTR] points to the next node to be processed.
Thus update PTR by the assignment
PTR : =LINK[PTR]
START
X
INFO LINK
PTR
Fig : PTR : = LINK[PTR]
Traversing a linked lists
For printing the information at each node of a linked list, must traverse the list.
Algorithm 5.1 : TRAVERSE(INFO, LINK, START)
Algorithm Prints the information at each node of the list.
1. Set PTR : =START.
2. Repeat steps 3 and 4 while PTR : ≠ NULL:
3. Write : INFO[PTR].
4. Set PTR : =LINK[PTR].
5. Exit.
Traversing a linked lists
For Finding the number NUM of elements in a linked list, must traverse the list.
Algorithm 5.1 : COUNT(INFO, LINK, START, NUM)
1. Set NUM: =0.
2. . Set PTR : =START.
3. Repeat steps 4 and 5 while PTR : ≠ NULL:
4. Set NUM : =NUM+1.
5. Set PTR : =LINK[PTR].
6. Exit.
Searching a linked lists
List is unsorted
Algorithm 5.2 : SEARCH(INFO, LINK, START, ITEM,LOC)
It finds the location LOC of the node where ITEM first appears in LIST,
or sets LOC=NULL.
1. Set PTR: =START.
2. Repeat step 3 while PTR !=NULL:
3. If ITEM=INFO[PTR] then:
Set LOC:=PTR and Exit.
Else
Set PTR := LINK[PTR].
4 Set LOC:= NULL.
5 Exit.
Searching a linked lists
List is sorted
Algorithm 5.3 : SEARCHSL(INFO, LINK, START, ITEM,LOC)
It finds the location LOC of the node where ITEM first appears in LIST,
or sets LOC=NULL.
1. Set PTR: =START.
2. Repeat step 3 while PTR !=NULL:
3. If ITEM>INFO[PTR] then:
Set PTR:= LINK[PTR].
Else If ITEM=INFO[PTR] then:
Set LOC:=PTR and Exit.
Else
Set LOC:= NULL. And Exit.
4 Set LOC:= NULL.
5 Exit.
Memory allocation: Garbage collection
• Memory space can be reused if a node is deleted from a list
– i.e deleted node can be made available for future use
• The operating system of a computer may periodically collect all the deleted space
on to the free storage list. Any technique which does this collection called garbage
collection
Sent to
avail list
Garbage
(Deleted Space)
Computer
programs
Periodic
Collector
…
Avail List (Free space)
Takes
space
avail list
Overflow and Underflow
• Overflow:
– Sometimes data are inserted into a data structure but there is no
available space.
– This situation is called overflow
– Example: In linked list overflow occurs when
• AVAIL= NULL and
• There is an insertion operation
• Underflow:
– Situation:
• Want to delete data from data structure that is empty.
– Example: In linked list overflow occurs when
• START = NULL and
• There is an deletion operation
Insertion into a linked list
• Node N is to be inserted in to the list between nodes A and B
• Three pointer fields are changed as follows:
1. The next pointer field of node A now points to the new node N, to which AVAIL previously
pointed.
2. AVAIL now point to the second node in the free pool, to which node N previously pointed.
3. The next pointer field of node N now points to node B, to which node A previously pointed.
START
Node
A
Node
B
(a) Before insertion
START
Node
A
Node
B
(a) After insertion
AVAIL
Node N
Free storage list
Inserting a new node
• Possible cases of Insert Node
1. Insert into an empty list
2. Insert in front
3. Insert at back
4. Insert in middle
• But, in fact, only need to handle two cases:
1. Insert as the first node (Case 1 and Case 2)
2. Insert in the middle or at the end of the list (Case 3 and Case 4)
Inserting a new node
INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)
1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit
2. Set NEW= AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]= ITEM
4. IF LOC = NULL then [Insert as first Node]
Set LINK[NEW]= START and START=NEW.
Else: [Insert after node with location LOC]
Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW
5. Exit.
Check for available
memory
Inserting a new node
INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)
1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit
2. Set NEW= AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]= ITEM
4. IF LOC = NULL then [Insert as first Node]
Set LINK[]= START and START=NEW.
Else: [Insert after node with location LOC]
Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW
5. Exit.
Create a new node
Inserting a new node
INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)
1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit
2. Set NEW= AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]= ITEM
4. IF LOC = NULL then [Insert as first Node]
Set LINK[NEW]= START and START=NEW.
Else: [Insert after node with location LOC]
Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW
5. Exit.
Insert as first element
START
NEW
Inserting a new node
INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM)
1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit
2. Set NEW= AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]= ITEM
4. IF LOC = NULL then [Insert as first Node]
Set LINK[NEW]= START and START=NEW.
Else: [Insert after node with location LOC]
Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW
5. Exit.
Insert after currNode
LOC
NEW

More Related Content

PDF
Bca data structures linked list mrs.sowmya jyothi
PPTX
linked list in dsa python (presentation)
PPTX
Engineering.CSE.DataStructure.Linkedlist.notes
PPT
Data structure lecture 5
PPT
linked list2.ppt linked list part 2 ppt
PPTX
5.Linked list
PPTX
DSModule2.pptx
PPTX
Ppt of operations on one way link list
Bca data structures linked list mrs.sowmya jyothi
linked list in dsa python (presentation)
Engineering.CSE.DataStructure.Linkedlist.notes
Data structure lecture 5
linked list2.ppt linked list part 2 ppt
5.Linked list
DSModule2.pptx
Ppt of operations on one way link list

Similar to linked list1.ppt linked list ppts and notes (20)

PPTX
Ppt of operations on one way link list
PPT
ds 4Linked lists.ppt
PPTX
Linked list (1).pptx
PPT
Unit ii(dsc++)
PDF
unit 2- PPT.pdf
PDF
unit 2- linked list- PPT for btech students.pdf
DOCX
Link list assi
PPTX
linked list in data structure
PDF
ds-lecture-4-171012041008 (1).pdf
PPTX
Data Structures_Linked List
PPTX
Linked List.pptx
PDF
DS Module 03.pdf
PPTX
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
PPT
linked list
PPT
Data Structures with C Linked List
PPTX
Data structures linked list introduction.pptx
PDF
linkrd_list.pdf
PPTX
data structures and applications power p
PPT
Linkedlists
Ppt of operations on one way link list
ds 4Linked lists.ppt
Linked list (1).pptx
Unit ii(dsc++)
unit 2- PPT.pdf
unit 2- linked list- PPT for btech students.pdf
Link list assi
linked list in data structure
ds-lecture-4-171012041008 (1).pdf
Data Structures_Linked List
Linked List.pptx
DS Module 03.pdf
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
linked list
Data Structures with C Linked List
Data structures linked list introduction.pptx
linkrd_list.pdf
data structures and applications power p
Linkedlists
Ad

Recently uploaded (20)

PDF
PPT on Performance Review to get promotions
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Current and future trends in Computer Vision.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
DOCX
573137875-Attendance-Management-System-original
PPTX
Artificial Intelligence
PPT
introduction to datamining and warehousing
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Geodesy 1.pptx...............................................
PDF
Digital Logic Computer Design lecture notes
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Lecture Notes Electrical Wiring System Components
PPT on Performance Review to get promotions
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Mechanical Engineering MATERIALS Selection
Current and future trends in Computer Vision.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
573137875-Attendance-Management-System-original
Artificial Intelligence
introduction to datamining and warehousing
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
R24 SURVEYING LAB MANUAL for civil enggi
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
bas. eng. economics group 4 presentation 1.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
OOP with Java - Java Introduction (Basics)
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Geodesy 1.pptx...............................................
Digital Logic Computer Design lecture notes
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Lecture Notes Electrical Wiring System Components
Ad

linked list1.ppt linked list ppts and notes

  • 2. Why linked lists • Disadvantages of arrays as storage data structures: – slow insertion in ordered array – Expensive to insert or delete elements – Fixed size
  • 3. Linked lists •A linked list, or one way list, is a linear collection of data elements, called nodes, where the linear order is given by means of pointers. •Each node is divided into two parts: •The first part contains the information of the element, and •The second part, called the link field or next pointer field, contains the address of the next node in the list. •The pointer of the last node contains a special value,called the null pointer. •A pointer variable – called START which contains the address of the first node. •A special case is the list that has no nodes, such a list is called the null list or empty list and is denoted by the null pointer in the variable START. Linked list with 3 nodes Start Data Next node node Data Data Next node node A data pointer
  • 4. linked lists •A linked list organizes a collection of data items (elements ) such that elements can easily be added to and deleted from any position in the list. •Only references To next elements are updated in insertion and deletion operations. •There is no need to copy or move large blocks of data to facilitate insertion and deletion of elements. •Lists grow dynamically.
  • 5. Representation of Linked lists in memory 1 2 3 4 5 6 7 8 67 45 80 75 90 3 5 2 7 4 0 START START=3, INFO[3]=45 LINK[3]=2, INFO[2]=67 LINK[2]=5, INFO[5]=75 LINK[5]=4, INFO[4]=80 LINK[4]=7, INFO[7]=90 LINK[7]=0, NULL value, So the list has ended INFO LINK
  • 6. Traversing a linked lists LIST be a linked list in memory stored in linear arrays INFO and LINK with START pointing to the first element and NULL indicating the end of LIST. We want to traverse LIST in order to process each node exactly once. Pointer variable PTR points to the node that is currently being processed. LINK[PTR] points to the next node to be processed. Thus update PTR by the assignment PTR : =LINK[PTR] START X INFO LINK PTR Fig : PTR : = LINK[PTR]
  • 7. Traversing a linked lists For printing the information at each node of a linked list, must traverse the list. Algorithm 5.1 : TRAVERSE(INFO, LINK, START) Algorithm Prints the information at each node of the list. 1. Set PTR : =START. 2. Repeat steps 3 and 4 while PTR : ≠ NULL: 3. Write : INFO[PTR]. 4. Set PTR : =LINK[PTR]. 5. Exit.
  • 8. Traversing a linked lists For Finding the number NUM of elements in a linked list, must traverse the list. Algorithm 5.1 : COUNT(INFO, LINK, START, NUM) 1. Set NUM: =0. 2. . Set PTR : =START. 3. Repeat steps 4 and 5 while PTR : ≠ NULL: 4. Set NUM : =NUM+1. 5. Set PTR : =LINK[PTR]. 6. Exit.
  • 9. Searching a linked lists List is unsorted Algorithm 5.2 : SEARCH(INFO, LINK, START, ITEM,LOC) It finds the location LOC of the node where ITEM first appears in LIST, or sets LOC=NULL. 1. Set PTR: =START. 2. Repeat step 3 while PTR !=NULL: 3. If ITEM=INFO[PTR] then: Set LOC:=PTR and Exit. Else Set PTR := LINK[PTR]. 4 Set LOC:= NULL. 5 Exit.
  • 10. Searching a linked lists List is sorted Algorithm 5.3 : SEARCHSL(INFO, LINK, START, ITEM,LOC) It finds the location LOC of the node where ITEM first appears in LIST, or sets LOC=NULL. 1. Set PTR: =START. 2. Repeat step 3 while PTR !=NULL: 3. If ITEM>INFO[PTR] then: Set PTR:= LINK[PTR]. Else If ITEM=INFO[PTR] then: Set LOC:=PTR and Exit. Else Set LOC:= NULL. And Exit. 4 Set LOC:= NULL. 5 Exit.
  • 11. Memory allocation: Garbage collection • Memory space can be reused if a node is deleted from a list – i.e deleted node can be made available for future use • The operating system of a computer may periodically collect all the deleted space on to the free storage list. Any technique which does this collection called garbage collection Sent to avail list Garbage (Deleted Space) Computer programs Periodic Collector … Avail List (Free space) Takes space avail list
  • 12. Overflow and Underflow • Overflow: – Sometimes data are inserted into a data structure but there is no available space. – This situation is called overflow – Example: In linked list overflow occurs when • AVAIL= NULL and • There is an insertion operation • Underflow: – Situation: • Want to delete data from data structure that is empty. – Example: In linked list overflow occurs when • START = NULL and • There is an deletion operation
  • 13. Insertion into a linked list • Node N is to be inserted in to the list between nodes A and B • Three pointer fields are changed as follows: 1. The next pointer field of node A now points to the new node N, to which AVAIL previously pointed. 2. AVAIL now point to the second node in the free pool, to which node N previously pointed. 3. The next pointer field of node N now points to node B, to which node A previously pointed. START Node A Node B (a) Before insertion START Node A Node B (a) After insertion AVAIL Node N Free storage list
  • 14. Inserting a new node • Possible cases of Insert Node 1. Insert into an empty list 2. Insert in front 3. Insert at back 4. Insert in middle • But, in fact, only need to handle two cases: 1. Insert as the first node (Case 1 and Case 2) 2. Insert in the middle or at the end of the list (Case 3 and Case 4)
  • 15. Inserting a new node INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM) 1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit 2. Set NEW= AVAIL and AVAIL=LINK[AVAIL] 3. Set INFO[NEW]= ITEM 4. IF LOC = NULL then [Insert as first Node] Set LINK[NEW]= START and START=NEW. Else: [Insert after node with location LOC] Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW 5. Exit. Check for available memory
  • 16. Inserting a new node INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM) 1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit 2. Set NEW= AVAIL and AVAIL=LINK[AVAIL] 3. Set INFO[NEW]= ITEM 4. IF LOC = NULL then [Insert as first Node] Set LINK[]= START and START=NEW. Else: [Insert after node with location LOC] Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW 5. Exit. Create a new node
  • 17. Inserting a new node INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM) 1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit 2. Set NEW= AVAIL and AVAIL=LINK[AVAIL] 3. Set INFO[NEW]= ITEM 4. IF LOC = NULL then [Insert as first Node] Set LINK[NEW]= START and START=NEW. Else: [Insert after node with location LOC] Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW 5. Exit. Insert as first element START NEW
  • 18. Inserting a new node INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM) 1. [OVERFLOW?] If AVAIL=NULL, then print OVERFLOW and exit 2. Set NEW= AVAIL and AVAIL=LINK[AVAIL] 3. Set INFO[NEW]= ITEM 4. IF LOC = NULL then [Insert as first Node] Set LINK[NEW]= START and START=NEW. Else: [Insert after node with location LOC] Set LINK[NEW]= LINK [LOC] and LINK[LOC]= NEW 5. Exit. Insert after currNode LOC NEW