Array List & Linked List
Array List & Linked List
C has shifted one position to the right to make way for B the list has grown
automatically by 1
List Types
• Many ways to implement a list two most
common are :
– Array List
– Linked List
List Types
• An array list is sequence that supports
access to its elements by their indices
• As the name suggests, an array list uses
an array to hold the values. The array list
uses an array based implementation
10 21 34 56
• set
– Sets the value at a specified position (0, 1, 2,. . .) in the list.
Returns the value originally at the specified position. Throws
IndexOutOfBoundsException if the specified position is outside
the range (0 <= index < size()). (i.e. Update command)
• add
– Adds a value to the end of the list. The size of the list will
increase by one.
• delete
– Deletes the first occurrence of a specified value from a list. The
size of the list will decrease by one if the value is found. Returns
true if the value is found or false if the value doesn’t exist.
• contains
– Determines whether a specified value is contained within a list.
Convenience Operations on a List
• indexOf
– Obtains the position (0, 1, 2,. . .) of the first occurrence of a
specified value within a list. Returns -1 if the value is not found.
Equality is determined by calling the value’s equals method.
• isEmpty
– Determines whether a list is empty or not. Returns true if the list
is empty (size() == 0); otherwise, returns false.
• iterator
– Obtains an Iterator over all elements in a list.
• clear
– Deletes all elements from a list. The size of the list is reset to 0.
What is a Class
• A class is a set of individual objects all of
the same kind and have same
characteristics/properties e.g. bicycle
• There may be thousands of other bicycles
in existence, all of the same make and
model.
• Each bicycle was built from the same set
of blueprints and therefore contains the
same components.
What is a Class
• In object-oriented terms, we say that your
bicycle is an instance of the class of
objects known as bicycles.
• A class is the blueprint from which
individual objects are created.
What is an Interface
• Objects define their interaction with the outside
world through the methods that they expose.
• Methods form the object's interface with the
outside world;
• E.g. The buttons on the front of your television
set, for example, are the interface between you
and the electrical wiring on the other side of its
plastic casing.
What is an Interface
• You press the "power" button to turn the
television on and off.
• An interface is a group of related methods
with empty bodies.
• A bicycle's behaviour, if specified as an
interface, might appear as follows:
Interface
interface Bicycle {
// wheel revolutions per minute
void changeCadence(int newValue);
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
}
Java Tutorials
• To implement this interface, the name of
your class would change (to a particular
brand of bicycle, for example, such as
ACMEBicycle), and you'd use the
implements keyword in the class
declaration:
• class ACMEBicycle implements Bicycle
{ // remainder of this class // implemented
as before }
List Interface
package com.wrox.algorithms.lists;
import com.wrox.algorithms.iteration.Iterable;
public interface List extends Iterable {
public void insert(int index, Object value) throws IndexOutOfBoundsException;
public void add(Object value);
public Object delete(int index) throws IndexOutOfBoundsException;
public boolean delete(Object value);
public void clear();
public Object set(int index, Object value throws IndexOutOfBoundsException;
public Object get(int index) throws IndexOutOfBoundsException;
public int indexOf(Object value);
public boolean contains(Object value);
public int size();
public boolean isEmpty();
}
• One area where being able to switch from string to character array and back
again is useful is in cryptography, the science of secret messages and
their applications.
• This field studies ways of performing encryption, which takes a message,
called the plaintext, and converts it into a scrambled message, called the
ciphertext. Likewise, cryptography also studies corresponding ways of
performing decryption, which takes a ciphertext and turns it back into its
original plaintext.
Arrays V Array List
Downsides to using an Array
1. The is that each time you insert a new element, you need to shift
any elements in higher positions one place to the right by
physically copying them.
• Similarly, when deleting an existing element, you need to shift any
objects in higher positions one place to the left to fill the gap left by
the deleted element.
2. Because arrays are fixed in size, anytime you need to increase the
size of the list, you also need to reallocate a new array and copy
the contents over. This clearly affects the performance of insertion
and deletion.
5 3 11
Representation of Linked List in
memory
• The patients in a hospital ward example
shows nodes in a list do not have to
occupy adjacent elements in an array
Representation of Linked List in
memory (example)
• Let LIST be a linked list
• LIST requires two linear arrays INFO and
LINK. Then INFO[K] and LINK[K] contain
the information part and the nextpointer
part of a node K of list. The variable name
of LIST is START which contains the
location of the beginning of the LIST
Representation of Linked List in
memory (example)
• The next diagram shows a representation of
a linked list in memory where each node of
the list contains a single character .
• In other words the list represents a string
• START = 9 so INFO[9] = N 1st character
• LINK[9] = 3 so INFO[3] = 0 2nd character
• LINK[3] =6 so INFO[6] = blank 3rd character
• ………… NO EXIT
Representation of Linked List in
memory (example)
9 INFO. LINK.
1
2 .
3 O 6
4 T 0
5 .
6 . .11
7 X .10
8
9 N .3
10 .I 4
11 E 7
12
Representation of two lists of test scores: ALG and GEOM
TEST. LINK.
1
2 74. 14 Node 2 of ALG
ALG 11
3
4 82 0 Node 4 of ALG
5 .84 12 Node 1 of GEOM
6 .78 .0 .
7 74 8. .Node 3 of GEOM
GEOM 5 8
9 .
10 .
11 88 2 Node 1 of ALG
12 62 7 Node 2 of GEOM
13 74 6
14 93 4 Node 3 of ALG