SlideShare a Scribd company logo
1 of 16
Lecture # 2Lecture # 2
LIST Data StructureLIST Data Structure
 The List is among the most generic of dataThe List is among the most generic of data
structures.structures.
 In Real life:In Real life:
 shopping list,shopping list,
 groceries list,groceries list,
 list of people to invite to dinnerlist of people to invite to dinner

List of presents to getList of presents to get
ListsLists
 AA listlist is collection of items that are all of theis collection of items that are all of the
same typesame type (grocery items, integers, names)(grocery items, integers, names)
 The items, or elements of the list, are stored inThe items, or elements of the list, are stored in
some particular ordersome particular order
 It is possible to insert new elements into variousIt is possible to insert new elements into various
positions in the list and remove any element ofpositions in the list and remove any element of
the listthe list
ListsLists
 List is a set of elements in a linear order.List is a set of elements in a linear order.
For example, data values a1, a2, a3, a4 can be arrangedFor example, data values a1, a2, a3, a4 can be arranged
in a list:in a list:
(a3, a1, a2, a4)(a3, a1, a2, a4)
In this list, a3, is the first element, a1 is the secondIn this list, a3, is the first element, a1 is the second
element, and so onelement, and so on
 The order is important here; this is not just a randomThe order is important here; this is not just a random
collection of elements, it is ancollection of elements, it is an orderedordered collectioncollection
List OperationsList Operations
Useful operationsUseful operations
 createList(): create a new list (presumably empty)createList(): create a new list (presumably empty)
 copy(): set one list to be a copy of anothercopy(): set one list to be a copy of another
 clear(); clear a list (remove all elements)clear(); clear a list (remove all elements)
 insert(X, ?): Insert element X at a particular positioninsert(X, ?): Insert element X at a particular position
in the listin the list
 remove(?): Remove element at some position inremove(?): Remove element at some position in
the listthe list
 get(?): Get element at a given positionget(?): Get element at a given position
 update(X, ?): replace the element at a given positionupdate(X, ?): replace the element at a given position
with Xwith X
 find(X): determine if the element X is in the listfind(X): determine if the element X is in the list
 length(): return the length of the list.length(): return the length of the list.
List OperationsList Operations
 We need to decide what is meant by “particularWe need to decide what is meant by “particular
position”; we have used “?” for this.position”; we have used “?” for this.
 There are two possibilities:There are two possibilities:
 Use the actual index of element: insert after elementUse the actual index of element: insert after element
3, get element number 6. This approach is taken by3, get element number 6. This approach is taken by
arraysarrays
 Use a “current” marker orUse a “current” marker or pointerpointer to refer to ato refer to a
particular position in the list.particular position in the list.
List OperationsList Operations
 If we use the “current” marker, the following fourIf we use the “current” marker, the following four
methods would be useful:methods would be useful:
 start()start(): moves to “current” pointer to the very first: moves to “current” pointer to the very first
element.element.
 tail()tail(): moves to “current” pointer to the very last: moves to “current” pointer to the very last
element.element.
 nextnext(): move the current position forward one(): move the current position forward one
elementelement
 backback(): move the current position backward one(): move the current position backward one
elementelement
Implementing ListsImplementing Lists
 We have designed the interface for the List; weWe have designed the interface for the List; we
now must consider how to implement thatnow must consider how to implement that
interfaceinterface..
 Implementing Lists using an array: for example,Implementing Lists using an array: for example,
the list of integers (2, 6, 8, 7, 1) could bethe list of integers (2, 6, 8, 7, 1) could be
represented as:represented as:
List ImplementationList Implementation
 add(9);add(9); current position is 3. The new list wouldcurrent position is 3. The new list would
thus be: (2, 6, 8, 9, 7, 1)thus be: (2, 6, 8, 9, 7, 1)
 We will need toWe will need to shiftshift everything to the right of 8everything to the right of 8
one place to the right to make place for the newone place to the right to make place for the new
element ‘9’.element ‘9’.
Implementing ListsImplementing Lists
 next():next():
Implementing ListsImplementing Lists
 There are special cases for positioning theThere are special cases for positioning the
current pointer:current pointer:
 past the last array cellpast the last array cell
 before the first cellbefore the first cell
 We will have to worry about these when weWe will have to worry about these when we
