SlideShare a Scribd company logo
1 of 8
Download to read offline
ecomputernotes.com Data Structures Lecture No. 02
___________________________________________________________________
Page 1 of 8
Data Structures
Lecture No. 02
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 2 6 8 7 1 current size
1 2 3 4 5 3 5
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
array out-rightly. However for simplification purposes, it is good to use the index from
1.
add Method
Now we will talk about adding an element to the list. Suppose there is a call to add an
element in the list i.e. add(9). As we said earlier that the current position is 3, so by
adding the element 9 to the list, the new list will be (2, 6, 8, 9, 7, 1).
To add the new element (9) to the list at the current position, at first, we have to make
space for this element. For this purpose, we shift every element on the right of 8 (the
current position) to one place on the right. Thus after creating the space for new
element at position 4, the array can be represented as
A 2 6 8 7 1 current size
ecomputernotes.com Data Structures Lecture No. 02
___________________________________________________________________
Page 2 of 8
1 2 3 4 5 3 5
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 2 6 8 9 7 1 current size
1 2 3 4 5 6 4 6
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 let s 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 variables-
current 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. Let s see what happens when we want to add 101st
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
discuss the remove method. The remove method removes the element residing at the
current position. The removal of the element will be carried out as follows. Suppose
there are 6 elements (2, 6, 8, 9, 7, 1) in the list. The current pointer is pointing to the
position 5 that has the value 7. We remove the element, making the current position
empty. The size of the list will become 5. This is represented in the following figure.
A 2 6 8 9 1 current size
1 2 3 4 5 6 5 6
5
ecomputernotes.com Data Structures Lecture No. 02
___________________________________________________________________
Page 3 of 8
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.
A 2 6 8 9 1 current size
1 2 3 4 5 5 5
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) // x is found
{
current = j ; //current points to the position where x found
return 1 ; // return true
}
return 0 ; //return false, x is not found
}
In the above code, we execute a for loop to traverse the array. The number of
execution of this loop is equal to the size of the list. This for loop gets terminated
when the value of loop variable (j) increases from the size of the list. However we
terminate the loop with the break statement if the element is found at a position. When
the control comes out from the loop, we check the value of j. If the value of j is less
than the size of the array, it means that the loop was terminated by the break
statement. We use the break statement when we find the required element (x) in the
list. The execution of break statement shows that the required element was found at
the position equal to the value of j. So the program sets the current position to j and
ecomputernotes.com Data Structures Lecture No. 02
___________________________________________________________________
Page 4 of 8
comes out the function by returning 1 (i.e. true). If the value of j is greater than the
size of the array, it means that the whole array has traversed and the required element
is not found. So we simply return 0 (i.e. false) and come out of the function.
Other Methods
There are some other methods to implement the list using an array. These methods are
very simple, which perform their task just in one step (i.e. in one statement). There is a
get() method , used to get the element from the current position in the array. The
syntax of this function is of one line and is as under
return A[current] ;
This statement returns the element to which the current is pointing to (i.e. the current
position) in the list A.
Another function is update(x). This method is used to change (set) the value at the
current position. A value is passed to this method as an argument. It puts that value at
the current position. The following statement in this method carries out this process.
A [current] = x ;
Then there is a method length( ).This method returns the size of the list. The syntax of
this method is
return size ;
You may notice here that we are returning the size of the list and not the size of the
array being used internally to implement the list. This size is the number of the
elements of the list, stored in the array.
The back() method decreases the value of variable current by 1. In other words, it
moves the current position one element backward. This is done by writing the
statement.
current -- ;
The -- is a decrement operator in C++ that decreases the value of the operand by one.
The above statement can also be written as
current = current -1 ;
The start() method sets the current position to the first element of the list. We know
that the index of the array starts from 0 but we use the index 1 for the starting position.
We do not use the index zero. So we set the current position to the first element by
writing
current = 1 ;
Similarly, the end() method sets the current position to the last element of the list i.e.
size. So we write
current = size ;
ecomputernotes.com Data Structures Lecture No. 02
___________________________________________________________________
Page 5 of 8
Analysis of Array List
Now we analyze the implementation of the list while using an array internally. We
analyze different methods used for the implementation of the list. We try to see the
level upto which these are efficient in terms of CPU s time consumption. Time is the
major factor to see the efficiency of a program.
Add
First of all, we have talked about the add method. When we add an element to the list,
every element is moved to the right of the current position to make space for the new
element. So, if the current position is the start of the list and we want to add an
element in the beginning, we have to shift all the elements of the list to the right one
place. This is the worst case of adding an element to the list. Suppose if the size of the
list is 10000 or 20000, we have to do the shift operation for all of these 10000 or
20000 elements. Normally, it is done by shifting of elements with the use of a for loop.
This operation takes much time of the CPU and thus it is not a good practice to add an
element at the beginning of a list. On the other hand, if we add an element at the end of
the list, it can be done by carrying out no shift operation . It is the best case of adding
an element to the list. However, normally we may have to move half of the elements.
The usage of add method is the matter warranting special care at the time of
implementation of the list in our program. To provide the interface of the list, we just
define these methods.
Remove
When we remove an element at the current position in the list, its space gets empty.
The current pointer remains at the same position. To fill this space, we shift the
elements on the right of this empty space one place to the left. If we remove an
element from the beginning of the list, then we have to shift the entire remaining
elements one place to the left. Suppose there is a large number of elements, say 10000
or 20000, in the list. We remove the first element from the list. Now to fill this space,
the remaining elements are shifted (that is a large number). Shifting such a large
number of elements is time consuming process. The CPU takes time to execute the for
loop that performs this shift operation. Thus to remove an element at the beginning of
the list is the worst case of remove method. However it is very easy to remove an
element at the end of the list. In average cases of the remove method we expect to shift
half of the elements. This average does not mean that in most of the cases, you will
have to shift half the elements. It is just the average. We may have to shift all the
elements in one operation (if we remove at the beginning) and in the second operation,
we have to shift no element (if we remove at the end). Similarly, in certain operations,
we have to shift just 10, 15 elements.
Find
We have discussed that the find method takes an element and traverses the list to find
that element. The worst case of the find method is that it has to search the entire list
from beginning to end. So, it finds the element at the end of the array or the element is
not found. On average the find method searches at most half the list.
The other methods get (), length () etc are one-step methods. They carry out their
operation in one instruction. There is no need of any loop or other programming
ecomputernotes.com Data Structures Lecture No. 02
___________________________________________________________________
Page 6 of 8
structures to perform the task. The get() method gets the value from the specified
position just in one step. Similarly the update() method sets a value at the specific
position just in one-step. The length () method returns the value of the size of the list.
The other methods back(), start() and end() also perform their tasks only in one step.
List using Linked Memory
We have seen the implementation of the list with the use of an array. Now we will
discuss the implementation of the list while using linked memory. In an array, the
memory cells of the array are linked with each other. It means that the memory of the
array is contiguous. In an array, it is impossible that one element of the array is located
at a memory location while the other element is located somewhere far from it in the
memory. It is located in very next location in the memory. It is a property of the array
that its elements are placed together with one another in the memory. Moreover, when
we have declared the size of the array, it is not possible to increase or decrease it
during the execution of the program. If we need more elements to store in the array,
there is need of changing its size in the declaration. We have to compile the program
again before executing it. Now array will be of the new size. But what happens if we
again need to store more elements? We will change the code of our program to change
the declaration of the array while recompiling it.
Suppose we have used the dynamic memory allocation and created an array of 100
elements with the use of new operator. In case of need of 200 elements, we will release
this array and allocate a new array of 200 elements. Before releasing the previous
array, it will wise to copy its elements to the new array so that it does not lose any
information. Now this new array is in ready for use position. Thus the procedure of
creating a new array is not an easy task.
To avoid such problems, usually faced by the programmers while using an array, there
is need of using linked memory in which the various cells of memory, are not located
continuously. In this process, each cell of the memory not only contains the value of
the element but also the information where the next element of the list is residing in the
memory. It is not necessary that the next element is at the next location in the memory.
It may be anywhere in the memory. We have to keep a track of it. Thus, in this way,
the first element must explicitly have the information about the location of the second
element. Similarly, the second element must know where the third element is located
and the third should know the position of the fourth element and so on. Thus, each cell
(space) of the list will provide the value of the element along with the information
about where the next element is in the memory. This information of the next element is
accomplished by holding the memory address of the next element. The memory
address can be understood as the index of the array. As in case of an array, we can
access an element in the array by its index. Similarly, we can access a memory location
by using its address, normally called memory address.
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.
ecomputernotes.com Data Structures Lecture No. 02
___________________________________________________________________
Page 7 of 8
object next
A chain of these nodes forms a linked list. Now let s 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 6 8 7 1 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 don t 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.
ecomputernotes.com Data Structures Lecture No. 02
___________________________________________________________________
Page 8 of 8
1051 6
1052 1063
current 1053
1054 2
1055 1051
1056
1057 7
1058 1060
1059
1060 1
1061 0
head 1062 1054
1063 8
1064 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.

More Related Content

What's hot

data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sortingguest2cb109
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhithRj Juhith
 
List Data Structure
List Data StructureList Data Structure
List Data StructureZidny Nafan
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithmmaamir farooq
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searchingsajinis3
 
Concept of Stream API Java 1.8
Concept of Stream API Java 1.8Concept of Stream API Java 1.8
Concept of Stream API Java 1.8InnovationM
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsDrishti Bhalla
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting AlgorithmsRahul Jamwal
 
Coin Changing, Binary Search , Linear Search - Algorithm
Coin Changing, Binary Search , Linear Search - AlgorithmCoin Changing, Binary Search , Linear Search - Algorithm
Coin Changing, Binary Search , Linear Search - AlgorithmMd Sadequl Islam
 
7 searching injava-binary
7 searching injava-binary7 searching injava-binary
7 searching injava-binaryirdginfo
 
Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary searchnikunjandy
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithmNeoClassical
 
Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityMotaleb Hossen Manik
 
Sorting & Linked Lists
Sorting & Linked ListsSorting & Linked Lists
Sorting & Linked ListsJ.T.A.JONES
 
Searching in c language
Searching in c languageSearching in c language
Searching in c languageCHANDAN KUMAR
 

What's hot (20)

data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
 
Binary search algorithm
Binary search algorithmBinary search algorithm
Binary search algorithm
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
Python set
Python setPython set
Python set
 
