Professional Documents
Culture Documents
Data Structure Lec02 Handout
Data Structure Lec02 Handout
Lecture No.
02
___________________________________________________________________
Data Structures
Lecture No. 02
Reading Material
Data Structures and Algorithm Analysis in C++
Chapter. 3
3.1, 3.2, 3.2.1, 3.2.2
Summary
1)
List Implementation
add Method
next Method
remove Method
find Method
Other Methods
Analysis Of Array List
List Using Linked Memory
Linked List
2)
3)
4)
Today, we will discuss the concept of list operations. You may have a fair idea of
start operation that sets the current pointer to the first element of the list while the tail
operation moves the current pointer to the last element of the list. In the previous
lecture, we discussed the operation next that moves the current pointer one element
forward. Similarly, there is the back operation which moves the current pointer one
element backward.
List Implementation
Now we will see what the implementation of the list is and how one can create a list in
C++. After designing the interface for the list, it is advisable to know how to
implement that interface. Suppose we want to create a list of integers. For this
purpose, the methods of the list can be implemented with the use of an array inside.
For example, the list of integers (2, 6, 8, 7, 1) can be represented in the following
manner where the current position is 3.
A
current
size
In this case, we start the index of the array from 1 just for simplification against the
usual practice in which the index of an array starts from zero in C++. It is not
necessary to always start the indexing from zero. Sometimes, it is required to start the
indexing from 1. For this, we leave the zeroth position and start using the array from
index 1 that is actually the second position. Suppose we have to store the numbers
from 1 to 6 in the array. We take an array of 7 elements and put the numbers from the
index 1. Thus there is a correspondence between index and the numbers stored in it.
This is not very useful. So, it does not justify the non-use of zeroth position of the
Page 1 of 8
7
4
current
size
Now in the second step, we put the element 9 at the empty space i.e. position 4. Thus
the array will attain the following shape. The figure shows the elements in the array in
the same order as stored in the list.
A
current
size
We have moved the current position to 4 while increasing the size to 6. The size shows
that the elements in the list. Where as the size of the array is different that we have
defined already to a fixed length, which may be 100, 200 or even greater.
next Method
Now lets see another method, called next. We have talked that the next method
moves the current position one position forward. In this method, we do not add a new
element to the list but simply move the pointer one element ahead. This method is
required while employing the list in our program and manipulating it according to the
requirement. There is also an array to store the list in it. We also have two variablescurrent and size to store the position of current pointer and the number of elements in
the list. By looking on the values of these variables, we can find the state of the list i.e.
how many elements are in the list and at what position the current pointer is.
The method next is used to know about the boundary conditions of the list i.e. the
array being used by us to implement the list. To understand the boundary conditions,
we can take the example of an array of size 100 to implement the list. Here, 100
elements are added to the array. Lets see what happens when we want to add 101 st
element to the array? We used to move the current position by next method and
reached the 100th position. Now, in case of moving the pointer to the next position (i.e.
101st), there will be an error as the size of the array is 100, having no position after this
point. Similarly if we move the pointer backward and reach at the first position
regardless that the index is 0 or 1. But what will happen if we want to move backward
from the first position? These situations are known as boundary conditions and need
attention during the process of writing programs when we write the code to use the
list. We will take care of these things while implementing the list in C++ programs.
remove Method
We have seen that the add method adds an element in the list. Now we are going to
Page 2 of 8
current
size
6
5
We fill in the blank position left by the removal of 7 by shifting the values on the right
of position 5 to the left by one space. This means that we shift the remaining elements
on the right hand side of the current position one place to the left so that the element
next to the removed element (i.e. 1) takes its place (the fifth position) and becomes the
current position element. We do not change the current pointer that is still pointing to
the position 5. Thus the current pointer remains pointing to the position 5 despite the
fact that there is now element 1 at this place instead of 7. Thus in the remove method,
when we remove an element, the element next to it on the right hand side comes at its
place and the remaining are also shifted one place to the right. This step is represented
by the following figure.
current
size
find Method
Now lets talk about a function, used to find a specific element in the array. The find (x)
function is used to find a specific element in the array. We pass the element, which is to
be found, as an argument to the find function. This function then traverses the array
until the specific element is found. If the element is found, this function sets the current
position to it and returns 1 i.e. true. On the other hand, if the element is not found, the
function returns 0 i.e. false. This indicates that the element was not found. Following is
the code of this find(x) function in C++.
int find (int x)
{
int j ;
for (j = 1; j < size + 1; j++ )
if (A[j] == x )
break ;
if ( j < size + 1)
{
current = j ;
return 1 ;
}
return 0 ;
}
// x is found
//current points to the position where x found
// return true
//return false, x is not found
In the above code, we execute a for loop to traverse the array. The number of
Page 3 of 8
Page 4 of 8
Linked List
For the utilization of the concept of linked memory, we usually define a structure,
called linked list. To form a linked list, at first, we define a node. A node comprises two
fields. i.e. the object field that holds the actual list element and the next that holds the
starting location of the next node.
Page 6 of 8
next
A chain of these nodes forms a linked list. Now lets consider our previous list, used
with an array i.e. 2, 6, 8, 7, 1. Following is the figure which represents the list stored as
a linked list.
Head
2
size = 5
current
This diagram just represents the linked list. In the memory, different nodes may occur
at different locations but the next part of each node contains the address of the next
node. Thus it forms a chain of nodes which we call a linked list.
While using an array we knew that the array started from index 1that means the first
element of the list is at index 1. Similarly in the linked list we need to know the starting
point of the list. For this purpose, we have a pointer head that points to the first node
of the list. If we dont use head, it will not be possible to know the starting position of
the list. We also have a pointer current to point to the current node of the list. We need
this pointer to add or remove current node from the list. Here in the linked list, the
current is a pointer and not an index as we used while using an array. The next field of
the last node points to nothing .It is the end of the list. We place the memory address
NULL in the last node. NULL is an invalid address and is inaccessible.
Now again consider the list 2, 6, 8, 7, 1. The previous figure represents this list as a
linked list. In this linked list, the head points to 2, 2 points to 6, 6 points to 8, 8 points
to 7 and 7 points to 1. Moreover we have the current position at element 8.
This linked list is stored in the memory. The following diagram depicts the process
through which this linked list is stored in the memory.
Page 7 of 8
current
head
1051
1052
1063
1053
1054
1055
1056
1051
1057
1058
7
1060
1059
1060
1061
1062
0
1054
1063
1064
8
1057
1065
We can see in the figure that each memory location has an address. Normally in
programming, we access the memory locations by some variable names. These variable
names are alias for these locations and are like labels that are put to these memory
locations. We use head and current variable names instead of using the memory
address in numbers for starting and the current nodes. In the figure, we see that head is
the name of the memory location 1062 and the name current is used for the memory
address 1053. The head holds the address 1054 and the element 2, the first one in the
list, is stored in the location 1054. Similarly current holds the address 1063 where the
element 8 is stored which is our current position in the list. In the diagram, two
memory locations comprise a node. So we see that the location 1054 holds the element
2 while the next location 1055 holds the address of the memory location (1051) where
the next element of the list (i.e. 6) is stored. Similarly the next part of the node that has
value 6 holds the memory address of the location occupied by the next element (i.e. 8)
of the list. The other nodes are structured in a similar fashion. Thus, by knowing the
address of the next element we can traverse the whole list.
Page 8 of 8