SlideShare a Scribd company logo
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

More Related Content

What's hot

What's hot (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
 

Viewers also liked (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
 

Similar to Data Structure lec#2

computer notes - Stack
computer notes - Stackcomputer notes - Stack
computer notes - Stack
ecomputernotes
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
FaheemMahmood2
 
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
jane3dyson92312
 
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
festockton
 

Similar to 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
 
AD3251-LINKED LIST,STACK ADT,QUEUE ADT.docx
AD3251-LINKED LIST,STACK ADT,QUEUE ADT.docxAD3251-LINKED LIST,STACK ADT,QUEUE ADT.docx
AD3251-LINKED LIST,STACK ADT,QUEUE ADT.docx
 
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
 

More from University of Gujrat, Pakistan

More from 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
 

Recently uploaded

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
 

Recently uploaded (20)

Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptx
 
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
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Keeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security ServicesKeeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security Services
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
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...
 
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
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
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
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
 
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
 
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
 

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