SlideShare uma empresa Scribd logo
1 de 24
Lecture No.02
Data Structures
Zaigham Abbas
University of Gujrat (Pakistan)
Implementing Lists
 We have designed the interface for the List; we
now must consider how to implement that
interface.
Implementing Lists
 We have designed the interface for the List; we
now must consider how to implement that
interface.
 Implementing Lists using an array: for example,
the list of integers (2, 6, 8, 7, 1) could be
represented as:
A 6 8 7 1
1 2 3 4 5
2
current
3
size
5
List Implementation
 add(9); current position is 3. The new list would thus
be: (2, 6, 8, 9, 7, 1)
 We will need to shift everything to the right of 8 one
place to the right to make place for the new element ‘9’.
current
3
size
5
step 1: A 6 8 7 1
1 2 3 4 5
2
6
current
4
size
6
step 2: A 6 8 7 1
1 2 3 4 5
2
6
9
notice: current points
to new element
Implementing Lists
 next():
current
4
size
6
A 6 8 7 1
1 2 3 4 5
2
6
9
5
Implementing Lists
 There are special cases for positioning the
current pointer:
a. past the last array cell
b. before the first cell
Implementing Lists
 There are special cases for positioning the
current pointer:
a. past the last array cell
b. before the first cell
 We will have to worry about these when we
write the actual code.
Implementing Lists
 remove(): removes the element at the current
index
current
5
size
6
A 6 8 1
1 2 3 4 5
2
6
9
5
Step 1:
current
5
size
5
A 6 8 1
1 2 3 4 5
2 9Step 2:
Implementing Lists
 remove(): removes the element at the current
index
 We fill the blank spot left by the removal of 7 by
shifting the values to the right of position 5 over to the left one space.
current
5
size
5
A 6 8 1
1 2 3 4 5
2 9Step 2:
current
5
size
6
A 6 8 1
1 2 3 4 5
2
6
9
5
Step 1:
Implementing Lists
find(X): traverse the array until X is located.
int find(int X)
{
int j;
for(j=1; j < size+1; j++ )
if( A[j] == X ) break;
if( j < size+1 ) { // found X
current = j; // current points to where X found
return 1; // 1 for true
}
return 0; // 0 (false) indicates not found
}
Implementing Lists
 Other operations:
get()  return A[current];
update(X)  A[current] = X;
length()  return size;
back()  current--;
start()  current = 1;
end()  current = size;
Analysis of Array Lists
 add
 we have to move every element to the right of
current to make space for the new element.
 Worst-case is when we insert at the beginning; we
have to move every element right one place.
 Average-case: on average we may have to move
half of the elements
Analysis of Array Lists
 remove
 Worst-case: remove at the beginning, must shift all
remaining elements to the left.
 Average-case: expect to move half of the elements.
 find
 Worst-case: may have to search the entire array
 Average-case: search at most half the array.
 Other operations are one-step.
List Using Linked Memory
 Various cells of memory are not allocated
consecutively in memory.
List Using Linked Memory
 Various cells of memory are not allocated
consecutively in memory.
 Not enough to store the elements of the list.
List Using Linked Memory
 Various cells of memory are not allocated
consecutively in memory.
 Not enough to store the elements of the list.
 With arrays, the second element was right next
to the first element.
List Using Linked Memory
 Various cells of memory are not allocated
consecutively in memory.
 Not enough to store the elements of the list.
 With arrays, the second element was right next
to the first element.
 Now the first element must explicitly tell us
where to look for the second element.
List Using Linked Memory
 Various cells of memory are not allocated
consecutively in memory.
 Not enough to store the elements of the list.
 With arrays, the second element was right next
to the first element.
 Now the first element must explicitly tell us
where to look for the second element.
 Do this by holding the memory address of the
second element
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.
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
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.
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.
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.
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

Mais conteúdo relacionado

Mais procurados

Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queuesurya pandian
 
List Data Structure
List Data StructureList Data Structure
List Data StructureZidny Nafan
 
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 KristinaBorooah
 
