Consider the fun() method in a singly linked list of integers below:
4 phương ánCó giải thích
Integer fun()
{if (isEmpty()) return(null);
Integer x = tail.info;
if (head == tail)
head = tail = null;
else
{Node p1,p; p1=null;p=head;
while(p!=tail) {p1=p;p=p.next;}
p1.next=null; tail=p1;
}
return(x);
}Specify the correct statement about the fun() method in the code above (choose the most suitable one).
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 removes and returns a node at the end of the list". Đọc theo đúng thụt lề: hàm lưu x bằng tail.info trước, nếu danh sách chỉ có một nút thì head và tail cùng về null, ngược lại cho p chạy từ head còn p1 luôn giữ nút liền trước p, vòng while với điều kiện p khác tail dừng đúng lúc p chạm tail nên p1 là nút áp cuối; hai lệnh p1.next bằng null và tail bằng p1 cắt hẳn nút cuối ra khỏi danh sách, rồi hàm trả về giá trị đã lưu. Vì vậy "It returns a node at the end of the list." sai vì nó bỏ mất phần quan trọng là danh sách bị ngắn đi một nút; "It removes and returns a node at the beginning of the list" sai vì head chỉ bị gán lại trong trường hợp danh sách một nút, các trường hợp khác head không hề thay đổi; "It removes and returns a node at the middle of the list" sai vì điều kiện dừng của vòng lặp gắn chặt vào tail nên không bao giờ dừng ở nút giữa.
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