A0 A1 A2 A3
first last
ListNode
A0 A1 A2 A3
first last
// constructors
ListNode (Object theElement, ListNode n){
element = theElement;
next = n;
}
ListNode (Object theElement){
this (theElement, null);
}
ListNode (){
this (null, null);
}
}
a b c d
current
a x b c d
current x
a b
current x
tmp
Fasilkom UI IKI10400 Struktur Data & Algoritma 2010/11 Genap Kuliah 8 11
Langkah-langkah menyisipkan yang lebih efisien
a b
current x
current.next = tmp;
a b
current x
a b c d x
last last
lebih singkat:
last = last.next = new ListNode (x, null);
a x b
current
a b
current
a x b
current
a x b
current
A B C
Header
/**
Make the list logically empty.
*/
void makeEmpty ();
}
/**
Construct the list
*/
public LinkedList ()
{
header = new ListNode (null);
}
/**
Test if the list is logically empty.
@return true if empty, false otherwise.
*/
public boolean isEmpty( )
{
return header.next == null;
}
/**
Construct the list.
As a result of the construction, the current position is
the first item, unless the list is empty, in which case
the current position is the zeroth item.
@param anyList a LinkedList object to which this iterator is
permanently bound.
*/
public LinkedListItr( LinkedList anyList )
{
theList = anyList;
current = theList.isEmpty( ) ? theList.header :
theList.header.next;
}
/**
* Advance the current position to the next node in the list.
* If the current position is null, then do nothing.
* No exceptions are thrown by this routine because in the
* most common use (inside a for loop), this would require the
* programmer to add an unnecessary try/catch block.
*/
public void advance( ){
if( current != null )
current = current.next;
}
/**
* Set the current position to the header node.
*/
public void zeroth( ){
current = theList.header;
}
/**
* Set the current position to the first node in the list.
* This operation is valid for empty lists.
*/
public void first( ){
current = theList.header.next;
}
current = itr;
return true;
}
/**
* Test if the current position references a valid list item.
* @return true if the current position is not null and is
* not referencing the header node.
*/
public boolean isInList( )
{
return current != null && current != theList.header;
}
Fasilkom UI IKI10400 Struktur Data & Algoritma 2010/11 Genap Kuliah 8 34
Catatan: Exceptions
Beberapa method dapat menthrow ItemNotFound
exceptions.
Namun, jangan menggunakan exceptions secara berlebihan
karena setiap exception harus di tangkap (caught) atau di
teruskan (propagate). Sehingga menuntut program harus
selalu membungkusnya dengan blok try/catch
Contoh: method advance tidak men-throw exception,
walaupun sudah berada pada akhir elemen.
Bayangkan bagaimana implementasi method listSize bila
method advance men-throw exception!
class LinkedList {
public void print () {
if (header.next != null) {
header.next.print ();
}
}
}
prev
A
next
head tail
prev
A B C
next
first
a 2 b
?
1 x
current
A 4
B
prev 3
2 X 5
current 1 next
6
newNode
1
a b
2
3
current x
Stack
Tumpukan kertas
Tumpukan tagihan
Tumpukan piring
Waktu O ( 1 ) per operasi stack. (Dengan kata
lain, waktu konstan per operasi, tidak
bergantung berapa banyak item yang
tersimpan dalam stack).
•
•
Push A Push B
top(1)
B
top(0)
A A
top(-1)
public MyArrayStack() …
public boolean isEmpty() …
public void makeEmpty() …
public T top() …
public void pop() …
public T topAndPop() …
public void push(T x) …
d c b a
topOfStack
Fasilkom UI IKI10400 Struktur Data & Algoritma 2012/13 Genap Kuliah 8 69
Stack Implementation: Linked List
public MyLinkedListStack() …
public boolean isEmpty() …
public void makeEmpty() …
public T top() …
public void pop() …
public T topAndPop() …
public void push(T x) …
}
dequeue
enqueue Queue getFront
X X Y X Y Z Y Z
front
enqueue(X) a b c d
enqueue(Y)
enqueue(Z) Front
dequeue()Backdequeue()
X Y Z Y Z Z
a b c d
front back front back front back
Front Back
P Q R T R S
back front
Fasilkom UI IKI10400 Struktur Data & Algoritma 2012/13 Genap Kuliah 8 80
Latihan: Implementasi Array Circular
Bagaimana implementasi dari:
enqueue(); // menambahkan elemen pada queue
dequeue(); // mengambil dan menghapus elemen
isEmpty(); // apakah queue kosong?
isFull(); // apakah queue sudah full?
size(); //memberikan jumlah elemen dalam queue
arrayDoubling(); // memperbesar kapasitas array
Kerjakan berpasangan.
a b c d
front back
Fasilkom UI IKI10400 Struktur Data & Algoritma 2012/13 Genap Kuliah 8 82
Queue Implementation: Linked List
public MyLinkedListQueue() …
public boolean isEmpty() …
public void makeEmpty() …
public T getFront() …
public void dequeue() …
public T getFrontAndDequeue() …
public void enqueue(T x) …
}