Suppose a doubly linked list of integers is given below and p is a reference to the node with value 12 in the list (i.e. p.info=12): (head) 7 11 61 42 3 12 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 = 5;
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: "5 7 11 61 42 3 12 8 2". Đoạn mã dựng nút mới mang 5, đặt prev của nó là null, next của nó trỏ vào head cũ là 7, rồi sửa prev của 7 trỏ về nút mới và cuối cùng dời head sang nút mới, nên 5 thành phần tử đầu danh sách còn dãy cũ giữ nguyên thứ tự phía sau. Biến p trỏ tới nút 12 không hề được dùng nên quanh 12 không có gì thay đổi. Dãy "7 11 61 42 3 12 8 2 5" đặt 5 ở cuối, muốn vậy phải sửa next của nút cuối chứ không phải cho next của nút mới trỏ vào head. Dãy "7 11 61 42 3 12 8 5 2" chèn 5 vào trước phần tử cuối, cần một vòng lặp đi tìm nút liền trước nút cuối mà đoạn mã này không có. Dãy "7 11 61 42 3 12 5 8 2" chèn 5 ngay sau 12, đó mới là chỗ dùng tới biến p. Dãy "7 5 11 61 42 3 12 8 2" đặt 5 vào sau nút đầu, tức phải gán next của head cũ, trong khi ở đây head bị thay bằng chính nút mới.
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