1. Trang chủ
  2. » Công Nghệ Thông Tin

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

6 274 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

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) { if ( isEmpty() ) firstNode = lastNode = new ListNode( insertItem ); else firstNode = new ListNode( insertItem, firstNode ); } 9 Cài đặt Linked List public void insertAtBack( int insertItem ) { if ( isEmpty() ) firstNode = lastNode = new ListNode( insertItem ); else lastNode = lastNode.nextNode = new ListNode( insertItem ); } public int removeFromFront() { int removeItem = -1; if ( ! isEmpty() ) { removeItem = firstNode.data; if ( firstNode == lastNode ) firstNode = lastNode = null; else firstNode = firstNode.nextNode; } return removeItem; } 10 Cài đặt Linked List public int removeFromBack() { 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 + " "); node = node.nextNode; } System.out.println("\n"); } } 12 Mô tả insertAtFront 7 11 firstNode 12 new ListNode (a) 7 11 firstNode 12 new ListNode (b) . = node.nextNode; } System.out.println(" "); } } 12 Mô tả insertAtFront 7 11 firstNode 12 new ListNode (a) 7 11 firstNode 12 new ListNode (b)

Ngày đăng: 26/07/2014, 12:21

Xem thêm: Lập trình Java cơ bản : Collections part 2 doc

TỪ KHÓA LIÊN QUAN