Lập trình Java cơ bản : Collections part 2 doc - Pdf 19

7
Cài đặt Linked List
// Dinh nghia mot node trong linked list
class ListNode
{
int data;
ListNode nextNode;
ListNode(int value)
{
this(value, null);
}
ListNode(int value, ListNode node)
{
data = value;
nextNode = node;
}
int getData() { return data; }
ListNode getNext() { return nextNode; }
}
8
Cài đặt Linked List
// Dinh nghia lop LinkedList
public class LinkedList
{
private ListNode firstNode;
private ListNode lastNode;
public LinkedList()
{
firstNode = lastNode = null;
}
public void insertAtFront(int insertItem)

{
int removeItem = -1;
if ( ! isEmpty() )
{
removeItem = lastNode.data;
if ( firstNode == lastNode )
firstNode = lastNode = null;
else
{
ListNode current = firstNode;
while ( current.nextNode != lastNode )
current = current.nextNode;
lastNode = current;
current.nextNode = null;
}
}
return removeItem;
}
11
Cài đặt Linked List
public boolean isEmpty()
{
return (firstNode == null);
}
public void print()
{
ListNode node = firstNode;
while (node != null)
{
System.out.print(node.data + " ");


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status