Concept of Stream API Java 1.8
Concept of Stream API Java 1.8Concept of Stream API Java 1.8
Concept of Stream API Java 1.8
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
Coin Changing, Binary Search , Linear Search - Algorithm
Coin Changing, Binary Search , Linear Search - AlgorithmCoin Changing, Binary Search , Linear Search - Algorithm
Coin Changing, Binary Search , Linear Search - Algorithm
 
7 searching injava-binary
7 searching injava-binary7 searching injava-binary
7 searching injava-binary
 
Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary search
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 
Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexity
 
Selection sort
Selection sortSelection sort
Selection sort
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting & Linked Lists
Sorting & Linked ListsSorting & Linked Lists
Sorting & Linked Lists
 
List interface
List interfaceList interface
List interface
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
 

Viewers also liked

Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 
Data structure and algorithms in c++
Data structure and algorithms in c++Data structure and algorithms in c++
Data structure and algorithms in c++Karmjeet Chahal
 
Data structures / C++ Program examples
Data structures / C++ Program examplesData structures / C++ Program examples
Data structures / C++ Program examplesKevin III
 
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSCBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSGautham Rajesh
 
Computer science study material
Computer science study materialComputer science study material
Computer science study materialSwarup Kumar Boro
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Deepak Singh
 
Principles of programming languages. Detail notes
Principles of programming languages. Detail notesPrinciples of programming languages. Detail notes
Principles of programming languages. Detail notesVIKAS SINGH BHADOURIA
 
+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II NotesAndrew Raj
 
Lab manual of C++
Lab manual of C++Lab manual of C++
Lab manual of C++thesaqib
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming LanguageAhmad Idrees
 

Viewers also liked (11)

Core programming in c
Core programming in cCore programming in c
Core programming in c
 
Data structure and algorithms in c++
Data structure and algorithms in c++Data structure and algorithms in c++
Data structure and algorithms in c++
 
Array notes
Array notesArray notes
Array notes
 
Data structures / C++ Program examples
Data structures / C++ Program examplesData structures / C++ Program examples
Data structures / C++ Program examples
 
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVSCBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
CBSE XII COMPUTER SCIENCE STUDY MATERIAL BY KVS
 
