The document discusses lists as a data structure and how they can be implemented. Lists are collections of ordered elements that allow inserting, removing, and accessing elements by position. Lists can be implemented using arrays by storing elements in consecutive memory locations and tracking the current position. Operations like adding, removing, getting, and updating elements require shifting elements in the array. Linked lists are also discussed as an alternative implementation that uses linked memory locations rather than a consecutive array.
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
}}
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