Chapter 3 - Linked Lists
Chapter 3 - Linked Lists
Linked Lists
Our First Data Structure
◦ Linked Lists
Singly
Doubly
Circular
This week we will look at linked lists.
◦ Why to use linked lists?
◦ How to use linked lists?
◦ Different Types of linked list.
But first, lets review arrays.
◦ A very useful structure provided by programming
languages
◦ However, arrays have at least 2 limitations;
The size has to be known at compilation.
The data in the array are separated in the computers
memory by the same distance.
◦ These limitations make ‘inserting’ an item inside
the array difficult.
Linked Lists can be used to get around the
limitations of arrays by linking data
independently from where it is stored in the
computers memory.
To create a linked list, each piece of data in
0
A linked structure is a collection of nodes
storing data and links to other nodes.
Nodes can be located anywhere in memory,
Ex1:
picture a classroom of students who are
seated in no particular order. A unique
number identifies each seat (see next
figure). We’ve also included the relative
height of each student, which we’ll use in
the next exercise.
Let’s say that a teacher needs to place students
names in alphabetical order so she can easily find a
name on the list.
Solutions:
Change their seats (chaotic if many)
leave students seated and make a list of seat
A linked list structure in which each node has pointer to both its successor
and its predecessor.
1. looker = 0
2. loop (looker < last AND target not equal
list[looker])
1. looker = looker + 1
3. end loop
4. location = looker
5. if (target equal list[looker])
1. found = true
6. else
1. found = false
7. end if
8. return found
Generally, to find a value in unsorted array, we should look through
elements of an array one by one, until searched value is found. In
case of searched value is absent from array, we go through all
elements. In average, complexity of such an algorithm is proportional to
the length of the array.
Situation changes significantly, when array is sorted. If we know it,
random access capability can be utilized very efficiently to find
searched value quick. Cost of searching algorithm reduces to binary
logarithm of the array length. For reference, log 2(1 000 000) ≈ 20. It
means, that in worst case, algorithm makes 20 steps to find a value in
sorted array of a million elements or to say, that it doesn't present it the
array.
Algorithm
Algorithm is quite simple. It can be done either recursively or iteratively:
1. get the middle element;
2. if the middle element equals to the searched value, the algorithm
stops;
3. otherwise, two cases are possible:
◦ searched value is less, than the middle element. In this case, go
to the step 1 for the part of the array, before middle element.
◦ searched value is greater, than the middle element. In this case,
go to the step 1 for the part of the array, after middle element.
Figure 2-4: Binary search example.
Figure 2-5
1. First = 0
2. Last = end
3. Loop (first <= last)
1. Mid = (first + last)/2
2. If (target > list[mid])
Look in the upper half
First = mid + 1
3. Else if (target < list[mid])
Look in the lower half
1. Last = mid – 1
4. Else
5. Found equal : force exit
1. First = last + 1
6. End if
4. End loop
5. Locn = mid
6. If (target equal list[mid])
1. Found = true
7. Else
1. Found = false
8. End if
9. Return found
1. First = 0, Last =
list.size() - 1
2. while (first <= last)
1. Mid = (first + last)/2
2. If (list[mid] < target])
Look in the upper half
First = mid + 1
3. Else if (target < list[mid])
Look in the lower half
1. Last = mid – 1
4. Else
1. Return mid
5. End if
3. End loop
4. Return -1
/*
* searches for a value in sorted array
* arr is an array to search in
* value is searched value
* left is an index of left boundary
* right is an index of right boundary
* returns position of searched value, if it presents in the array
* or -1, if it is absent
*/
int binarySearch(int arr[ ], int value, int left, int right) {
while (left <= right) {
int middle = (left + right) / 2;
if (arr[middle] == value)
return middle;
else if (arr[middle] > value)
right = middle - 1;
else
left = middle + 1;
}
return -1;