Select the statement that is most correct. Suppose we are considering a doubly linked list which has at least 2 nodes. What does the java code snippet below do?
4 phương ánCó giải thích
Node f = tail.prev; // prev is a link to predecessor node
Node q = new Node(x);
q.prev = f; q.next = tail;
f.next = q; tail.prev = 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: "It inserts new node with value x before the last node.". Mốc duy nhất đoạn mã dùng là f bằng tail.prev, tức nút áp cuối; bốn phép gán q.prev bằng f, q.next bằng tail, f.next bằng q và tail.prev bằng q khâu nút mới vào đúng khe giữa nút áp cuối và nút cuối, giữ nguyên cả head lẫn tail. Vì vậy "It creates new node with value x at the middle of the list." sai vì vị trí chèn luôn cố định ngay trước nút cuối bất kể danh sách dài bao nhiêu, với danh sách hai nút nó rơi ngay sau head; "It inserts new node with value x after the last node." sai vì muốn nối sau nút cuối thì phải gán tail.next bằng q rồi tail bằng q, mà đoạn mã không hề sửa tail; "It replaces the last node with new node with value x." sai vì nút cuối vẫn còn nguyên trong chuỗi liên kết qua q.next bằng tail và tail.prev bằng q, không nút nào bị tháo ra.
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 30 in the list (i.e. p.info=30):
(head) 7 11 6 4 30 9 8 2 (tail)
What does the list look like after the following java code snippet is run?
Node p1,p2; p1 = p.next;
p2 = p1.next;
p.next = p2;
if(p2!=null) p2.prev=p;Có lời giảiCâu hỏiSuppose 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?
int x = 17;
Node q = new Node(x);
q.prev=null;
q.next = head;
head.prev = q;
head = q;Có lời giảiCâu hỏiSelect the statement that is most correct.
Basically, the complexity of getting an element at the middle of a singly linked lists isCó 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