Lecture09-11_ADTList (3)
Lecture09-11_ADTList (3)
Lecture09-11_ADTList (3)
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
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
null
head current
node node
10
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;
}
// Setters/Getters...
}
13
public LinkedList () {
head = current = null;
}
}
public T retrieve () {
return current.data;
}
public void update (T val) {
current.data = val;
}
20
}
public T retrieve () {
return current.data;
}
public void update (T val) {
current.data = val;
}
21
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
48
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
49
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
50
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
51
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
52
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
53
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
54
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
55
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
56
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
57
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
58
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
59
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
60
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
61
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
62
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
63
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
64
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
65
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
66
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
67
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
68
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
69
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
70
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
71
if (current == head) {
head = head.next; … null
}
else {
Node<T> tmp = head;
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
72
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
73
tmp.next = current.next;
}
if (current.next == null)
current = head;
else
current = current.next;
}
}
74
maxsize = n;
size = 0; …
current = -1; 0 1 2 3 n-1
nodes = (T[]) new Object[n]; size max
0 n
}
76
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
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
nodes[current] = val;
}
nodes[current] = val;
}
current = 0; size
1
max
n
}
Example #1
}
108
current = 0; size
1
max
n
}
Example #1
}
109
current = 0; size
0
max
n
}
Example #1
}
110
current = 0; size
0
max
n
}
Example #1
}
111
current = 0; size
4
max
n
}
Example #2
}
112
current = 0; size
4
max
n
}
Example #2
}
113
current = 0; size
4
max
n
}
Example #2
}
114
current = 0; size
4
max
n
}
Example #2
}
115
current = 0; size
4
max
n
}
Example #2
}
116
current = 0; size
4
max
n
}
Example #2
}
117
current = 0; size
4
max
n
}
Example #2
}
118
current = 0; size
3
max
n
}
Example #2
}
119
current = 0; size
3
max
n
}
Example #3
}
120
current = 0; size
3
max
n
}
Example #3
}
121
current = 0; size
2
max
n
}
Example #3
}
122
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
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
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
System.out.println(al.full());
Output:
System.out.println(length(al));
System.out.println("Hello, World");
}
127
System.out.println(al.full());
Output:
System.out.println(length(al));
System.out.println("Hello, World");
}
128
System.out.println(al.full());
Output:
System.out.println(length(al));
System.out.println("Hello, World");
}
129
System.out.println(al.full());
Output:
System.out.println(length(al));
System.out.println("Hello, World");
}
130
System.out.println(al.full());
Output:
System.out.println(length(al));
System.out.println("Hello, World");
}
131
System.out.println(al.full());
Output:
xyz
false
System.out.println(length(al));
System.out.println("Hello, World");
}
132
System.out.println(al.full());
Output:
xyz
false
System.out.println(length(al));
System.out.println("Hello, World");
}
133
if (!l.empty()) {
l.findFirst(); C
count++;
Output:
} xyz
false
return count;
}
}
134
if (l.empty() == false) {
l.findFirst(); C
count++;
Output:
} xyz
false
return count;
}
}
135
if (l.empty() == false) {
l.findFirst(); C
count++;
Output:
} xyz
false
return count;
}
}
136
if (l.empty() == false) {
l.findFirst(); C
count++;
Output:
} xyz
false
return count;
}
}
137
if (l.empty() == false) {
l.findFirst(); C
count++;
Output:
} xyz
false
return count;
}
}
138
if (l.empty() == false) {
l.findFirst(); C
count++;
Output:
} xyz
false
return count;
}
}
139
if (l.empty() == false) {
l.findFirst(); C
count++;
Output:
} xyz
false
return count;
}
}
140
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
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
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
• 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
Head null
Current
Singly-Linked List
Size
Head null
Current
Singly-Linked List
(with size)
179
Tail
Head null
Current
Singly-Linked List
(with Tail)
180
List: Doubly-Linked
Head
null
null
Doubly-Linked List
Current
181
Tail
null
Head
Current
List with Sentinel Nodes
183
Head
Current Circular List