In the Name of Allah the Most
Beneficent the Most Merciful
Subject : Data Structures & Algorithms
Lecture : 07
Monday, January 18, 20 1
21
The LIST Data Structure
The List is among the most generic of data
structures.
In daily life we use:
a. shopping list,
b. groceries list,
c. list of people to invite to a dinner
d. List of presents to give
Lists
A list is collection of items that are all of the same
type (grocery items, integers, names)
The items, or elements of the list, are stored in
some particular order
It is possible to insert new elements into various
positions in the list and remove any element of the
list
Lists
The data in arrays are also of same type. When we
say int x[6]; it means that only the integers can be
stored in it. The same is true for list. The data which
we store in list should be of same nature.
The items, or elements of the list, are stored in
some particular order. What does this mean?
Suppose in the list, you have the fruit first which are
also in some order. You may have names in some
alphabetical order i.e. the names which starts with A
should come first followed by the name starting with
B and so on.
Lists
List is a set of elements in a linear order.
For example, data values a1, a2, a3, a4 can be
arranged in a list:
(a3, a1, a2, a4)
In this list, a3, is the first element, a1 is the second
element, and so on
The order is important here; this is not just a
random collection of elements, it is an ordered
collection
List Operations
Useful operations
• createList(): create a new list (presumably empty)
• copy(): set one list to be a copy of another
• clear(); clear a list (remove all elments)
• insert(X, ?): Insert element X at a particular position
in the list
• remove(?): Remove element at some position in
the list
• get(?): Get element at a given position
• update(X, ?): replace the element at a given position
with X
• find(X): determine if the element X is in the list
• length(): return the length of the list.
List Operations
We need to decide what is meant by “particular
position”; we have used “?” for this.
There are two possibilities:
1. Use the actual index of element: insert after element
3, get element number 6. This approach is taken by
arrays
2. Use a “current” marker or pointer to refer to a
particular position in the list.
List Operations
If we use the “current” marker, the following
four methods would be useful:
start(): moves to “current” pointer to the very first
element.
tail(): moves to “current” pointer to the very last
element.
next(): move the current position forward one
element
back(): move the current position backward one
element
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:
current size
A 2 6 8 7 1
3 5
1 2 3 4 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 size
step 1: A 2 6 8 7 1
3 5
1 2 3 4 5 6
step 2: A current size
2 6 8 9 7 1
4 6
1 2 3 4 5 6
notice: current points
to new element
Implementing Lists
next():
current size
A 2 6 8 9 7 1
4 6
1 2 3 4 5 6 5
Implementing Lists
remove(): removes the element at the current
index
current size
Step 1: A 2 6 8 9 1
5 6
1 2 3 4 5 6
5
current size
Step 2: A 2 6 8 9 1
5 5
1 2 3 4 5
Implementing Lists
remove(): removes the element at the current
index
current size
Step 1: A 2 6 8 9 1
5 6
1 2 3 4 5 6
5
current size
Step 2: A 2 6 8 9 1
5 5
1 2 3 4 5
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.
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
A linked list is a collection of components, called
nodes. Every node (except the last node) contains
the address of the next node.
Thus, every node in a linked list has two
components: one to store the relevant information
(data) and the one to store the address, called the
link of the next node in the list.
The address of the first node in the list is stored in
a separate location, called the head or first.
A chain of these nodes forms a linked list
Linked List
Create a structure called a Node.
Object/data Next/link
The object/data field will hold the actual list
element.
The next/link field in the structure will hold the
starting location of the next node.
Chain the nodes together to form a linked list.
Linked List
Linked list: a list of items called nodes, in which
the order of the nodes is determined by the
address, called the link, stored in each node.
Picture of our list (2, 6, 7, 8, 1) stored as a linked
list:
head
2 6 8 7 1 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 6
1052 1063
current 1053 1063
1054 2
head 1055 1051
1056
2 6 8 7 1 1057 7
1058 1060
current 1059
1060 1
1061 0
head 1062 1054
1063 8
1064 1057
1065
Linked List
Linked Lists: some Properties
head 2000 2800 1500 3600
2000 17 2800 92 1500 63 3600 45 /
info link info link info link info
This linked list has four nodes. The address of the first node is stored in the
pointer head. Each node has two components: info to store the info, and link, to
store the address of the next node.