Linked stacks and queues
Linked stacks and queuesLinked stacks and queues
Linked stacks and queuesRamzi Alqrainy
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked listFahd Allebdi
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arorakulachihansraj
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]Muhammad Hammad Waseem
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure shameen khan
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)swajahatr
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structuresDurgaDeviCbit
 

Mais procurados (17)

Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queue
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
 
poornima.coseq
poornima.coseqpoornima.coseq
poornima.coseq
 
List interface
List interfaceList interface
List interface
 
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
 
Linked stacks and queues
Linked stacks and queuesLinked stacks and queues
Linked stacks and queues
 
Doubly Link List
Doubly Link ListDoubly Link List
Doubly Link List
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Linked lists
Linked listsLinked lists
Linked lists
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Linked List
Linked ListLinked List
Linked List
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
Data Structure (Circular Linked List)
Data Structure (Circular Linked List)Data Structure (Circular Linked List)
Data Structure (Circular Linked List)
 
List and iterator
List and iteratorList and iterator
List and iterator
 

Destaque

Destaque (6)

Data Structure Lec #1
Data Structure Lec #1Data Structure Lec #1
Data Structure Lec #1
 
DLD Practical Lab Work
DLD Practical Lab WorkDLD Practical Lab Work
DLD Practical Lab Work
 
Data structure and algorithm All in One
Data structure and algorithm All in OneData structure and algorithm All in One
Data structure and algorithm All in One
 
Data base management system
Data base management systemData base management system
Data base management system
 
Data Base Management System
Data Base Management SystemData Base Management System
Data Base Management System
 
Database management system
Database management systemDatabase management system
Database management system
 

Semelhante a Data Structure lec#2

introduction_dst.pptx
introduction_dst.pptxintroduction_dst.pptx
introduction_dst.pptxHammadTariq51
 
computer notes - Stack
computer notes - Stackcomputer notes - Stack
computer notes - Stackecomputernotes
 
Data Structure Searching.pptx
Data Structure Searching.pptxData Structure Searching.pptx
Data Structure Searching.pptxSourabhTaneja4
 
computer notes - Linked list
computer notes - Linked listcomputer notes - Linked list
computer notes - Linked listecomputernotes
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptxssuserd2f031
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.pptSeethaDinesh
 
unit-ids17-180709051413-1.pdf
unit-ids17-180709051413-1.pdfunit-ids17-180709051413-1.pdf
unit-ids17-180709051413-1.pdfKarthiKeyan326587
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)Elavarasi K
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)Durga Devi
 
ds 2Arrays.ppt
ds 2Arrays.pptds 2Arrays.ppt
ds 2Arrays.pptAlliVinay1
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxjane3dyson92312
 
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxfestockton
 
Arrays and linked lists
Arrays and linked listsArrays and linked lists
Arrays and linked listsAfriyieCharles
 

Semelhante a Data Structure lec#2 (20)

introduction_dst.pptx
introduction_dst.pptxintroduction_dst.pptx
introduction_dst.pptx
 
Data structures
Data structuresData structures
Data structures
 
computer notes - Stack
computer notes - Stackcomputer notes - Stack
computer notes - Stack
 
day 13.pptx
day 13.pptxday 13.pptx
day 13.pptx
 
Data Structure Searching.pptx
Data Structure Searching.pptxData Structure Searching.pptx
Data Structure Searching.pptx
 
computer notes - Linked list
computer notes - Linked listcomputer notes - Linked list
computer notes - Linked list
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
 
Ds notes
Ds notesDs notes
Ds notes
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 
unit-ids17-180709051413-1.pdf
unit-ids17-180709051413-1.pdfunit-ids17-180709051413-1.pdf
unit-ids17-180709051413-1.pdf
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
ds 2Arrays.ppt
ds 2Arrays.pptds 2Arrays.ppt
ds 2Arrays.ppt
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
 
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
 
Arrays and linked lists
Arrays and linked listsArrays and linked lists
Arrays and linked lists
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 

Mais de University of Gujrat, Pakistan

Constitutional development of pakistan since 1947 to thereayf
Constitutional development of pakistan since 1947 to thereayfConstitutional development of pakistan since 1947 to thereayf
Constitutional development of pakistan since 1947 to thereayfUniversity of Gujrat, Pakistan
 