Computer science study material
Computer science study materialComputer science study material
Computer science study material
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++
 
Principles of programming languages. Detail notes
Principles of programming languages. Detail notesPrinciples of programming languages. Detail notes
Principles of programming languages. Detail notes
 
+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes
 
Lab manual of C++
Lab manual of C++Lab manual of C++
Lab manual of C++
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 

Similar to computer notes - List implementation

linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)swajahatr
 
computer notes - Linked list
computer notes - Linked listcomputer notes - Linked list
computer notes - Linked listecomputernotes
 
Data Structures Design Notes.pdf
Data Structures Design Notes.pdfData Structures Design Notes.pdf
Data Structures Design Notes.pdfAmuthachenthiruK
 
computer notes - Stack
computer notes - Stackcomputer notes - Stack
computer notes - Stackecomputernotes
 
VCE Unit 04vv.pptx
VCE Unit 04vv.pptxVCE Unit 04vv.pptx
VCE Unit 04vv.pptxskilljiolms
 
computer notes - Priority queue
computer notes -  Priority queuecomputer notes -  Priority queue
computer notes - Priority queueecomputernotes
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.pptSeethaDinesh
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingnikshaikh786
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsHoang Nguyen
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsFraboni Ec
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsYoung Alista
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsLuis Goldster
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJames Wong
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsTony Nguyen
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsHarry Potter
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arraysmaamir farooq
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبياناتRafal Edward
 

Similar to computer notes - List implementation (20)

linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
 
computer notes - Linked list
computer notes - Linked listcomputer notes - Linked list
computer notes - Linked list
 
Data Structures Design Notes.pdf
Data Structures Design Notes.pdfData Structures Design Notes.pdf
Data Structures Design Notes.pdf
 
computer notes - Stack
computer notes - Stackcomputer notes - Stack
computer notes - Stack
 
Ch17 Hashing
Ch17 HashingCh17 Hashing
Ch17 Hashing
 
VCE Unit 04vv.pptx
VCE Unit 04vv.pptxVCE Unit 04vv.pptx
VCE Unit 04vv.pptx
 
computer notes - Priority queue
computer notes -  Priority queuecomputer notes -  Priority queue
computer notes - Priority queue
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبيانات
 
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
 

More from ecomputernotes

computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30ecomputernotes
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39ecomputernotes
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11ecomputernotes
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20ecomputernotes
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15ecomputernotes
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraintsecomputernotes
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functionsecomputernotes
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueriesecomputernotes
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objectsecomputernotes
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28ecomputernotes
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19ecomputernotes
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31ecomputernotes
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4ecomputernotes
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13ecomputernotes
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueriesecomputernotes
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functionsecomputernotes
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16ecomputernotes
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22ecomputernotes
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35ecomputernotes
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36ecomputernotes
 

More from ecomputernotes (20)

computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraints
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functions
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueries
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objects
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueries
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functions
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36
 

Recently uploaded

The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxheathfieldcps1
 
In - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxIn - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxAditiChauhan701637
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxraviapr7
 
Benefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationBenefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationMJDuyan
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptxSandy Millin
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational PhilosophyShuvankar Madhu
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfYu Kanazawa / Osaka University
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17Celine George
 
M-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxM-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxDr. Santhosh Kumar. N
 
How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17Celine George
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.EnglishCEIPdeSigeiro
 
How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17Celine George
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?TechSoup
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...CaraSkikne1
 
Practical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxPractical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxKatherine Villaluna
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxraviapr7
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...raviapr7
 

Recently uploaded (20)

Finals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quizFinals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quiz
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptx
 
In - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxIn - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptx
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptx
 
Benefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationBenefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive Education
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational Philosophy
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17
 
M-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxM-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptx
 
How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.
 
How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?
 
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdfPersonal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...
 
Practical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxPractical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptx
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptx
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...
 

