Suppose a doubly linked list of integers is given below and p is a reference to the node with value 13 in the list (i.e. p.info=13): (head) 7 11 6 4 32 13 8 2 (tail) What does the list look like after the following java code snippet is run?
5 phương ánCó giải thích
int x = 17;
Node q = new Node(x);
q.prev=null;
q.next = head;
head.prev = q;
head = q;Các phương án trả lời
Chọn một phương ánLời giảiChọn phương án để mở
Đáp án đúng: "17 7 11 6 4 32 13 8 2". Bốn lệnh cuối chỉ chạm vào đầu danh sách: q.prev=null và q.next=head cho nút mới trỏ tới nút 7 đang đứng đầu, head.prev=q nối lại chiều ngược, rồi head=q đưa chính nút 17 lên làm nút đầu; tham chiếu p tới nút 13 không được dùng lần nào nên khúc giữa giữ nguyên. "7 11 6 4 32 13 8 2 17" là kết quả của phép thêm vào cuối, muốn vậy phải đi tới tail và gán tail.next, đoạn mã không làm thế. "7 11 6 4 32 13 8 17 2" đặt 17 vào giữa 8 và 2, cần một tham chiếu tới nút 8 hoặc nút 2 mà mã không có. "7 11 6 4 32 13 17 8 2" là chèn ngay sau nút 13, tức phải gán q.next=p.next rồi p.next=q, trong khi mã lại gán q.next=head. "7 17 11 6 4 32 13 8 2" là chèn sau nút đầu, cần head.next trong hai phép gán thay vì head.
Cùng phần 1
Suppose a doubly linked list of integers is given below and p is a reference to the node with value 9 in the list (i.e. p.info=9):
(head) 7 11 6 4 3 9 8 12 (tail)
What does the list look like after the following java code snippet is run?
int x = 5;
Node f = p.prev; // prev is a link to predecessor node
Node q = new Node(x); // Create new node with value x
q.prev = f; q.next = p;
f.next = q; p.prev = q;Có lời giảiCâu hỏiSelect the statement that is most correct.
Suppose we are considering a singly linked list which has at least 2 nodes. What does the java code snippet below do?
Node f = head;
while(f.next != tail) f = f.next;
f.next = null;
tail = f;Có lời giảiCâu hỏiSuppose a singly linked list of integers is given below and p is a reference to the node with value 3 in the list (i.e. p.info=3):
(head) 7 11 6 14 3 9 8 12 (tail)
What does the list look like after the following java code snippet is run?
int x = 5;
Node f = head;
while(f.next != p) f = f.next;
Node q = new Node(x);
q.next = p;
f.next = q;Có lời giảiCâu hỏiCả 64 câu của chủ đề64 câu
Xuất hiện trong 1 bộ đề
Đi tiếp từ đây