Multiplication & division instructions microprocessor 8086
Multiplication & division instructions microprocessor 8086Multiplication & division instructions microprocessor 8086
Multiplication & division instructions microprocessor 8086University of Gujrat, Pakistan
 
Passive Thermal management for avionics in high temperature environment
Passive Thermal management for avionics in high temperature environmentPassive Thermal management for avionics in high temperature environment
Passive Thermal management for avionics in high temperature environmentUniversity of Gujrat, Pakistan
 
Future Integrated Systems Concept for Preventing Aircraft Loss-of-Control (L...
Future Integrated Systems Concept for Preventing Aircraft Loss-of-Control (L...Future Integrated Systems Concept for Preventing Aircraft Loss-of-Control (L...
Future Integrated Systems Concept for Preventing Aircraft Loss-of-Control (L...University of Gujrat, Pakistan
 
Hardware implementation of cots avionics system on unmanned
Hardware implementation of cots avionics system on unmannedHardware implementation of cots avionics system on unmanned
Hardware implementation of cots avionics system on unmannedUniversity of Gujrat, Pakistan
 
A wireless sensors network in Aircraft Control Systems
A wireless sensors network in Aircraft Control SystemsA wireless sensors network in Aircraft Control Systems
A wireless sensors network in Aircraft Control SystemsUniversity of Gujrat, Pakistan
 

Mais de University of Gujrat, Pakistan (20)

Natural disasters of pakistan
Natural disasters of pakistanNatural disasters of pakistan
Natural disasters of pakistan
 
Dual combustion cycle
Dual combustion cycleDual combustion cycle
Dual combustion cycle
 
Diesel cycle
Diesel cycleDiesel cycle
Diesel cycle
 
Carnot cycle
Carnot cycleCarnot cycle
Carnot cycle
 
Brayton cycle
Brayton cycleBrayton cycle
Brayton cycle
 
Constitutional development of pakistan since 1947 to thereayf
Constitutional development of pakistan since 1947 to thereayfConstitutional development of pakistan since 1947 to thereayf
Constitutional development of pakistan since 1947 to thereayf
 
Essay writing
Essay writingEssay writing
Essay writing
 
Letter writing (Communication Skills)
Letter writing (Communication Skills)Letter writing (Communication Skills)
Letter writing (Communication Skills)
 
Architecture of high end processors
Architecture of high end processorsArchitecture of high end processors
Architecture of high end processors
 
Architecture of pentium family
Architecture of pentium familyArchitecture of pentium family
Architecture of pentium family
 
Bus interface 8086
Bus interface 8086Bus interface 8086
Bus interface 8086
 
Protected mode memory addressing 8086
Protected mode memory addressing 8086Protected mode memory addressing 8086
Protected mode memory addressing 8086
 
Internal microprocessor architecture
Internal microprocessor architectureInternal microprocessor architecture
Internal microprocessor architecture
 
Multiplication & division instructions microprocessor 8086
Multiplication & division instructions microprocessor 8086Multiplication & division instructions microprocessor 8086
Multiplication & division instructions microprocessor 8086
 
Wireless sensors network in Avionic control system
Wireless sensors network in Avionic control systemWireless sensors network in Avionic control system
Wireless sensors network in Avionic control system
 
An autonomous uav with an optical flow sensor
An autonomous uav with an optical flow sensorAn autonomous uav with an optical flow sensor
An autonomous uav with an optical flow sensor
 
Passive Thermal management for avionics in high temperature environment
Passive Thermal management for avionics in high temperature environmentPassive Thermal management for avionics in high temperature environment
Passive Thermal management for avionics in high temperature environment
 
Future Integrated Systems Concept for Preventing Aircraft Loss-of-Control (L...
Future Integrated Systems Concept for Preventing Aircraft Loss-of-Control (L...Future Integrated Systems Concept for Preventing Aircraft Loss-of-Control (L...
Future Integrated Systems Concept for Preventing Aircraft Loss-of-Control (L...
 
Hardware implementation of cots avionics system on unmanned
Hardware implementation of cots avionics system on unmannedHardware implementation of cots avionics system on unmanned
Hardware implementation of cots avionics system on unmanned
 
A wireless sensors network in Aircraft Control Systems
A wireless sensors network in Aircraft Control SystemsA wireless sensors network in Aircraft Control Systems
A wireless sensors network in Aircraft Control Systems
 

Último

AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
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
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
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
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
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
 
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
 
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
 
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
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxElton John Embodo
 

Último (20)

AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
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
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
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
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.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
 
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
 
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
 
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
 
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
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
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
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docx
 

Data Structure lec#2

  • 1. Lecture No.02 Data Structures Zaigham Abbas University of Gujrat (Pakistan)
  • 2. Implementing Lists  We have designed the interface for the List; we now must consider how to implement that interface.
  • 3. Implementing Lists  We have designed the interface for the List; we now must consider how to implement that interface.  Implementing Lists using an array: for example, the list of integers (2, 6, 8, 7, 1) could be represented as: A 6 8 7 1 1 2 3 4 5 2 current 3 size 5
  • 4. List Implementation  add(9); current position is 3. The new list would thus be: (2, 6, 8, 9, 7, 1)  We will need to shift everything to the right of 8 one place to the right to make place for the new element ‘9’. current 3 size 5 step 1: A 6 8 7 1 1 2 3 4 5 2 6 current 4 size 6 step 2: A 6 8 7 1 1 2 3 4 5 2 6 9 notice: current points to new element
  • 6. Implementing Lists  There are special cases for positioning the current pointer: a. past the last array cell b. before the first cell
  • 7. Implementing Lists  There are special cases for positioning the current pointer: a. past the last array cell b. before the first cell  We will have to worry about these when we write the actual code.
  • 8. Implementing Lists  remove(): removes the element at the current index current 5 size 6 A 6 8 1 1 2 3 4 5 2 6 9 5 Step 1: current 5 size 5 A 6 8 1 1 2 3 4 5 2 9Step 2:
  • 9. Implementing Lists  remove(): removes the element at the current index  We fill the blank spot left by the removal of 7 by shifting the values to the right of position 5 over to the left one space. current 5 size 5 A 6 8 1 1 2 3 4 5 2 9Step 2: current 5 size 6 A 6 8 1 1 2 3 4 5 2 6 9 5 Step 1:
  • 10. Implementing Lists find(X): traverse the array until X is located. int find(int X) { int j; for(j=1; j < size+1; j++ ) if( A[j] == X ) break; if( j < size+1 ) { // found X current = j; // current points to where X found return 1; // 1 for true } return 0; // 0 (false) indicates not found }
  • 11. Implementing Lists  Other operations: get()  return A[current]; update(X)  A[current] = X; length()  return size; back()  current--; start()  current = 1; end()  current = size;
  • 12. Analysis of Array Lists  add  we have to move every element to the right of current to make space for the new element.  Worst-case is when we insert at the beginning; we have to move every element right one place.  Average-case: on average we may have to move half of the elements
  • 13. Analysis of Array Lists  remove  Worst-case: remove at the beginning, must shift all remaining elements to the left.  Average-case: expect to move half of the elements.  find  Worst-case: may have to search the entire array  Average-case: search at most half the array.  Other operations are one-step.
  • 14. List Using Linked Memory  Various cells of memory are not allocated consecutively in memory.
  • 15. List Using Linked Memory  Various cells of memory are not allocated consecutively in memory.  Not enough to store the elements of the list.
  • 16. List Using Linked Memory  Various cells of memory are not allocated consecutively in memory.  Not enough to store the elements of the list.  With arrays, the second element was right next to the first element.
  • 17. List Using Linked Memory  Various cells of memory are not allocated consecutively in memory.  Not enough to store the elements of the list.  With arrays, the second element was right next to the first element.  Now the first element must explicitly tell us where to look for the second element.
  • 18. List Using Linked Memory  Various cells of memory are not allocated consecutively in memory.  Not enough to store the elements of the list.  With arrays, the second element was right next to the first element.  Now the first element must explicitly tell us where to look for the second element.  Do this by holding the memory address of the second element
  • 19. 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.
  • 20. 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
  • 21. 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.
  • 22. 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.
  • 23. 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.
  • 24. 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