Consider the fun() method in a singly linked list below:
4 phương ánCó giải thích
void fun()
{ if (isEmpty()) return;
if (head == tail)
head = tail = null;
else head = head.next;
}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 the first element of the list". Hàm thoát ngay nếu danh sách rỗng; nếu chỉ còn một nút thì head == tail nên cả hai mốc bị đặt null và danh sách thành rỗng; các trường hợp còn lại thì head = head.next dời đầu sang nút thứ hai, nên nút đầu cũ không còn ai trỏ tới và bị loại. Cả hai nhánh đều tác động vào phía head, nghĩa là hàm bỏ phần tử đầu, đúng hành vi dequeue của hàng đợi hoặc pop của ngăn xếp cài bằng danh sách liên kết. Phương án "It removes the last element of the list" sai vì muốn xoá nút cuối phải đi tìm nút kế cuối rồi gán lại tail, mà mã này không có vòng lặp nào. Phương án "It removes the second element of the list" sai vì head.next được dùng làm đầu mới, tức nút thứ hai được giữ lại chứ không bị bỏ. Phương án "It returns the first element of the list" sai vì hàm khai báo void và không có lệnh trả giá trị nào, nó chỉ sửa liên kết.
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