Lecture09-11_ADTList (3)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 183

1

DATA STRUCTURES
ADT List
2

ADT List

Elements Structure

Domain Operations

User of an ADT
must only Specification
know this
Implementer must
Representation know all these.

Implementation
3

ADT List: Specification


Elements: The elements are of generic type <Type> (The elements
are placed in nodes for linked list implementation).

Structure: the elements are linearly arranged. The first element is


called head, there is a element called current.

Domain: the number of elements in the list is bounded therefore the


domain is finite. Type name of elements in the domain: List
4

ADT List: Specification


Operations: We assume all operations operate on a list L.
1. Method findFirst ( )
requires: list L is not empty. input: none
results: first element set as the current element. output: none.
2. Method findNext ( )
requires: list L is not empty. Current is not last. input: none
results: element following the current element is made the current element.
output: none.
3. Method retrieve (Type e)
requires: list L is not empty. input: none
results: current element is copied into e. output: element e.
5

ADT List: Specification


Operations:
4. Method update (Type e).
requires: list L is not empty. input: e.
results: the element e is copied into the current node.
output: none.
5. Method insert (Type e).
requires: list L is not full. input: e.
results: a new node containing element e is created and inserted after the current
element in the list. The new element e is made the current element. If the list is empty e is
also made the head element.
output: none.
6

ADT List: Specification


Operations:
6. Method remove ( )
requires: list L is not empty. input: none
results: the current element is removed from the list. If the resulting list is empty current
is set to NULL. If successor of the deleted element exists it is made the new current
element otherwise first element is made the new current element.
output: none.
7. Method full (boolean flag)
input: none.
returns: if the number of elements in L has reached the maximum number allowed then
flag is set to true otherwise false.
output: flag.
7

ADT List: Specification


Operations:
8. Method empty (boolean flag).
input: none.
results: if the number of elements in L is zero, then flag is set to true
otherwise false.
Output: flag.
9. Method last (boolean flag).
input: none. requires: L is not empty.
Results: if the last element is the current element then flag is set to true
otherwise false.
Output: flag
8

Representation/Implementation
• Programmers have to deal with the questions:
• How to represent lists? Storage structure affects the efficiency of the
operations.
• How to implement the operations? Algorithm chosen must be efficient.
• Lists can represented as
• Linked List
• Array based List
9

List (Linked List)

null

head current
node node
10

List (Array Based)

0 1 2 3 4 5 6 n-2 n-1

size maxsize
current
List Interface
public interface List<T>{
public void findFirst( );
public void findNext( );
public T retrieve( );
public void update(T e);
public void insert(T e);
public void remove( );
public boolean full( );
public boolean empty( );
public boolean last( );
}
ADT List (Linked List): Element
public class Node<T> {
public T data;
public Node<T> next;

public Node () {
data = null;
next = null;
}

public Node (T val) {


data = val;
next = null;
}

// Setters/Getters...
}
13

ADT List (Linked List): Representation


