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

PPT
Lecture3
PPT
Lecture4
PPT
Data Structure Lecture 6
PPT
linked list
PPT
Data structure lecture 5
PPT
Unit ii(dsc++)
PPT
Link List
PDF
Doubly Link List
Lecture3
Lecture4
Data Structure Lecture 6
linked list
Data structure lecture 5
Unit ii(dsc++)
Link List
Doubly Link List

What's hot (20)

PPT
Data Structure Lecture 5
PPTX
Data Structures - Lecture 7 [Linked List]
PPTX
Linked lists 1
PPT
Circular linked list
PPSX
Data Structure (Double Linked List)
PPTX
Linked lists in Data Structure
PPSX
Data Structure (Dynamic Array and Linked List)
PPT
header, circular and two way linked lists
PPTX
Linked list
PPTX
CSE240 Doubly Linked Lists
PPTX
PPSX
Data Structure (Circular Linked List)
PPTX
Doubly linked list
PDF
Circular linked list
PPT
Data Structure lec#2
PPTX
Linked lists a
PPT
Linked list
PPTX
Linked list
PPTX
Circular link list.ppt
Data Structure Lecture 5
Data Structures - Lecture 7 [Linked List]
Linked lists 1
Circular linked list
Data Structure (Double Linked List)
Linked lists in Data Structure
Data Structure (Dynamic Array and Linked List)
header, circular and two way linked lists
Linked list
CSE240 Doubly Linked Lists
Data Structure (Circular Linked List)
Doubly linked list
Circular linked list
Data Structure lec#2
Linked lists a
Linked list
Linked list
Circular link list.ppt
Ad

Viewers also liked (15)

PDF
La vision de la supervision libre en entreprise
PPTX
Cloud computing disadvantages
PPT
Lecture8
PPTX
Why do m tech
PPT
Lecture7
PPTX
Why do m tech
PDF
Hobijeve pustolovine
PDF
Pjesmarica DZ Zvončići
PPT
Lecture10
PPTX
Cloud computing
DOCX
Cyber crime in pakistan by zubair
PPT
Lecture1
PDF
Moti_중간발표
PPT
Lecture9 recursion
DOCX
Project ISR - Mary HPDG-JL14-1132
La vision de la supervision libre en entreprise
Cloud computing disadvantages
Lecture8
Why do m tech
Lecture7
Why do m tech
Hobijeve pustolovine
Pjesmarica DZ Zvončići
Lecture10
Cloud computing
Cyber crime in pakistan by zubair
Lecture1
Moti_중간발표
Lecture9 recursion
Project ISR - Mary HPDG-JL14-1132
Ad

Similar to Lecture2 (20)

PPTX
introduction_dst.pptx
PPTX
Data Structures and Agorithm: DS 02 Array List.pptx
PPT
linked list (c#)
PPTX
Data structures and Algorithm analysis_Lecture 2.pptx
PPT
Data structures
DOCX
AD3251-LINKED LIST,STACK ADT,QUEUE ADT.docx
PPTX
Data Structures and Algorithms: introduction
PPTX
Adt of lists
PDF
3. List .pdf
PPTX
8.DATA STRUCTURES UNIT 1 AND 2 CS3301PPT.pptx
PPT
lecture 02.2.ppt
PDF
Linked list
PDF
computer notes - List implementation
PPTX
ds bridge.pptx
PDF
4 chapter3 list_stackqueuepart1
PPTX
List,Stacks and Queues.pptx
PPT
Array linked list.ppt
PPTX
Data structures and algorithms
PPT
computer notes - Data Structures - 2
introduction_dst.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
linked list (c#)
Data structures and Algorithm analysis_Lecture 2.pptx
Data structures
AD3251-LINKED LIST,STACK ADT,QUEUE ADT.docx
Data Structures and Algorithms: introduction
Adt of lists
3. List .pdf
8.DATA STRUCTURES UNIT 1 AND 2 CS3301PPT.pptx
lecture 02.2.ppt
Linked list
computer notes - List implementation
ds bridge.pptx
4 chapter3 list_stackqueuepart1
List,Stacks and Queues.pptx
Array linked list.ppt
Data structures and algorithms
computer notes - Data Structures - 2

Recently uploaded (20)

PPTX
GDM (1) (1).pptx small presentation for students
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
master seminar digital applications in india
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
GDM (1) (1).pptx small presentation for students
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Abdominal Access Techniques with Prof. Dr. R K Mishra
FourierSeries-QuestionsWithAnswers(Part-A).pdf
master seminar digital applications in india
Microbial disease of the cardiovascular and lymphatic systems
Insiders guide to clinical Medicine.pdf
Week 4 Term 3 Study Techniques revisited.pptx
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
O5-L3 Freight Transport Ops (International) V1.pdf
Basic Mud Logging Guide for educational purpose
STATICS OF THE RIGID BODIES Hibbelers.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
01-Introduction-to-Information-Management.pdf
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
O7-L3 Supply Chain Operations - ICLT Program
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPH.pptx obstetrics and gynecology in nursing
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf

Lecture2

  • 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