J. E. N. I.
Gambar 2.6a: Sebuah node
Berikut ini merupakan contoh dari non-empty linked list dengan 3 node.
Gambar 3.6b: Non-empty linked list dengan tiga node
Berikut ini bagaimana class node diimplementasikan. Class ini dapat digunakan untuk membuat linked list.
class Node { int data; /* integer data contained in the node */ Node nextNode; /* the next node in the list */
}
class TestNode { public static void main( String args []) { Node emptyList = null; /* create an empty list */ /* head points to 1st node in the list */ Node head = new Node(); /* initialize 1st node in the list */ head. data = 5; head. nextNode = new Node(); head. nextNode. data = 10; /* null marks the end of the list */ head. nextNode. nextNode = null; /* print elements of the list */ Node currNode = head; while( currNode!= null) { System. out. println( currNode. data); currNode = currNode. nextNode;
}
}
}
Pengenalan Pemrograman 2 8