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(); }}
Ngọc Bích xanh bản chất thuộc dòng đá đá hoa (jade). Và cẩm thạch là tên gọi chung của ngọc bích. Vì thực chất chúng thuộc dòng đá đa khoáng được hình thành từ chất Silicat dưới dạng dioxy. Ngọc bích...
Chia sẻ Vòng tay ngọc bích xanh là...