computer notes - List implementation

  • 1. ecomputernotes.com Data Structures Lecture No. 02 ___________________________________________________________________ Page 1 of 8 Data Structures Lecture No. 02 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 2 6 8 7 1 current size 1 2 3 4 5 3 5 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 array out-rightly. However for simplification purposes, it is good to use the index from 1. add Method Now we will talk about adding an element to the list. Suppose there is a call to add an element in the list i.e. add(9). As we said earlier that the current position is 3, so by adding the element 9 to the list, the new list will be (2, 6, 8, 9, 7, 1). To add the new element (9) to the list at the current position, at first, we have to make space for this element. For this purpose, we shift every element on the right of 8 (the current position) to one place on the right. Thus after creating the space for new element at position 4, the array can be represented as A 2 6 8 7 1 current size
  • 2. ecomputernotes.com Data Structures Lecture No. 02 ___________________________________________________________________ Page 2 of 8 1 2 3 4 5 3 5 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 2 6 8 9 7 1 current size 1 2 3 4 5 6 4 6 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 let s 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 variables- current 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. Let s see what happens when we want to add 101st 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 discuss the remove method. The remove method removes the element residing at the current position. The removal of the element will be carried out as follows. Suppose there are 6 elements (2, 6, 8, 9, 7, 1) in the list. The current pointer is pointing to the position 5 that has the value 7. We remove the element, making the current position empty. The size of the list will become 5. This is represented in the following figure. A 2 6 8 9 1 current size 1 2 3 4 5 6 5 6 5
  • 3. ecomputernotes.com Data Structures Lecture No. 02 ___________________________________________________________________ Page 3 of 8 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. A 2 6 8 9 1 current size 1 2 3 4 5 5 5 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) // x is found { current = j ; //current points to the position where x found return 1 ; // return true } return 0 ; //return false, x is not found } In the above code, we execute a for loop to traverse the array. The number of execution of this loop is equal to the size of the list. This for loop gets terminated when the value of loop variable (j) increases from the size of the list. However we terminate the loop with the break statement if the element is found at a position. When the control comes out from the loop, we check the value of j. If the value of j is less than the size of the array, it means that the loop was terminated by the break statement. We use the break statement when we find the required element (x) in the list. The execution of break statement shows that the required element was found at the position equal to the value of j. So the program sets the current position to j and
  • 4. ecomputernotes.com Data Structures Lecture No. 02 ___________________________________________________________________ Page 4 of 8 comes out the function by returning 1 (i.e. true). If the value of j is greater than the size of the array, it means that the whole array has traversed and the required element is not found. So we simply return 0 (i.e. false) and come out of the function. Other Methods There are some other methods to implement the list using an array. These methods are very simple, which perform their task just in one step (i.e. in one statement). There is a get() method , used to get the element from the current position in the array. The syntax of this function is of one line and is as under return A[current] ; This statement returns the element to which the current is pointing to (i.e. the current position) in the list A. Another function is update(x). This method is used to change (set) the value at the current position. A value is passed to this method as an argument. It puts that value at the current position. The following statement in this method carries out this process. A [current] = x ; Then there is a method length( ).This method returns the size of the list. The syntax of this method is return size ; You may notice here that we are returning the size of the list and not the size of the array being used internally to implement the list. This size is the number of the elements of the list, stored in the array. The back() method decreases the value of variable current by 1. In other words, it moves the current position one element backward. This is done by writing the statement. current -- ; The -- is a decrement operator in C++ that decreases the value of the operand by one. The above statement can also be written as current = current -1 ; The start() method sets the current position to the first element of the list. We know that the index of the array starts from 0 but we use the index 1 for the starting position. We do not use the index zero. So we set the current position to the first element by writing current = 1 ; Similarly, the end() method sets the current position to the last element of the list i.e. size. So we write current = size ;
  • 5. ecomputernotes.com Data Structures Lecture No. 02 ___________________________________________________________________ Page 5 of 8 Analysis of Array List Now we analyze the implementation of the list while using an array internally. We analyze different methods used for the implementation of the list. We try to see the level upto which these are efficient in terms of CPU s time consumption. Time is the major factor to see the efficiency of a program. Add First of all, we have talked about the add method. When we add an element to the list, every element is moved to the right of the current position to make space for the new element. So, if the current position is the start of the list and we want to add an element in the beginning, we have to shift all the elements of the list to the right one place. This is the worst case of adding an element to the list. Suppose if the size of the list is 10000 or 20000, we have to do the shift operation for all of these 10000 or 20000 elements. Normally, it is done by shifting of elements with the use of a for loop. This operation takes much time of the CPU and thus it is not a good practice to add an element at the beginning of a list. On the other hand, if we add an element at the end of the list, it can be done by carrying out no shift operation . It is the best case of adding an element to the list. However, normally we may have to move half of the elements. The usage of add method is the matter warranting special care at the time of implementation of the list in our program. To provide the interface of the list, we just define these methods. Remove When we remove an element at the current position in the list, its space gets empty. The current pointer remains at the same position. To fill this space, we shift the elements on the right of this empty space one place to the left. If we remove an element from the beginning of the list, then we have to shift the entire remaining elements one place to the left. Suppose there is a large number of elements, say 10000 or 20000, in the list. We remove the first element from the list. Now to fill this space, the remaining elements are shifted (that is a large number). Shifting such a large number of elements is time consuming process. The CPU takes time to execute the for loop that performs this shift operation. Thus to remove an element at the beginning of the list is the worst case of remove method. However it is very easy to remove an element at the end of the list. In average cases of the remove method we expect to shift half of the elements. This average does not mean that in most of the cases, you will have to shift half the elements. It is just the average. We may have to shift all the elements in one operation (if we remove at the beginning) and in the second operation, we have to shift no element (if we remove at the end). Similarly, in certain operations, we have to shift just 10, 15 elements. Find We have discussed that the find method takes an element and traverses the list to find that element. The worst case of the find method is that it has to search the entire list from beginning to end. So, it finds the element at the end of the array or the element is not found. On average the find method searches at most half the list. The other methods get (), length () etc are one-step methods. They carry out their operation in one instruction. There is no need of any loop or other programming
  • 6. ecomputernotes.com Data Structures Lecture No. 02 ___________________________________________________________________ Page 6 of 8 structures to perform the task. The get() method gets the value from the specified position just in one step. Similarly the update() method sets a value at the specific position just in one-step. The length () method returns the value of the size of the list. The other methods back(), start() and end() also perform their tasks only in one step. List using Linked Memory We have seen the implementation of the list with the use of an array. Now we will discuss the implementation of the list while using linked memory. In an array, the memory cells of the array are linked with each other. It means that the memory of the array is contiguous. In an array, it is impossible that one element of the array is located at a memory location while the other element is located somewhere far from it in the memory. It is located in very next location in the memory. It is a property of the array that its elements are placed together with one another in the memory. Moreover, when we have declared the size of the array, it is not possible to increase or decrease it during the execution of the program. If we need more elements to store in the array, there is need of changing its size in the declaration. We have to compile the program again before executing it. Now array will be of the new size. But what happens if we again need to store more elements? We will change the code of our program to change the declaration of the array while recompiling it. Suppose we have used the dynamic memory allocation and created an array of 100 elements with the use of new operator. In case of need of 200 elements, we will release this array and allocate a new array of 200 elements. Before releasing the previous array, it will wise to copy its elements to the new array so that it does not lose any information. Now this new array is in ready for use position. Thus the procedure of creating a new array is not an easy task. To avoid such problems, usually faced by the programmers while using an array, there is need of using linked memory in which the various cells of memory, are not located continuously. In this process, each cell of the memory not only contains the value of the element but also the information where the next element of the list is residing in the memory. It is not necessary that the next element is at the next location in the memory. It may be anywhere in the memory. We have to keep a track of it. Thus, in this way, the first element must explicitly have the information about the location of the second element. Similarly, the second element must know where the third element is located and the third should know the position of the fourth element and so on. Thus, each cell (space) of the list will provide the value of the element along with the information about where the next element is in the memory. This information of the next element is accomplished by holding the memory address of the next element. The memory address can be understood as the index of the array. As in case of an array, we can access an element in the array by its index. Similarly, we can access a memory location by using its address, normally called memory address. 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.
  • 7. ecomputernotes.com Data Structures Lecture No. 02 ___________________________________________________________________ Page 7 of 8 object next A chain of these nodes forms a linked list. Now let s 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 6 8 7 1 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 don t 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.
  • 8. ecomputernotes.com Data Structures Lecture No. 02 ___________________________________________________________________ Page 8 of 8 1051 6 1052 1063 current 1053 1054 2 1055 1051 1056 1057 7 1058 1060 1059 1060 1 1061 0 head 1062 1054 1063 8 1064 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.