tailieunhanh - Data Structures and Program Design in C++ phần 3

vật liệu xanh trong văn bản chính nó sẽ chuyển đến nơi chỉ định. sau khi tham giabiểu tượng (trở lại) trong Acrobat một bước nhảy như vậy, bạn có thể trở lại bằng cách chọn thanh công cụ. ¯ biểu tượng minh bạch chiếu () sẽ trả về một chủ minh bạch vào biểu tượng (quay lại) trên thanh công cụ Acrobat. không chủ đề. | 130 Chapter 4 Linked Stacks and Queues old_top Figure . Popping a node from a linked stack Error_code Stack pop Post The top of the Stack is removed. If the Stack is empty the method returns underflow otherwise it returns success. Node old_top top_node if top_node NULL return underflow top_node old_top- next delete old_top return success When we reset the value of top_node in the method pop the pointer old_top is the only link to the node that used to occupy the top position of the Stack. Therefore once the function ends and old_top goes out of scope there will be no way for us to access that Node. We therefore delete old_top otherwise garbage would be created. Of course in small applications the method would work equally well without the use of delete. However if a client repeatedly used such an implementation the garbage would eventually mount up to occupy all available memory and our client s program would suffocate. Our linked stack implementation actually suffers from a number of subtle defects that we shall identify and rectify in the next section. We hasten to add that we know of no bugs in the methods that we have presented however it is possible for a client to make a Stack object malfunction. We must either document the limitations on the use of our Stack class or we must correct the problems by adding in a number of extra features to the class. Exercises E1. Explain why we cannot use the following implementation for the method push in our linked Stack. Error_code Stack push Stack_entry item Node new_top item top_node top_node new_top return success Section Linked Stacks with Safeguards 131 E2. Consider a linked stack that includes a method size. This method size requires a loop that moves through the entire stack to count the entries since the number of entries in the stack is not kept as a separate member in the stack record. a Write a method size for a linked stack by using a loop that moves a pointer variable from node to node through the

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.