write the actual code.write the actual code.
Implementing ListsImplementing Lists
 remove():remove(): removes the element at theremoves the element at the
current indexcurrent index
 We fill the blank spot left by the removal of 7 byWe fill the blank spot left by the removal of 7 by
shifting the values to the right of position 5 overshifting the values to the right of position 5 over
to the left one spaceto the left one space
Implementing ListsImplementing Lists
 find(X):find(X): traverse the array until X is located.traverse the array until X is located.
int find(int X)int find(int X)
{{
int j;int j;
for(j=1; j < size+1; j++ )for(j=1; j < size+1; j++ )
if( A[j] == X ) break;if( A[j] == X ) break;
if( j < size+1 )if( j < size+1 )
{ // found X{ // found X
current = j; // current points to where X foundcurrent = j; // current points to where X found
return 1; // 1 for truereturn 1; // 1 for true
}}
return 0; // 0 (false) indicates not foundreturn 0; // 0 (false) indicates not found
}}
Implementing ListsImplementing Lists
 Other operations:Other operations:
get()get()  return A[current];return A[current];
update(X)update(X)  A[current] = X;A[current] = X;
length()length()  return size;return size;
back()back()  current--;current--;
start()start()  current = 1;current = 1;
end()end()  current = size;current = size;
Assignment # 1Assignment # 1
 Implement aImplement a List Data StructureList Data Structure discussed above includingdiscussed above including
following operations using Array ADT.following operations using Array ADT.
 get()get()
 update()update()
 length()length()
 back()back()
 Next()Next()
 start()start()
 end()end()
 Remove()Remove()
 Add()Add()
 NOTE:NOTE: Implement above operations only usingImplement above operations only using PointersPointers
withoutwithout using anyusing any indexesindexes of arrays.of arrays.
 Assignment will be graded on the basis of relevant quiz ofAssignment will be graded on the basis of relevant quiz of
this assignment.this assignment.
List UsingList Using Linked MemoryLinked Memory
 Various cells of memory areVarious cells of memory are not allocatednot allocated
consecutivelyconsecutively in memory.in memory.
 Array is Not enough to store the future elements ofArray is Not enough to store the future elements of
the list after full exhaust of array locations.the list after full exhaust of array locations.
 With arrays, the second element was right next toWith arrays, the second element was right next to
the first element.the first element.
 Now inNow in Linked Memory approach,Linked Memory approach, the first elementthe first element
mustmust explicitlyexplicitly tell us where to look for the secondtell us where to look for the second
element.element.
 Do this by holding the memory address of theDo this by holding the memory address of the
second elementsecond element

More Related Content

What's hot

What's hot (20)

Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Linked lists 1
Linked lists 1Linked lists 1
Linked lists 1
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Data Structure (Double Linked List)
Data Structure (Double Linked List)Data Structure (Double Linked List)
Data Structure (Double Linked List)
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked lists
 
Linked list
Linked listLinked list
Linked list
 
CSE240 Doubly Linked Lists
CSE240 Doubly Linked ListsCSE240 Doubly Linked Lists
CSE240 Doubly Linked Lists
 
Linklist
LinklistLinklist
Linklist
 
Team 10
Team 10Team 10
Team 10
 
Data Structure (Circular Linked List)
Data Structure (Circular Linked List)Data Structure (Circular Linked List)
Data Structure (Circular Linked List)
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Data Structure lec#2
Data Structure lec#2Data Structure lec#2
Data Structure lec#2
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 

Viewers also liked

Viewers also liked (15)

La vision de la supervision libre en entreprise
La vision de la supervision libre en entrepriseLa vision de la supervision libre en entreprise
La vision de la supervision libre en entreprise
 
Cloud computing disadvantages
Cloud computing disadvantagesCloud computing disadvantages
Cloud computing disadvantages
 
Lecture8
Lecture8Lecture8
Lecture8
 
Why do m tech
Why do m techWhy do m tech
Why do m tech
 
Lecture7
Lecture7Lecture7
Lecture7
 
Why do m tech
Why do m techWhy do m tech
Why do m tech
 
Hobijeve pustolovine
Hobijeve pustolovineHobijeve pustolovine
Hobijeve pustolovine
 
Pjesmarica DZ Zvončići
Pjesmarica DZ ZvončićiPjesmarica DZ Zvončići
Pjesmarica DZ Zvončići
 
