Gửi bởi
trinhlkq
Yêu cầu của bài như thế này ạ:
1. Tạo class Node (
private int value;
private Node prev;
private Node next;
... các phương thức tương ứng)
2. Tạo một vector với các phần tử là đối tượng Node
- Viết hàm Add
- Viết hàm remove
- Viết hàm find(int value)
- Viết hàm size
- Viết hàm print(Theo cả hai chiều)
Em không hiểu yêu cầu của phần 2, mong mọi người gỡ giúp em,
demo cho em thì càng tốt ạ!
Thanks all!!!
Đề bài là danh sách liên kết thì bạn cứ làm danh sách liên kết thôi
void Add (Node)
{// Thêm phần tử}
void remove (Node)
{//...}
....
Demo một chút code dạo trước cho bạn tham khảo
Ps: Lười chú thích nên bạn chịu khó đọc, ko hiểu có thể hỏi [IMG]images/smilies/smile.png[/IMG]
Mã:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author beautifulsoul84hung */public class Node { private int data; private Node next; public Node (int data, Node next) { this.data = data; this.next = next; } public Node getNext () { return next; } public int getData () { return data; } public void setNext (Node next) { this.next = next; } public void print () { System.out.println (data); }}
Mã:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author beautifulsoul84hung */public class SinglyLinkedList { /** * @param args the command line arguments */ private Node head; private int size; public SinglyLinkedList () { head = null; size = 0; } public void addFirst (int data) { Node tmp = new Node (data, head); head = tmp; size ++; } private Node get (int index) { if (index < 0 || index >= size) return null; Node node = head; for (int i = 0; i < index; i ++) node = node.getNext(); return node; } public void addLast (int data) { if (size == 0) head = new Node (data, null); else { Node tmp = get (size - 1); Node newNode = new Node (data, null); tmp.setNext (newNode); } size ++; } public int removeFirst () { Node tmp; if (size == 0) return 0; else { tmp = head; head = head.getNext(); } size --; return tmp.getData (); } public int removeLast () { int data; Node tmp = null; if (size == 0) return 0; else if (size == 1) data = head.getData (); else { tmp = get (size - 2); data = tmp.getNext().getData(); tmp.setNext(null); } size --; return data; } public void printList () { Node tmp = head; for (int i = 0; i < size; i ++) { tmp.print(); tmp = tmp.getNext(); } System.out.println (); }}
Mã:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author beautifulsoul84hung */public class TestList{ public static void main (String args []) { SinglyLinkedList list = new SinglyLinkedList (); list.addFirst(1); list.addFirst(2); list.addLast(3); list.printList(); list.removeLast (); list.printList(); list.removeFirst (); list.printList(); }}
Thông qua phân tích dữ liệu Google từ 86 quốc gia, mới đây, một công ty tại Anh đã công bố bảng xếp hạng kích tấc "cậu nhỏ" của các nước trên thế giới. Kết quả, hầu hết các nước xếp ở nhóm đầu của...
"Chim" của chàng trai Việt thuộc...