SlideShare a Scribd company logo
1 of 26
Download to read offline
Linked Lists
Sesion 03
Ing. Gómez Marı́n, Jaime1
1Desarrollo y Diseño de Software
Departamento de TdG
September 2020
JGM Data Structure and Algorithmic
Table of Contents
Introduction
Arrays Overview
Advantages and Disadvantages of Arrays
Dynamic Arrays Overview
Linked Lists
Advantages and Disadvantages of Linked Lists
Linked Lists vs Arrays vs Dynamic Arrays
Singly Linked Lists
Conclusion
Bibliography
JGM Data Structure and Algorithmic
Introduction
In this session, we will learn about Linked Lists, which is a very
common dynamic data structure that is used to create other data
structures likes tree, graphs, hashing, etc.
JGM Data Structure and Algorithmic
Arrays Overview
The arrays need one memory block for hold the elements of
the array.
The array elements can be accessed in constant time by using
the index of the particular element.
To access an array element, the address of an element is
computed as an offset from the base address of the array
JGM Data Structure and Algorithmic
Advantages and Disadvantages of Arrays
Advantages
Simple and easy to use
Faster access to the elements
Disadvantages
Fixed size
One block allocation
Complex position-based insertion.
JGM Data Structure and Algorithmic
Dynamic Arrays Overview
Dynamic array is a random access, variable-size list data structure
that allows elements to be added or removed.
Start the dynamic array with a fixed size.
As soon as that array becomes full, create the new array
double the size of the original array.
In other case, reduce the array size to half if the elements in
the array are less than half.
JGM Data Structure and Algorithmic
Linked Lists
It is a data structure user for storing collections of data, its
characteristics are:
Successive elements are connected by pointers.
The last element points to NULL.
Can grow or shrink in size during execution of a program
Does not waste memory space, but takes some extra memory
for pointers.
Figure: 01
JGM Data Structure and Algorithmic
Advantages and Disadvantages of Linked Lists
Advantages
The dynamic allocation of storage is a great advantage.
We can start with space for just one allocated elements and
add on new elements.
Disadvantages
Linked Lists take O(n) for access to an element in the list in
the worst case.
Do not have spacial locality in memory and lost the benefits
from modern CPU caching methods.
There are overhead with storing and retrieving data.
Linked lists waste memory in terms of extra reference points.
JGM Data Structure and Algorithmic
Linked Lists vs Arrays vs Dynamic Arrays
Figure: 0‘
JGM Data Structure and Algorithmic
Linked Lists ADT
Main operations
Insert : inserts an element into the list
Delete: removes and returns the specified position element
from the list
Auxiliary operations
Delete List: removes all elements of the list (dispose of the
list).
Count: returns the number of elements in the list.
Find nth
node from the end of the list.
JGM Data Structure and Algorithmic
Singly Linked Lists (SLL)
This list consists of a number of nodes in which each node has a
next pointer to the following element. The link of the last node in
the list is NULL, which indicates the end of the list.
JGM Data Structure and Algorithmic
Class Diagram
JGM Data Structure and Algorithmic
Traversing the SLL
JGM Data Structure and Algorithmic
Code : Traversing the SLL
1
2 def listLength(self ):
3
4 current = self.head
5 count = 0
6
7 while current != None:
8 count = count + 1
9 current = current.getNext ()
10
11 return count
JGM Data Structure and Algorithmic
Inserting a Node in SLL at the Beginning
Figure: 05
Figure: 06
JGM Data Structure and Algorithmic
Code : Inserting a Node in SLL at the Beginning
1 import Node
2
3 # method for inserting a new node at
4 # the beginning of the Linked List (at the head)
5 def insertAtBeginning (self ,data ):
6
7 newNode = Node ()
8 newNode.setData(data)
9
10 if self.length == 0:
11 self.head = newNode
12 else:
13 newNode.setNext(self.head)
14 self.head = newNode
15
16 self.length += 1
JGM Data Structure and Algorithmic
Inserting a Node in SLL at the Ending
Figure: 07
Figure: 08
JGM Data Structure and Algorithmic
Code : Inserting a Node in SLL at the Ending
1 import Node
2
3 # Method for inserting a new node at
4 # the end of a Linked List
5 def insertAtEnd(self ,data ):
6
7 newNode = Node ()
8 newNode.setData(data)
9
10 current = self.head
11
12 while current.getNext () != None:
13 current = current.getNext ()
14
15 current.setNext(newNode)
16 self.length += 1
JGM Data Structure and Algorithmic
Inserting a Node in SLL at the Middle
Figure: 09
Figure: 10
JGM Data Structure and Algorithmic
Code : Inserting a Node in SLL at the Middle
1 # Method for inserting a new node at any position in
2 def insertAtPos(self ,pos ,data ):
3 if pos > self.length or pos < 0:
4 return None
5 else:
6 if pos == 0:
7 self.insertAtBeg(data)
8 else:
9 if pos == self.length:
10 self.insertAtEnd(data)
11 else:
12 newNode = Node ()
13 newNode.setData(data)
14 count = 0
15 current = self.head
16 while count < pos -1:
17 count += 1
18 current = currcnt.getNext ()
19
20 newNode.setNext(current.getNext ())
JGM Data Structure and Algorithmic
Deleting the First Node in SLL
Figure: 11
Figure: 12
JGM Data Structure and Algorithmic
Deleting the Last Node in SLL
Figure: 13
Figure: 14
Figure: 15
JGM Data Structure and Algorithmic
Deleting an Intermediate Node in SLL
Figure: 16
Figure: 17
JGM Data Structure and Algorithmic
Code : Deleting SLL
1 def clear(self) :
2 self.head = None
JGM Data Structure and Algorithmic
Conclusion
In this lesson we learned how to traverse, append, prepend,
insert and delete nodes from a linked list.
Backtracking is a recursive algorithm for finding all (or some)
solutions to some computational problems.
JGM Data Structure and Algorithmic
Bibliography
Narasimha Karumanchi. Data Structure and Algorithmic -
Thinking with Python, 2019.
John Wiley & Sons. Data Structure And Algorithms In Java
internet, 2010.
JGM Data Structure and Algorithmic

More Related Content

Similar to Week-03.pdf

Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfkarymadelaneyrenne19
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureRai University
 
Data structure
Data structureData structure
Data structureMohd Arif
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptxsarala9
 
Lecture#1(Algorithmic Notations).ppt
Lecture#1(Algorithmic Notations).pptLecture#1(Algorithmic Notations).ppt
Lecture#1(Algorithmic Notations).pptMuhammadTalhaAwan1
 
Data structures Lecture no. 2
Data structures Lecture no. 2Data structures Lecture no. 2
Data structures Lecture no. 2AzharIqbal710687
 
SamYang's Project Poster
SamYang's Project PosterSamYang's Project Poster
SamYang's Project PosterSam Yang
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm AnalysisMary Margarat
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptxSaralaT3
 
Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.pptGokiladeepa
 
Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.pptsaivasu4
 

Similar to Week-03.pdf (20)

Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
 
Linked List
Linked ListLinked List
Linked List
 
Data structure
 Data structure Data structure
Data structure
 
Data structure
Data structureData structure
Data structure
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
 
Lecture#1(Algorithmic Notations).ppt
Lecture#1(Algorithmic Notations).pptLecture#1(Algorithmic Notations).ppt
Lecture#1(Algorithmic Notations).ppt
 
Ln liers
Ln liersLn liers
Ln liers
 
test1
test1test1
test1
 
Data structures Lecture no. 2
Data structures Lecture no. 2Data structures Lecture no. 2
Data structures Lecture no. 2
 
SamYang's Project Poster
SamYang's Project PosterSamYang's Project Poster
SamYang's Project Poster
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
Data Structure Basics
Data Structure BasicsData Structure Basics
Data Structure Basics
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
 
List 2
List 2List 2
List 2
 
List moderate
List   moderateList   moderate
List moderate
 
Linked List.ppt
Linked List.pptLinked List.ppt
Linked List.ppt
 
Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.ppt
 
Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.ppt
 
Wk11-linkedlist.ppt
Wk11-linkedlist.pptWk11-linkedlist.ppt
Wk11-linkedlist.ppt
 

More from AaronContreras28

More from AaronContreras28 (9)

03 Arduino - Analogo (2).pdf
03 Arduino - Analogo (2).pdf03 Arduino - Analogo (2).pdf
03 Arduino - Analogo (2).pdf
 
2022 Carta LSP.pdf
2022 Carta LSP.pdf2022 Carta LSP.pdf
2022 Carta LSP.pdf
 
MODULO1.3.pptx
MODULO1.3.pptxMODULO1.3.pptx
MODULO1.3.pptx
 
MODULO5-1.pptx
MODULO5-1.pptxMODULO5-1.pptx
MODULO5-1.pptx
 
MODULO6.pptx
MODULO6.pptxMODULO6.pptx
MODULO6.pptx
 
PPT-S08-MMIRANDA-2022-01.pptx
PPT-S08-MMIRANDA-2022-01.pptxPPT-S08-MMIRANDA-2022-01.pptx
PPT-S08-MMIRANDA-2022-01.pptx
 
SESIÓN 16 - Pruebas de Aceptacion (1).pptx
SESIÓN 16 - Pruebas de Aceptacion (1).pptxSESIÓN 16 - Pruebas de Aceptacion (1).pptx
SESIÓN 16 - Pruebas de Aceptacion (1).pptx
 
PresentacionFinal.pptx
PresentacionFinal.pptxPresentacionFinal.pptx
PresentacionFinal.pptx
 
ULTIMOS 3 CUPOS.pdf
ULTIMOS 3 CUPOS.pdfULTIMOS 3 CUPOS.pdf
ULTIMOS 3 CUPOS.pdf
 

Recently uploaded

SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxCHAIRMAN M
 
electrical installation and maintenance.
electrical installation and maintenance.electrical installation and maintenance.
electrical installation and maintenance.benjamincojr
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxKarpagam Institute of Teechnology
 
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfMadan Karki
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxMustafa Ahmed
 
Software Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdfSoftware Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdfssuser5c9d4b1
 
Basics of Relay for Engineering Students
Basics of Relay for Engineering StudentsBasics of Relay for Engineering Students
Basics of Relay for Engineering Studentskannan348865
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisDr.Costas Sachpazis
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...archanaece3
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptamrabdallah9
 
Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)NareenAsad
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailingAshishSingh1301
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...josephjonse
 
Piping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdfPiping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdfAshrafRagab14
 
Autodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxAutodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxMustafa Ahmed
 
Raashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashidFaiyazSheikh
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfEr.Sonali Nasikkar
 
Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...IJECEIAES
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfJNTUA
 
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfInvolute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfJNTUA
 

Recently uploaded (20)

SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
 
electrical installation and maintenance.
electrical installation and maintenance.electrical installation and maintenance.
electrical installation and maintenance.
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptx
 
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
Software Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdfSoftware Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdf
 
Basics of Relay for Engineering Students
Basics of Relay for Engineering StudentsBasics of Relay for Engineering Students
Basics of Relay for Engineering Students
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.ppt
 
Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)Operating System chapter 9 (Virtual Memory)
Operating System chapter 9 (Virtual Memory)
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailing
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
Piping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdfPiping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdf
 
Autodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxAutodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptx
 
Raashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashid final report on Embedded Systems
Raashid final report on Embedded Systems
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
 
Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdf
 
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfInvolute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
 

Week-03.pdf

  • 1. Linked Lists Sesion 03 Ing. Gómez Marı́n, Jaime1 1Desarrollo y Diseño de Software Departamento de TdG September 2020 JGM Data Structure and Algorithmic
  • 2. Table of Contents Introduction Arrays Overview Advantages and Disadvantages of Arrays Dynamic Arrays Overview Linked Lists Advantages and Disadvantages of Linked Lists Linked Lists vs Arrays vs Dynamic Arrays Singly Linked Lists Conclusion Bibliography JGM Data Structure and Algorithmic
  • 3. Introduction In this session, we will learn about Linked Lists, which is a very common dynamic data structure that is used to create other data structures likes tree, graphs, hashing, etc. JGM Data Structure and Algorithmic
  • 4. Arrays Overview The arrays need one memory block for hold the elements of the array. The array elements can be accessed in constant time by using the index of the particular element. To access an array element, the address of an element is computed as an offset from the base address of the array JGM Data Structure and Algorithmic
  • 5. Advantages and Disadvantages of Arrays Advantages Simple and easy to use Faster access to the elements Disadvantages Fixed size One block allocation Complex position-based insertion. JGM Data Structure and Algorithmic
  • 6. Dynamic Arrays Overview Dynamic array is a random access, variable-size list data structure that allows elements to be added or removed. Start the dynamic array with a fixed size. As soon as that array becomes full, create the new array double the size of the original array. In other case, reduce the array size to half if the elements in the array are less than half. JGM Data Structure and Algorithmic
  • 7. Linked Lists It is a data structure user for storing collections of data, its characteristics are: Successive elements are connected by pointers. The last element points to NULL. Can grow or shrink in size during execution of a program Does not waste memory space, but takes some extra memory for pointers. Figure: 01 JGM Data Structure and Algorithmic
  • 8. Advantages and Disadvantages of Linked Lists Advantages The dynamic allocation of storage is a great advantage. We can start with space for just one allocated elements and add on new elements. Disadvantages Linked Lists take O(n) for access to an element in the list in the worst case. Do not have spacial locality in memory and lost the benefits from modern CPU caching methods. There are overhead with storing and retrieving data. Linked lists waste memory in terms of extra reference points. JGM Data Structure and Algorithmic
  • 9. Linked Lists vs Arrays vs Dynamic Arrays Figure: 0‘ JGM Data Structure and Algorithmic
  • 10. Linked Lists ADT Main operations Insert : inserts an element into the list Delete: removes and returns the specified position element from the list Auxiliary operations Delete List: removes all elements of the list (dispose of the list). Count: returns the number of elements in the list. Find nth node from the end of the list. JGM Data Structure and Algorithmic
  • 11. Singly Linked Lists (SLL) This list consists of a number of nodes in which each node has a next pointer to the following element. The link of the last node in the list is NULL, which indicates the end of the list. JGM Data Structure and Algorithmic
  • 12. Class Diagram JGM Data Structure and Algorithmic
  • 13. Traversing the SLL JGM Data Structure and Algorithmic
  • 14. Code : Traversing the SLL 1 2 def listLength(self ): 3 4 current = self.head 5 count = 0 6 7 while current != None: 8 count = count + 1 9 current = current.getNext () 10 11 return count JGM Data Structure and Algorithmic
  • 15. Inserting a Node in SLL at the Beginning Figure: 05 Figure: 06 JGM Data Structure and Algorithmic
  • 16. Code : Inserting a Node in SLL at the Beginning 1 import Node 2 3 # method for inserting a new node at 4 # the beginning of the Linked List (at the head) 5 def insertAtBeginning (self ,data ): 6 7 newNode = Node () 8 newNode.setData(data) 9 10 if self.length == 0: 11 self.head = newNode 12 else: 13 newNode.setNext(self.head) 14 self.head = newNode 15 16 self.length += 1 JGM Data Structure and Algorithmic
  • 17. Inserting a Node in SLL at the Ending Figure: 07 Figure: 08 JGM Data Structure and Algorithmic
  • 18. Code : Inserting a Node in SLL at the Ending 1 import Node 2 3 # Method for inserting a new node at 4 # the end of a Linked List 5 def insertAtEnd(self ,data ): 6 7 newNode = Node () 8 newNode.setData(data) 9 10 current = self.head 11 12 while current.getNext () != None: 13 current = current.getNext () 14 15 current.setNext(newNode) 16 self.length += 1 JGM Data Structure and Algorithmic
  • 19. Inserting a Node in SLL at the Middle Figure: 09 Figure: 10 JGM Data Structure and Algorithmic
  • 20. Code : Inserting a Node in SLL at the Middle 1 # Method for inserting a new node at any position in 2 def insertAtPos(self ,pos ,data ): 3 if pos > self.length or pos < 0: 4 return None 5 else: 6 if pos == 0: 7 self.insertAtBeg(data) 8 else: 9 if pos == self.length: 10 self.insertAtEnd(data) 11 else: 12 newNode = Node () 13 newNode.setData(data) 14 count = 0 15 current = self.head 16 while count < pos -1: 17 count += 1 18 current = currcnt.getNext () 19 20 newNode.setNext(current.getNext ()) JGM Data Structure and Algorithmic
  • 21. Deleting the First Node in SLL Figure: 11 Figure: 12 JGM Data Structure and Algorithmic
  • 22. Deleting the Last Node in SLL Figure: 13 Figure: 14 Figure: 15 JGM Data Structure and Algorithmic
  • 23. Deleting an Intermediate Node in SLL Figure: 16 Figure: 17 JGM Data Structure and Algorithmic
  • 24. Code : Deleting SLL 1 def clear(self) : 2 self.head = None JGM Data Structure and Algorithmic
  • 25. Conclusion In this lesson we learned how to traverse, append, prepend, insert and delete nodes from a linked list. Backtracking is a recursive algorithm for finding all (or some) solutions to some computational problems. JGM Data Structure and Algorithmic
  • 26. Bibliography Narasimha Karumanchi. Data Structure and Algorithmic - Thinking with Python, 2019. John Wiley & Sons. Data Structure And Algorithms In Java internet, 2010. JGM Data Structure and Algorithmic