Lecture10
Lecture10Lecture10
Lecture10
 
Cloud computing
Cloud computingCloud computing
Cloud computing
 
Cyber crime in pakistan by zubair
Cyber crime in pakistan by zubairCyber crime in pakistan by zubair
Cyber crime in pakistan by zubair
 
Lecture1
Lecture1Lecture1
Lecture1
 
Moti_중간발표
Moti_중간발표Moti_중간발표
Moti_중간발표
 
Lecture9 recursion
Lecture9 recursionLecture9 recursion
Lecture9 recursion
 
Project ISR - Mary HPDG-JL14-1132
Project ISR - Mary HPDG-JL14-1132 Project ISR - Mary HPDG-JL14-1132
Project ISR - Mary HPDG-JL14-1132
 

Similar to List Data Structure Implementation Using Arrays

Similar to List Data Structure Implementation Using Arrays (20)

List interface
List interfaceList interface
List interface
 
Lecture5
Lecture5Lecture5
Lecture5
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
 
introduction_dst.pptx
introduction_dst.pptxintroduction_dst.pptx
introduction_dst.pptx
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
07 java collection
07 java collection07 java collection
07 java collection
 
Collection and framework
Collection and frameworkCollection and framework
Collection and framework
 
16 containers
16   containers16   containers
16 containers
 
Python list
Python listPython list
Python list
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
 
javacollections.pdf
javacollections.pdfjavacollections.pdf
javacollections.pdf
 
lec4.ppt
lec4.pptlec4.ppt
lec4.ppt
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Data structures1
Data structures1Data structures1
Data structures1
 
Aj unit2 notesjavadatastructures
Aj unit2 notesjavadatastructuresAj unit2 notesjavadatastructures
Aj unit2 notesjavadatastructures
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
Lec3
Lec3Lec3
Lec3
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
stack.ppt
stack.pptstack.ppt
stack.ppt
 

Recently uploaded

ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 

Recently uploaded (20)

ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 

List Data Structure Implementation Using Arrays

  • 2. LIST Data StructureLIST Data Structure  The List is among the most generic of dataThe List is among the most generic of data structures.structures.  In Real life:In Real life:  shopping list,shopping list,  groceries list,groceries list,  list of people to invite to dinnerlist of people to invite to dinner  List of presents to getList of presents to get
  • 3. ListsLists  AA listlist is collection of items that are all of theis collection of items that are all of the same typesame type (grocery items, integers, names)(grocery items, integers, names)  The items, or elements of the list, are stored inThe items, or elements of the list, are stored in some particular ordersome particular order  It is possible to insert new elements into variousIt is possible to insert new elements into various positions in the list and remove any element ofpositions in the list and remove any element of the listthe list
  • 4. ListsLists  List is a set of elements in a linear order.List is a set of elements in a linear order. For example, data values a1, a2, a3, a4 can be arrangedFor example, data values a1, a2, a3, a4 can be arranged in a list:in a list: (a3, a1, a2, a4)(a3, a1, a2, a4) In this list, a3, is the first element, a1 is the secondIn this list, a3, is the first element, a1 is the second element, and so onelement, and so on  The order is important here; this is not just a randomThe order is important here; this is not just a random collection of elements, it is ancollection of elements, it is an orderedordered collectioncollection
  • 5. List OperationsList Operations Useful operationsUseful operations  createList(): create a new list (presumably empty)createList(): create a new list (presumably empty)  copy(): set one list to be a copy of anothercopy(): set one list to be a copy of another  clear(); clear a list (remove all elements)clear(); clear a list (remove all elements)  insert(X, ?): Insert element X at a particular positioninsert(X, ?): Insert element X at a particular position in the listin the list  remove(?): Remove element at some position inremove(?): Remove element at some position in the listthe list  get(?): Get element at a given positionget(?): Get element at a given position  update(X, ?): replace the element at a given positionupdate(X, ?): replace the element at a given position with Xwith X  find(X): determine if the element X is in the listfind(X): determine if the element X is in the list  length(): return the length of the list.length(): return the length of the list.
  • 6. List OperationsList Operations  We need to decide what is meant by “particularWe need to decide what is meant by “particular position”; we have used “?” for this.position”; we have used “?” for this.  There are two possibilities:There are two possibilities:  Use the actual index of element: insert after elementUse the actual index of element: insert after element 3, get element number 6. This approach is taken by3, get element number 6. This approach is taken by arraysarrays  Use a “current” marker orUse a “current” marker or pointerpointer to refer to ato refer to a particular position in the list.particular position in the list.
  • 7. List OperationsList Operations  If we use the “current” marker, the following fourIf we use the “current” marker, the following four methods would be useful:methods would be useful:  start()start(): moves to “current” pointer to the very first: moves to “current” pointer to the very first element.element.  tail()tail(): moves to “current” pointer to the very last: moves to “current” pointer to the very last element.element.  nextnext(): move the current position forward one(): move the current position forward one elementelement  backback(): move the current position backward one(): move the current position backward one elementelement
  • 8. Implementing ListsImplementing Lists  We have designed the interface for the List; weWe have designed the interface for the List; we now must consider how to implement thatnow must consider how to implement that interfaceinterface..  Implementing Lists using an array: for example,Implementing Lists using an array: for example, the list of integers (2, 6, 8, 7, 1) could bethe list of integers (2, 6, 8, 7, 1) could be represented as:represented as:
  • 9. List ImplementationList Implementation  add(9);add(9); current position is 3. The new list wouldcurrent position is 3. The new list would thus be: (2, 6, 8, 9, 7, 1)thus be: (2, 6, 8, 9, 7, 1)  We will need toWe will need to shiftshift everything to the right of 8everything to the right of 8 one place to the right to make place for the newone place to the right to make place for the new element ‘9’.element ‘9’.
  • 11. Implementing ListsImplementing Lists  There are special cases for positioning theThere are special cases for positioning the current pointer:current pointer:  past the last array cellpast the last array cell  before the first cellbefore the first cell  We will have to worry about these when weWe will have to worry about these when we write the actual code.write the actual code.
  • 12. Implementing ListsImplementing Lists  remove():remove(): removes the element at theremoves the element at the current indexcurrent index  We fill the blank spot left by the removal of 7 byWe fill the blank spot left by the removal of 7 by shifting the values to the right of position 5 overshifting the values to the right of position 5 over to the left one spaceto the left one space
  • 13. Implementing ListsImplementing Lists  find(X):find(X): traverse the array until X is located.traverse the array until X is located. int find(int X)int find(int X) {{ int j;int j; for(j=1; j < size+1; j++ )for(j=1; j < size+1; j++ ) if( A[j] == X ) break;if( A[j] == X ) break; if( j < size+1 )if( j < size+1 ) { // found X{ // found X current = j; // current points to where X foundcurrent = j; // current points to where X found return 1; // 1 for truereturn 1; // 1 for true }} return 0; // 0 (false) indicates not foundreturn 0; // 0 (false) indicates not found }}
  • 14. Implementing ListsImplementing Lists  Other operations:Other operations: get()get()  return A[current];return A[current]; update(X)update(X)  A[current] = X;A[current] = X; length()length()  return size;return size; back()back()  current--;current--; start()start()  current = 1;current = 1; end()end()  current = size;current = size;
  • 15. Assignment # 1Assignment # 1  Implement aImplement a List Data StructureList Data Structure discussed above includingdiscussed above including following operations using Array ADT.following operations using Array ADT.  get()get()  update()update()  length()length()  back()back()  Next()Next()  start()start()  end()end()  Remove()Remove()  Add()Add()  NOTE:NOTE: Implement above operations only usingImplement above operations only using PointersPointers withoutwithout using anyusing any indexesindexes of arrays.of arrays.  Assignment will be graded on the basis of relevant quiz ofAssignment will be graded on the basis of relevant quiz of this assignment.this assignment.
  • 16. List UsingList Using Linked MemoryLinked Memory  Various cells of memory areVarious cells of memory are not allocatednot allocated consecutivelyconsecutively in memory.in memory.  Array is Not enough to store the future elements ofArray is Not enough to store the future elements of the list after full exhaust of array locations.the list after full exhaust of array locations.  With arrays, the second element was right next toWith arrays, the second element was right next to the first element.the first element.  Now inNow in Linked Memory approach,Linked Memory approach, the first elementthe first element mustmust explicitlyexplicitly tell us where to look for the secondtell us where to look for the second element.element.  Do this by holding the memory address of theDo this by holding the memory address of the second elementsecond element