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?
5 phương ánCó giải thích
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á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: "7 11 6 4 3 5 9 8 12". Trong danh sách liên kết đôi, p trỏ nút 9 nên f = p.prev chính là nút 3; nút mới q mang giá trị 5 được gắn q.prev = f và q.next = p, rồi hai liên kết ngược chiều f.next = q cùng p.prev = q khâu q vào đúng khe giữa 3 và 9, nghĩa là chèn ngay trước p. "5 7 11 6 4 3 9 8 12" sai vì đoạn mã không hề chạm tới head; muốn 5 lên đầu thì p phải là nút đầu tiên của danh sách. "7 11 6 4 3 9 8 12 5" sai vì chèn vào cuối cần f là nút cuối và q.next phải là null, không phải q.next = p. "7 11 6 4 3 9 8 5 12" sai vì kết quả đó ứng với p trỏ nút 12 chứ không phải nút 9. "7 11 6 4 3 9 5 8 12" sai vì đó là chèn sau p, muốn vậy phải viết q.prev = p và q.next = p.next.
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ỏ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