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 1 6 4 3 12 8 21 (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 = 13;
Node f = p.prev; //prev is a link to predecessor node
Node q = new Node(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 1 6 4 3 13 12 8 21". Đoạn mã lấy f là nút liền trước p, mà p trỏ vào nút 12 nên f là nút 3. Nút mới mang 13 được gắn prev vào f, next vào p, rồi f.next trỏ sang nó và p.prev cũng trỏ về nó, nên 13 nằm đúng giữa 3 với 12; nhờ có liên kết lùi mà việc chèn trước một nút chỉ tốn vài phép gán, không phải đi tìm nút tiền nhiệm. Dãy "7 1 6 4 3 12 13 8 21" đặt 13 sau 12, muốn vậy phải lấy f là p.next rồi nối vào phía sau. Dãy "7 1 6 4 3 12 8 21 13" đặt 13 ở cuối danh sách, đó là phép thêm vào nút cuối, không liên quan tới p. Dãy "7 13 1 6 4 3 12 8 21" chèn 13 vào sau nút đầu, chỉ đúng nếu f được gán bằng head. Dãy "13 7 1 6 4 3 12 8 21" đưa 13 lên đầu, tức phép chèn ở head kèm việc dời biến head, mà đoạn mã không chạm tới 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 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