public class LinkedList<T> implements List<T>{
private Node<T> head;
private Node<T> current;

public LinkedList () {
head = current = null;
}

public boolean empty () {


return head == null;
}

public boolean last () {


return current.next == null;
}
14

ADT List (Linked List): Representation


public class LinkedList<T> implements List<T>{
private Node<T> head; H H
private Node<T> current;

public LinkedList () { null …


head = current = null;
true false
}

public boolean empty () {


return head == null;
}

public boolean last () {


return current.next == null;
}
15

ADT List (Linked List): Representation


public class LinkedList<T> implements List<T>{ H C
private Node<T> head;
private Node<T> current;
… …

public LinkedList () { null


false
head = current = null;
}
H C

public boolean empty () {


return head == null; … …
}
null
true
public boolean last () {
return current.next == null;
}
16

ADT List (Linked List): Implementation


public boolean full () {
return false;
}
public void findfirst () {
current = head;
}
public void findnext () {
current = current.next;
}
public T retrieve () {
return current.data;
}
public void update (T val) {
current.data = val;
}
17

ADT List (Linked List): Implementation


public boolean full () { H C
return false;
}
… …
public void findfirst () {
current = head; null
}
public void findnext () {
current = current.next; H C
}
public T retrieve () { … …
return current.data;
null
}
public void update (T val) {
current.data = val;
}
18

ADT List (Linked List): Implementation


H C
public boolean full () {
return false;
} … …
public void findfirst () {
null
current = head;
}
public void findnext () { H C
current = current.next;
}
… …
public T retrieve () {
return current.data; null
}
public void update (T val) {
current.data = val;
}
19

ADT List (Linked List): Implementation


public boolean full () {
return false;
} H C
public void findfirst () {
current = head;
… …
}
public void findnext () { null
current = current.next; current.data

}
public T retrieve () {
return current.data;
}
public void update (T val) {
current.data = val;
}
20

ADT List (Linked List): Implementation


public boolean full () {
return false;
} H C
public void findfirst () {
current = head;
… …
}
public void findnext () { null
current = current.next; val

}
public T retrieve () {
return current.data;
}
public void update (T val) {
current.data = val;
}
21

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
}
}
22

ADT List (Linked List): Implementation


Example #1 H C
public void insert (T val) {
Node<T> tmp;
if (empty()) { null
current = head = new Node<T> (val);
}
else {
tmp = current.next; H C
current.next = new Node<T> (val);
current = current.next;
current.next = tmp; null
}
}
23

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
} HC
}
Example #2 null
24

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
HC tmp
}
}
Example #2 null
25

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
HC tmp
}
}
Example #2 null
26

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
} H C tmp
}
Example #2 null
27

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
} H C tmp
}
Example #2 null
28

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
} H C
}
Example #2 null
29

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
} HC
}
Example #3 …
30

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
HC tmp
}
}
Example #3 …
31

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
HC tmp
}
}
Example #3 …
32

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
} H C tmp
}
Example #3 …
33

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
} H C tmp
}
Example #3 …
34

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
} H C
}
Example #3 …
35

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
} H C
}
Example #4 … … null
36

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
H C tmp
}
}
Example #4 … … null
37

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
H C tmp
}
}
Example #4 … … null
38

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
} H C tmp
}
Example #4 … … null
39

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
} H C tmp
}
Example #4 … … null
40

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
} H C
}
Example #4 … … null
41

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
} H C
}
Example #5 … null
42

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
H C tmp
}
}
Example #5 … null
43

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
H C tmp
}
}
Example #5 … null
44

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
} H C tmp
}
Example #5 … null
45

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
} H C tmp
}
Example #5 … null
46

ADT List (Linked List): Implementation


public void insert (T val) {
Node<T> tmp;
if (empty()) {
current = head = new Node<T> (val);
}
else {
tmp = current.next;
current.next = new Node<T> (val);
current = current.next;
current.next = tmp;
} H C
}
Example #5 … null
47

ADT List (Linked List): Implementation


