SlideShare uma empresa Scribd logo
1 de 26
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

Mais conteúdo relacionado

Mais procurados

Mais procurados (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
 

Semelhante a Data structures

Semelhante a 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
 
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
 
Linked list
Linked listLinked list
Linked list
 
linked list
linked listlinked list
linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked lists
Linked listsLinked lists
Linked lists
 

Mais de 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
 

Último

Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
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
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
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
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
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
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 

Último (20)

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
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
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
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
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
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
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
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
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)
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 

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