Suppose we are implementing a stack of Integers using a singly linked list where the head of the list is treated as the top of the stack. Specify the correct implementation of push() method of the stack. (Choose the most suitable one)
4 phương ánCó giải thích
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: "
void push(Integer x) {
Node p = new Node(x);
p.next = head;
head=p;
}". Ngăn xếp cài bằng danh sách liên kết đơn với đỉnh là đầu danh sách cần đúng hai việc theo đúng thứ tự: cho nút mới trỏ tới nút đang đứng đầu, rồi dời head về nút mới; làm ngược thứ tự thì mất phần còn lại của danh sách. "
void push(Integer x) {
Node p = new Node(x);
p.next = head;
head=p.next;
}" gán head bằng p.next, mà p.next vừa được đặt bằng head, nên head không đổi và nút p bị bỏ rơi, giá trị mới không vào được ngăn xếp. "
void push(Integer x) {
Node p = new Node(x);
p.next = head;
}" nối nút mới vào danh sách nhưng không bao giờ dời head, nên không ai chạm tới p được và kích thước ngăn xếp đứng yên. "
void push(Integer x) {
Node p = new Node(x);
p.next = null;
head=p;
}" cho head trỏ tới nút mới nhưng cắt p.next thành null, tức bỏ sạch mọi phần tử đã đẩy vào trước đó, ngăn xếp chỉ còn một phần tử.
Cùng phần 2
Select the statement that is most correct.
Which of the following applications may not use a stack?Có lời giảiCâu hỏiConsider the following pseudocode:
declare a stack of characters
while(there are more characters in the word to read)
{read a character
if a character is '*' then
pop the stack
else
push the character into the stack
}
while(the stack is not empty)
pop and write the poped character to the screen
What is written to the screen for the input "Good**Morn**in***g" ?Có lời giảiCâu hỏiConsider the following pseudocode:
declare a stack of characters
while(there are more characters in the word to read)
{read a character
if a character is '*' then
pop and write the poped character to the screen
else
push the character into the stack
}
What is written to the screen for the input "Good**Morn**i***ng" ?Có lời giảiCâu hỏiCả 51 câu của chủ đề51 câu
Xuất hiện trong 1 bộ đề
Đi tiếp từ đây