public void remove () {
if (current == head) {
head = head.next;
}
else {
Node<T> tmp = head;

while (tmp.next != current)


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
48

ADT List (Linked List): Implementation


public void remove () { HC
if (current == head) {
head = head.next; …
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #1


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
49

ADT List (Linked List): Implementation


public void remove () { C H
if (current == head) {
head = head.next; …
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #1


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
50

ADT List (Linked List): Implementation


public void remove () { HC
if (current == head) {
head = head.next; …
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #1


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
51

ADT List (Linked List): Implementation


public void remove () { HC
if (current == head) {
head = head.next; …
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #1


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
52

ADT List (Linked List): Implementation


public void remove () { HC
if (current == head) {
head = head.next; …
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #1


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
53

ADT List (Linked List): Implementation


public void remove () { HC
if (current == head) {
head = head.next; null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #2


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
54

ADT List (Linked List): Implementation


public void remove () { C H
if (current == head) {
head = head.next; null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #2


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
55

ADT List (Linked List): Implementation


public void remove () { HC
if (current == head) {
head = head.next; null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #2


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
56

ADT List (Linked List): Implementation


public void remove () { HC
if (current == head) {
head = head.next; null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #2


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
57

ADT List (Linked List): Implementation


public void remove () { HC
if (current == head) {
head = head.next; null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #2


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
58

ADT List (Linked List): Implementation


public void remove () { H C
if (current == head) {
head = head.next; … … null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #3


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
59

ADT List (Linked List): Implementation


public void remove () { H tmp C
if (current == head) {
head = head.next; … … null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #3


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
60

ADT List (Linked List): Implementation


public void remove () { H tmp C
if (current == head) {
head = head.next; … … null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #3


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
61

ADT List (Linked List): Implementation


public void remove () { H tmp C
if (current == head) {
head = head.next; … … null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #3


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
62

ADT List (Linked List): Implementation


public void remove () { H tmp C
if (current == head) {
head = head.next; … … null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #3


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
63

ADT List (Linked List): Implementation


public void remove () { H tmp C
if (current == head) {
head = head.next; … … null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #3


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
64

ADT List (Linked List): Implementation


public void remove () { H C
if (current == head) {
head = head.next; … … null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #3


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
65

ADT List (Linked List): Implementation


public void remove () { H C
if (current == head) {
head = head.next; … … null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #3


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
66

ADT List (Linked List): Implementation


public void remove () { H C
if (current == head) {
head = head.next; … null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #4


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
67

ADT List (Linked List): Implementation


public void remove () { H tmp C
if (current == head) {
head = head.next; … null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #4


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
68

ADT List (Linked List): Implementation


public void remove () { H tmp C
if (current == head) {
head = head.next; … null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #4


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
69

ADT List (Linked List): Implementation


public void remove () { H tmp C
if (current == head) {
head = head.next; … null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #4


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
70

ADT List (Linked List): Implementation


public void remove () { H tmp C
if (current == head) {
head = head.next; … null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #4


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
71

ADT List (Linked List): Implementation


public void remove () { HC tmp

if (current == head) {
head = head.next; … null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #4


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
72

ADT List (Linked List): Implementation


public void remove () { HC
if (current == head) {
head = head.next; … null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #4


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
73

ADT List (Linked List): Implementation


public void remove () { HC
if (current == head) {
head = head.next; … null
}
else {
Node<T> tmp = head;

while (tmp.next != current) Example #4


tmp = tmp.next;

tmp.next = current.next;
}

if (current.next == null)
current = head;
else
current = current.next;
}
}
74

ADT List (Array): Representation


public class ArrayList<T> implements List<T>
{
private int maxsize;
private int size;
private int current;
private T[] nodes;

/** Creates a new instance of ArrayList */


public ArrayList(int n) {
maxsize = n;
size = 0;
current = -1;
nodes = (T[]) new Object[n];
}
75

ADT List (Array): Representation


public class ArrayList<T> implements List<T>
{
private int maxsize;
private int size;
private int current;
private T[] nodes;

/** Creates a new instance of ArrayList */


public ArrayList(int n) { C nodes

maxsize = n;
size = 0; …
current = -1; 0 1 2 3 n-1
nodes = (T[]) new Object[n]; size max
0 n
}
76

ADT List (Array): Implementation


public boolean full () {
return size == maxsize;
}
public boolean empty () {
return size == 0;
}
public boolean last () {
return current == size - 1;
}
public void findFirst () {
current = 0;
}
public void findNext () {
current++;
}
77

ADT List (Array): Implementation


C
public boolean full () {
return size == maxsize; …
} 0 1 2 3 n-1

public boolean empty () { size max


n n

return size == 0; true


}
C
public boolean last () {
return current == size - 1; …
}
0 1 2 3 n-1
public void findFirst () { size max
3 n
current = 0;
false
}
public void findNext () {
current++;
}
78

ADT List (Array): Implementation


C
public boolean full () {
return size == maxsize; …
} 0 1 2 3 n-1

public boolean empty () { size


0
max
n

return size == 0; true


}
C
public boolean last () {
return current == size - 1; …
}
0 1 2 3 n-1
public void findFirst () { size max
3 n
current = 0;
false
}
public void findNext () {
current++;
}
79

ADT List (Array): Implementation


C
public boolean full () {
return size == maxsize; …
} 0 1 2 3 n-1

public boolean empty () { size


3
max
n

return size == 0; true


}
C
public boolean last () {
return current == size - 1; …
}
0 1 2 3 n-1
public void findFirst () { size max
3 n
current = 0;
false
}
public void findNext () {
current++;
}
80

ADT List (Array): Implementation


C
public boolean full () {
return size == maxsize; …
} 0 1 2 3 n-1

public boolean empty () { size


3
max
n

return size == 0;
}
C
public boolean last () {
return current == size - 1; …
}
0 1 2 3 n-1
public void findFirst () { size max
current = 0; 3 n

}
public void findNext () {
current++;
}
81

ADT List (Array): Implementation


C
public boolean full () {
return size == maxsize; …
} 0 1 2 3 n-1

public boolean empty () { size


3
max
n

return size == 0;
}
C
public boolean last () {
return current == size - 1; …
}
0 1 2 3 n-1
public void findFirst () { size max
current = 0; 3 n

}
public void findNext () {
current++;
}
82

ADT List (Array): Implementation


public T retrieve () {
return nodes[current];
}

public void update (T val) {


nodes[current] = val;
}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
83

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
3

nodes[current] = val;
}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
84

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; …
} 0 1 2 3 n-1

val size max


n
public void update (T val) {
3

nodes[current] = val;
}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
85

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
0

nodes[current] = val; Example #1


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
86

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
0

nodes[current] = val; Example #1


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
87

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
0

nodes[current] = val; Example #1


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
88

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; …
} 0 1 2 3 n-1
size val max
n
public void update (T val) {
0 5

nodes[current] = val; Example #1


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
89

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; 5 …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
0

nodes[current] = val; Example #1


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
90

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; 5 …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
1

nodes[current] = val; Example #1


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
91

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; 5 …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
1

nodes[current] = val; Example #2


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
92

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; 5 …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
1

nodes[current] = val; Example #2


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
93

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; 5 …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
1

nodes[current] = val; Example #2


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
94

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; 5 …
} 0 1 2 3 n-1
size val max
n
public void update (T val) {
1 8

nodes[current] = val; Example #2


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
95

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; 5 8 …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
1

nodes[current] = val; Example #2


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
96

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; 5 8 …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
2

nodes[current] = val; Example #2


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
97

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; 5 8 7 …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
3

nodes[current] = val; Example #3


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
98

ADT List (Array): Implementation


C i i+1
public T retrieve () {
return nodes[current]; 5 8 7 …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
3

nodes[current] = val; Example #3


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
99

ADT List (Array): Implementation


C i i+1
public T retrieve () {
return nodes[current]; 5 8 7 7 …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
3

nodes[current] = val; Example #3


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
100

ADT List (Array): Implementation


C i i+1
public T retrieve () {
return nodes[current]; 5 8 7 7 …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
3

nodes[current] = val; Example #3


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
101

ADT List (Array): Implementation


C i i+1
public T retrieve () {
return nodes[current]; 5 8 8 7 …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
3

nodes[current] = val; Example #3


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
102

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; 5 8 8 7 …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
3

nodes[current] = val; Example #3


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
103

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; 5 8 8 7 …
} 0 1 2 3 n-1
val size max
6 n
public void update (T val) {
3

nodes[current] = val; Example #3


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
104

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; 5 6 8 7 …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
3

nodes[current] = val; Example #3


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
105

ADT List (Array): Implementation


C
public T retrieve () {
return nodes[current]; 5 6 8 7 …
} 0 1 2 3 n-1
size max
n
public void update (T val) {
3

nodes[current] = val; Example #3


}

public void insert (T val) {


for (int i = size-1; i > current; --i) {
nodes[i+1] = nodes[i];
}
current++;
nodes[current] = val;
size++;
}
106

ADT List (Array): Implementation


public void remove () {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--;
if (size == 0)
current = -1;
else if (current == size)
current = 0;
}
}
107

ADT List (Array): Implementation


public void remove () {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--;
C
if (size == 0)
current = -1; 5 …
else if (current == size) 0 1 2 3 n-1

current = 0; size
1
max
n

}
Example #1
}
108

ADT List (Array): Implementation


public void remove () {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--;
C
if (size == 0)
current = -1; 5 …
else if (current == size) 0 1 2 3 n-1

current = 0; size
1
max
n

}
Example #1
}
109

ADT List (Array): Implementation


public void remove () {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--;
C
if (size == 0)
current = -1; 5 …
else if (current == size) 0 1 2 3 n-1

current = 0; size
0
max
n

}
Example #1
}
110

ADT List (Array): Implementation


public void remove () {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--;
C
if (size == 0)
current = -1; 5 …
else if (current == size) 0 1 2 3 n-1

current = 0; size
0
max
n

}
Example #1
}
111

ADT List (Array): Implementation


public void remove () {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--;
C
if (size == 0)
current = -1; 5 6 8 7 …
else if (current == size) 0 1 2 3 n-1

current = 0; size
4
max
n

}
Example #2
}
112

ADT List (Array): Implementation


public void remove () {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--; i-1 i
C
if (size == 0)
current = -1; 5 6 8 7 …
else if (current == size) 0 1 2 3 n-1

current = 0; size
4
max
n

}
Example #2
}
113

ADT List (Array): Implementation


public void remove () {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--; i-1 i
C
if (size == 0)
current = -1; 6 6 8 7 …
else if (current == size) 0 1 2 3 n-1

current = 0; size
4
max
n

}
Example #2
}
114

ADT List (Array): Implementation


public void remove () {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--; i-1 i
C
if (size == 0)
current = -1; 6 6 8 7 …
else if (current == size) 0 1 2 3 n-1

current = 0; size
4
max
n

}
Example #2
}
115

ADT List (Array): Implementation


public void remove () {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--; i-1 i
C
if (size == 0)
current = -1; 6 8 8 7 …
else if (current == size) 0 1 2 3 n-1

current = 0; size
4
max
n

}
Example #2
}
116

ADT List (Array): Implementation


public void remove () {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--; i-1 i
C
if (size == 0)
current = -1; 6 8 8 7 …
else if (current == size) 0 1 2 3 n-1

current = 0; size
4
max
n

}
Example #2
}
117

ADT List (Array): Implementation


public void remove () {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--; i-1 i
C
if (size == 0)
current = -1; 6 8 7 7 …
else if (current == size) 0 1 2 3 n-1

current = 0; size
4
max
n

}
Example #2
}
118

ADT List (Array): Implementation


public void remove () {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--;
C
if (size == 0)
current = -1; 6 8 7 7 …
else if (current == size) 0 1 2 3 n-1

current = 0; size
3
max
n

}
Example #2
}
119

ADT List (Array): Implementation


public void remove () {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--;
C
if (size == 0)
current = -1; 6 8 7 …
else if (current == size) 0 1 2 3 n-1

current = 0; size
3
max
n

}
Example #3
}
120

ADT List (Array): Implementation


public void remove () {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--;
C
if (size == 0)
current = -1; 6 8 7 …
else if (current == size) 0 1 2 3 n-1

current = 0; size
3
max
n

}
Example #3
}
121

ADT List (Array): Implementation


public void remove () {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--;
C
if (size == 0)
current = -1; 6 8 7 …
else if (current == size) 0 1 2 3 n-1

current = 0; size
2
max
n

}
Example #3
}
122

ADT List (Array): Implementation


public void remove () {
for (int i = current + 1; i < size; i++) {
nodes[i-1] = nodes[i];
}
size--;
C
if (size == 0)
current = -1; 6 8 7 …
else if (current == size) 0 1 2 3 n-1

current = 0; size
2
max
n

}
Example #3
}
123

ADT List
• How to use the ADT List?
• The implementation of ADT is available to you as a Java class ready for use.

Example: You are required to implement a static method to get the length of a
list.
124

Using ADT List


public class TestArrayList {
public static void main ( String[] args ) {
ArrayList<String> al = new ArrayList<String>(10);
String s1= "xyz", s2 = "abc";

al.insert(s1);
al.insert(s2);
al.findFirst();

System.out.println(al.retrieve());
System.out.println(al.full());

System.out.println(length(al));
System.out.println("Hello, World");
}
125

Using ADT List


public static <T> int length(ArrayList<T> l) {
int count = 0;

if (l.empty() == false) {
l.findFirst(); A static method
while (l.last() == false) { to find the length
count++; of a list.
l.findNext(); Note: it has been
} implemented using
count++; the methods of ADT
} List

return count;
}
}
126

Using ADT List


public class TestArrayList {
public static void main ( String[] args ) {
ArrayList<String> al = new ArrayList<String>(10);
String s1= "xyz", s2 = "abc";
C
al.insert(s1);
al.insert(s2); …
al.findFirst();
0 1 2 3 9
size max
System.out.println(al.retrieve()); 0 10

System.out.println(al.full());
Output:

System.out.println(length(al));
System.out.println("Hello, World");
}
127

Using ADT List


public class TestArrayList {
public static void main ( String[] args ) { a1
ArrayList<String> al = new ArrayList<String>(10);
String s1= "xyz", s2 = "abc";
C
al.insert(s1);
al.insert(s2); …
al.findFirst();
0 1 2 3 9
size max
System.out.println(al.retrieve()); 0 10

System.out.println(al.full());
Output:

System.out.println(length(al));
System.out.println("Hello, World");
}
128

Using ADT List


public class TestArrayList {
public static void main ( String[] args ) { a1
ArrayList<String> al = new ArrayList<String>(10);
String s1= "xyz", s2 = "abc";
C
al.insert(s1);
al.insert(s2); xyz …
al.findFirst();
0 1 2 3 9
size max
System.out.println(al.retrieve()); 1 10

System.out.println(al.full());
Output:

System.out.println(length(al));
System.out.println("Hello, World");
}
129

Using ADT List


public class TestArrayList {
public static void main ( String[] args ) { a1
ArrayList<String> al = new ArrayList<String>(10);
String s1= "xyz", s2 = "abc";
C
al.insert(s1);
al.insert(s2); xyz abc …
al.findFirst();
0 1 2 3 9
size max
System.out.println(al.retrieve()); 2 10

System.out.println(al.full());
Output:

System.out.println(length(al));
System.out.println("Hello, World");
}
130

Using ADT List


public class TestArrayList {
public static void main ( String[] args ) { a1
ArrayList<String> al = new ArrayList<String>(10);
String s1= "xyz", s2 = "abc";
C
al.insert(s1);
al.insert(s2); xyz abc …
al.findFirst();
0 1 2 3 9
size max
System.out.println(al.retrieve()); 2 10

System.out.println(al.full());
Output:

System.out.println(length(al));
System.out.println("Hello, World");
}
131

Using ADT List


public class TestArrayList {
public static void main ( String[] args ) { a1
ArrayList<String> al = new ArrayList<String>(10);
String s1= "xyz", s2 = "abc";
C
al.insert(s1);
al.insert(s2); xyz abc …
al.findFirst();
0 1 2 3 9
size max
System.out.println(al.retrieve()); 2 10

System.out.println(al.full());
Output:
xyz
false
System.out.println(length(al));
System.out.println("Hello, World");
}
132

Using ADT List


public class TestArrayList {
public static void main ( String[] args ) { a1
ArrayList<String> al = new ArrayList<String>(10);
String s1= "xyz", s2 = "abc";
C
al.insert(s1);
al.insert(s2); xyz abc …
al.findFirst();
0 1 2 3 9
size max
System.out.println(al.retrieve()); 2 10

System.out.println(al.full());
Output:
xyz
false
System.out.println(length(al));
System.out.println("Hello, World");
}
133

Using ADT List


public static <T> int length(ArrayList<T> l) {
a1
int count = 0;

if (!l.empty()) {
l.findFirst(); C

while (l.last() == false) {


xyz abc …
count++;
0 1 2 3 9
l.findNext();
size max
} 2 10

count++;
Output:
} xyz
false

return count;
}
}
134

Using ADT List


public static <T> int length(ArrayList<T> l) {
a1
int count = 0; count = 0
l

if (l.empty() == false) {
l.findFirst(); C

while (l.last() == false) {


xyz abc …
count++;
0 1 2 3 9
l.findNext();
size max
} 2 10

count++;
Output:
} xyz
false

return count;
}
}
135

Using ADT List


public static <T> int length(ArrayList<T> l) {
a1
int count = 0; count = 0
l

if (l.empty() == false) {
l.findFirst(); C

while (l.last() == false) {


xyz abc …
count++;
0 1 2 3 9
l.findNext();
size max
} 2 10

count++;
Output:
} xyz
false

return count;
}
}
136

Using ADT List


public static <T> int length(ArrayList<T> l) {
a1
int count = 0; count = 1
l

if (l.empty() == false) {
l.findFirst(); C

while (l.last() == false) {


xyz abc …
count++;
0 1 2 3 9
l.findNext();
size max
} 2 10

count++;
Output:
} xyz
false

return count;
}
}
137

Using ADT List


public static<T> int length(ArrayList<T> l) {
a1
int count = 0; count = 1
l

if (l.empty() == false) {
l.findFirst(); C

while (l.last() == false) {


xyz abc …
count++;
0 1 2 3 9
l.findNext();
size max
} 2 10

count++;
Output:
} xyz
false

return count;
}
}
138

Using ADT List


public static <T> int length(ArrayList<T> l) {
a1
int count = 0; count = 2
l

if (l.empty() == false) {
l.findFirst(); C

while (l.last() == false) {


xyz abc …
count++;
0 1 2 3 9
l.findNext();
size max
} 2 10

count++;
Output:
} xyz
false

return count;
}
}
139

Using ADT List


public static <T> int length(ArrayList<T> l) {
a1
int count = 0; count = 2
l

if (l.empty() == false) {
l.findFirst(); C

while (l.last() == false) {


xyz abc …
count++;
0 1 2 3 9
l.findNext();
size max
} 2 10

count++;
Output:
} xyz
false

return count;
}
}
140

Using ADT List


public class TestArrayList {
public static void main ( String[] args ) { a1
ArrayList<String> al = new ArrayList<String>(10);
String s1= "xyz", s2 = "abc";
C
al.insert(s1);
al.insert(s2); xyz abc …
al.findFirst();
0 1 2 3 9
size max
System.out.println(al.retrieve()); 2 10

System.out.println(al.full());
Output:
xyz
false
System.out.println(length(al)); 2
System.out.println("Hello, World"); Hello, World

}
141

ADT List

What are the changes that need to be made to use List (Linked List
implementation) instead of List (Array List implementation)?
142

Using ADT List


public class TestLinkedList {
public static void main ( String[] args ) {
ArrayList<String> al = new ArrayList<String>(10);
LinkedList<String> al = new LinkedList<String>();
String s1= "xyz", s2 = "abc";

al.insert(s1);
Only this line!
al.insert(s2);
al.findFirst();

System.out.println(al.retrieve());
System.out.println(al.full());

System.out.println(length(al));
System.out.println("Hello, World");
}
143

ADT List

You are required to implement a static method to search for an element e in a


list L, and if e is present make current pointer point to e. Use operations of
ADT List.
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) {
if(l.empty() == false) {
l.findFirst();
while(l.last() == false) {
if(l.retrieve().equals(key))
return true;
l.findNext();
}
if(l.retrieve().equals(key))
return true;
}

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #1
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) c 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static<T> boolean find(ArrayList<T> l, T key) { Example #1
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) c 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #1
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) c 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #1
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) c 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #1
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) c 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #1
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) c 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #1
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) c 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #1
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) c 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #2
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) d 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #2
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) d 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #2
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) d 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #2
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) d 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #2
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) d 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #2
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) d 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #2
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) d 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #2
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) d 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #2
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) d 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #2
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) d 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b c d …
} 0 1 2 3 n-1
size max
4 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #3
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) x 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b …
} 0 1 2 3 n-1
size max
2 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #3
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) x 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b …
} 0 1 2 3 n-1
size max
2 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #3
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) x 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b …
} 0 1 2 3 n-1
size max
2 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #3
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) x 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b …
} 0 1 2 3 n-1
size max
2 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #3
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) x 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b …
} 0 1 2 3 n-1
size max
2 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #3
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) x 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; a b …
} 0 1 2 3 n-1
size max
2 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #4
if(l.empty() == false) {
l.Findfirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) a 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; …
} 0 1 2 3 n-1
size max
0 n

return false;
}
...
Search Item (Static Method)
...
public static <T> boolean find(ArrayList<T> l, T key) { Example #4
if(l.empty() == false) {
l.findFirst(); key
while(l.last() == false) {
if(l.retrieve().equals(key)) a 1
return true;
l.findNext();
} C
if(l.retrieve().equals(key))
return true; …
} 0 1 2 3 n-1
size max
0 n

return false;
}
...
171

ADT List

You are required to implement the same search method but this time as a
member method of the ADT List (Linked List implementation).
Search Item (Member Method) #1
...
public boolean find(T key) {
Node<T> tmp = current;
current = head;
while(current != null) {
if(current.data.equals(key))
return true;
current = current.next;
}
current = tmp;
return false;
}
...
Search Item (Member Method) #2
...
public boolean find(T key) {
Node<T> tmp = head;
while(tmp != null) {
if(tmp.data.equals(key)) {
current = tmp;
return true;
}
tmp = tmp.next;
}
return false;
}
...
Search Item (Member Method) #3
...
public boolean find(T key) {
if(empty() == false) {
findFirst();
while(last() == false) {
if(retrieve().equals(key))
return true;
findNext();
}
if(retrieve().equals(key))
return true;
}
return false;
}
...
175

Comparison: Linked & Array Based Lists

Comparison on the basis:


• worst case time complexity operations
• Linked List: insert– O(1); remove – O(n).
• Array List: insert – O(n); remove – O(n).
• All other operations have time complexity O(1).
• Best case time complexities?
176

Comparison: Linked & Array Based Lists

• Linked List.
• Advantages: No need to know the size in advance. Fast
“Insert” – O(1).
• Disadvantage: a pointer at each node (more memory needed).
For traversal, pointer hoping is required.
• Array Based List.
• Advantages: No pointers to be stored (less memory). For
traversal, no pointer hoping is required (array faster in
traversal).
• Disadvantage: list size must be known in advance. Slow
“Insert” – O(n).
177

List: Other Implementations


• Singly-Linked List.
• Design Variations: (i) Count of elements may be kept i.e. size. Why? (ii) pointer to tail may
be kept i.e. last. Why?
• Doubly-Linked List.
• each node has two pointers: next node and previous node.
• Advantages: it is efficient to go from a node to its previous node or move back-to-front in
the list
• operations insert – O(1);
• remove – O(1).

(We will cover this topic in detail later)


178

List: Singly Linked

Head null
Current
Singly-Linked List

Size
Head null
Current
Singly-Linked List
(with size)
179

List: Singly Linked with Tail

Tail

Head null
Current
Singly-Linked List
(with Tail)
180

List: Doubly-Linked

Head
null
null

Doubly-Linked List
Current
181

List: Other Implementations


• List with Sentinel Nodes.
• Has special header & trailer nodes that do not store data
• All nodes that store data have previous & next nodes – so no special cases for insert &
remove.
• Advantage– simplifies code
• Circular List.
• tail pointer made to point to the head  tail has next node. Advantage: simpler code.
182

List: with Sentinel Nodes

Tail
null
Head
Current
List with Sentinel Nodes
183

List: Circular List

Head
Current Circular List

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy