CS 2114 Chapter Notes - Chapter 6: Java Class Library

34 views4 pages

Document Summary

Stack operations perform faster and more efficient if the first node: example: adding to the top, where topnode is a null field. This operation its first node faster than any other node. references the top entry of the stack. Nodes are allocated only when one is needed for a new entry and deallocated when an entry is removed. This operation is independent of the other public t peek(){ if(isempty()) throw new emptystackexception(); else return topnode. getdata(); entries in the stack, so its performance is (cid:4666)1(cid:4667). public t pop(){ T top = peek(); //might throw emptystackexception assert topnode != null; topnode = topnode. getnextnode(); return top: example: removing the top. Implementations: example: removing the top without calling peek(). This operation is independent of the other entries in the stack, so its performance is (cid:4666)1(cid:4667). public t pop(){ if(topnode != null){ T top = topnode. getdata(); topnode = topnode. getnextnode(); return top; else throw new emptystackexception(); return top;

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents