SlideShare a Scribd company logo
Lecture No.02
Data Structures
Linked List
 Create a structure called a Node.
object next
 The object field will hold the actual list element.
 The next field in the structure will hold the
starting location of the next node.
 Chain the nodes together to form a linked list.
2
Linked List
 Picture of our list (2, 6, 7, 8, 1) stored as a linked list:
2 6 8 7 1
head
current
size=5
3
Linked List
Note some features of the list:
 Need a head to point to the first node of the list. Otherwise we
won’t know where the start of the list is.
4
Linked List
Note some features of the list:
 Need a head to point to the first node of the list. Otherwise we
won’t know where the start of the list is.
 The current here is a pointer, not an index.
5
Linked List
Note some features of the list:
 Need a head to point to the first node of the list. Otherwise we
won’t know where the start of the list is.
 The current here is a pointer, not an index.
 The next field in the last node points to nothing. We will place the
memory address NULL which is guaranteed to be inaccessible.
6
Linked List
 Actual picture in memory:
1051
1052
1055
1059
1060
1061
1062
1063
1064
1056
1057
1058
1053
1054 2
6
8
7
1
1051
1063
1057
1060
0
head 1054
1063current
2 6 8 7 1
head
current
1065
7
Building a Linked List
headNode size=0List list;
8
Building a Linked List
headNode
2headNode
currentNode
size=1
lastcurrentNode
size=0List list;
list.add(2);
9
Building a Linked List
headNode
2headNode
currentNode
size=1
lastcurrentNode
2 6headNode
currentNode
size=2
lastcurrentNode
size=0List list;
list.add(2);
list.add(6);
10
Building a Linked List
List.add(8); list.add(7); list.add(1);
2 6 7 1headNode
currentNode
size=5
lastcurrentNode
8
11
Doubly-linked List
 Moving forward in a singly-linked list is easy;
moving backwards is not so easy.
 To move back one node, we have to start at the
head of the singly-linked list and move forward
until the node before the current.
 To avoid this we can use two pointers in a
node: one to point to next node and another to
point to the previous node:
element nextprev
12
Doubly-linked List
 Need to be more careful when adding or
removing a node.
 Consider add: the order in which pointers are
reorganized is important:
size=52 6 8 7 1head
current
13
Circularly-linked lists
 The next field in the last node in a singly-linked
list is set to NULL.
 Moving along a singly-linked list has to be done
in a watchful manner.
 Doubly-linked lists have two NULL pointers:
prev in the first node and next in the last node.
 A way around this potential hazard is to link the
last node with the first node in the list to create
a circularly-linked list.
14
Cicularly Linked List
 Two views of a circularly linked list:
2 6 8 7 1head
current
size=5
2
8
7
1
head
current
size=5
6
15
Josephus Problem
 A case where circularly linked list comes in
handy is the solution of the Josephus Problem.
 Consider there are 10 persons. They would like
to choose a leader.
 The way they decide is that all 10 sit in a circle.
 They start a count with person 1 and go in
clockwise direction and skip 3. Person 4
reached is eliminated.
 The count starts with the fifth and the next
person to go is the fourth in count.
 Eventually, a single person remains.
16
Josephus Problem
 N=10, M=3
9
8
7
6
5
4
3
2
1
10
17
Josephus Problem
 N=10, M=3
9
8
7
6
5
4
3
2
1
10
eliminated
18
Josephus Problem
 N=10, M=3
9
8
7
6
5
4
3
2
1
10
eliminated
19
Josephus Problem
 N=10, M=3
9
8
7
6
5
4
3
2
1
10
eliminated
20
Josephus Problem
 N=10, M=3
9
8
7
6
5
4
3
2
1
10
eliminated
21
Josephus Problem
 N=10, M=3
9
8
7
6
5
4
3
2
1
10
eliminated
22
Josephus Problem
 N=10, M=3
9
8
7
6
5
4
3
2
1
10
eliminated
23
Josephus Problem
 N=10, M=3
9
8
7
6
5
4
3
2
1
10
eliminated
24
Josephus Problem
 N=10, M=3
9
8
7
6
5
4
3
2
1
10
eliminated
25
Josephus Problem
 N=10, M=3
9
8
7
6
5
4
3
2
1
10
eliminated
26

More Related Content

What's hot (18)

Link_List.pptx
Link_List.pptxLink_List.pptx
Link_List.pptx
 
Lecture 2b lists
Lecture 2b listsLecture 2b lists
Lecture 2b lists
 
ISDD Database Structure N5
ISDD Database Structure N5ISDD Database Structure N5
ISDD Database Structure N5
 
rrrrrr
rrrrrrrrrrrr
rrrrrr
 
Sql server ___________session_17(indexes)
Sql server  ___________session_17(indexes)Sql server  ___________session_17(indexes)
Sql server ___________session_17(indexes)
 
DATABASE CONCEPTS AND PRACTICAL EXAMPLES
DATABASE CONCEPTS AND PRACTICAL EXAMPLESDATABASE CONCEPTS AND PRACTICAL EXAMPLES
DATABASE CONCEPTS AND PRACTICAL EXAMPLES
 
Using microsoft excel
Using microsoft excelUsing microsoft excel
Using microsoft excel
 
Create table relationships
Create table relationshipsCreate table relationships
Create table relationships
 
Relationship Database
Relationship DatabaseRelationship Database
Relationship Database
 
Call Referencing - R.D.Sivakumar
Call Referencing - R.D.SivakumarCall Referencing - R.D.Sivakumar
Call Referencing - R.D.Sivakumar
 
Normalization
NormalizationNormalization
Normalization
 
Linked list
Linked listLinked list
Linked list
 
Link list
Link listLink list
Link list
 
Application of Data structure
Application of Data structureApplication of Data structure
Application of Data structure
 
Trees
TreesTrees
Trees
 
Advance Sqlite3
Advance Sqlite3Advance Sqlite3
Advance Sqlite3
 
Database Management Systems 4 - Normalization
Database Management Systems 4 - NormalizationDatabase Management Systems 4 - Normalization
Database Management Systems 4 - Normalization
 
Db lec 06_new
Db lec 06_newDb lec 06_new
Db lec 06_new
 

Similar to Data structures

In this lab we will write code for working with a Linked List. Node .pdf
In this lab we will write code for working with a Linked List.  Node .pdfIn this lab we will write code for working with a Linked List.  Node .pdf
In this lab we will write code for working with a Linked List. Node .pdf
fms12345
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
FaheemMahmood2
 

Similar to Data structures (20)

In this lab we will write code for working with a Linked List. Node .pdf
In this lab we will write code for working with a Linked List.  Node .pdfIn this lab we will write code for working with a Linked List.  Node .pdf
In this lab we will write code for working with a Linked List. Node .pdf
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Data Structure lec#2
Data Structure lec#2Data Structure lec#2
Data Structure lec#2
 
CH4.pptx
CH4.pptxCH4.pptx
CH4.pptx
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
 
linked_lists
linked_listslinked_lists
linked_lists
 
Linked list.docx
Linked list.docxLinked list.docx
Linked list.docx
 
linked list using c
linked list using clinked list using c
linked list using c
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
 
Link list assi
Link list assiLink list assi
Link list assi
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 
Chapter 3 Linkedlist Data Structure .pdf
Chapter 3 Linkedlist Data Structure .pdfChapter 3 Linkedlist Data Structure .pdf
Chapter 3 Linkedlist Data Structure .pdf
 
Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5
 
csc211_lecture_21.pptx
csc211_lecture_21.pptxcsc211_lecture_21.pptx
csc211_lecture_21.pptx
 
data structures lists operation of lists
data structures lists operation of listsdata structures lists operation of lists
data structures lists operation of lists
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssssDSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
 
Linked list
Linked listLinked list
Linked list
 
linked list
linked listlinked list
linked list
 

More from Rokonuzzaman Rony (20)

Course outline for c programming
Course outline for c  programming Course outline for c  programming
Course outline for c programming
 
Pointer
PointerPointer
Pointer
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
 
Humanitarian task and its importance
Humanitarian task and its importanceHumanitarian task and its importance
Humanitarian task and its importance
 
Structure
StructureStructure
Structure
 
Pointers
 Pointers Pointers
Pointers
 
Loops
LoopsLoops
Loops
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Array
ArrayArray
Array
 
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
 
C Programming language
C Programming languageC Programming language
C Programming language
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Numerical Method 2
Numerical Method 2Numerical Method 2
Numerical Method 2
 
Numerical Method
Numerical Method Numerical Method
Numerical Method
 
Data structures
Data structures Data structures
Data structures
 
Data structures
Data structures Data structures
Data structures
 

Recently uploaded

Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
YibeltalNibretu
 

Recently uploaded (20)

Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 

Data structures

  • 2. Linked List  Create a structure called a Node. object next  The object field will hold the actual list element.  The next field in the structure will hold the starting location of the next node.  Chain the nodes together to form a linked list. 2
  • 3. Linked List  Picture of our list (2, 6, 7, 8, 1) stored as a linked list: 2 6 8 7 1 head current size=5 3
  • 4. Linked List Note some features of the list:  Need a head to point to the first node of the list. Otherwise we won’t know where the start of the list is. 4
  • 5. Linked List Note some features of the list:  Need a head to point to the first node of the list. Otherwise we won’t know where the start of the list is.  The current here is a pointer, not an index. 5
  • 6. Linked List Note some features of the list:  Need a head to point to the first node of the list. Otherwise we won’t know where the start of the list is.  The current here is a pointer, not an index.  The next field in the last node points to nothing. We will place the memory address NULL which is guaranteed to be inaccessible. 6
  • 7. Linked List  Actual picture in memory: 1051 1052 1055 1059 1060 1061 1062 1063 1064 1056 1057 1058 1053 1054 2 6 8 7 1 1051 1063 1057 1060 0 head 1054 1063current 2 6 8 7 1 head current 1065 7
  • 8. Building a Linked List headNode size=0List list; 8
  • 9. Building a Linked List headNode 2headNode currentNode size=1 lastcurrentNode size=0List list; list.add(2); 9
  • 10. Building a Linked List headNode 2headNode currentNode size=1 lastcurrentNode 2 6headNode currentNode size=2 lastcurrentNode size=0List list; list.add(2); list.add(6); 10
  • 11. Building a Linked List List.add(8); list.add(7); list.add(1); 2 6 7 1headNode currentNode size=5 lastcurrentNode 8 11
  • 12. Doubly-linked List  Moving forward in a singly-linked list is easy; moving backwards is not so easy.  To move back one node, we have to start at the head of the singly-linked list and move forward until the node before the current.  To avoid this we can use two pointers in a node: one to point to next node and another to point to the previous node: element nextprev 12
  • 13. Doubly-linked List  Need to be more careful when adding or removing a node.  Consider add: the order in which pointers are reorganized is important: size=52 6 8 7 1head current 13
  • 14. Circularly-linked lists  The next field in the last node in a singly-linked list is set to NULL.  Moving along a singly-linked list has to be done in a watchful manner.  Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node.  A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list. 14
  • 15. Cicularly Linked List  Two views of a circularly linked list: 2 6 8 7 1head current size=5 2 8 7 1 head current size=5 6 15
  • 16. Josephus Problem  A case where circularly linked list comes in handy is the solution of the Josephus Problem.  Consider there are 10 persons. They would like to choose a leader.  The way they decide is that all 10 sit in a circle.  They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated.  The count starts with the fifth and the next person to go is the fourth in count.  Eventually, a single person remains. 16
  • 17. Josephus Problem  N=10, M=3 9 8 7 6 5 4 3 2 1 10 17
  • 18. Josephus Problem  N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated 18
  • 19. Josephus Problem  N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated 19
  • 20. Josephus Problem  N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated 20
  • 21. Josephus Problem  N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated 21
  • 22. Josephus Problem  N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated 22
  • 23. Josephus Problem  N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated 23
  • 24. Josephus Problem  N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated 24
  • 25. Josephus Problem  N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated 25
  • 26. Josephus Problem  N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated 26