📄 page141.html
字号:
<HTML><HEAD><TITLE>push, pop and getTop Methods</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html2837" HREF="page142.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2835" HREF="page138.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2829" HREF="page140.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2839" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION006123000000000000000"><tt>push</tt>, <tt>pop</tt> and <tt>getTop</tt> Methods</A></H3><P>The <tt>push</tt>, <tt>pop</tt> and <tt>getTop</tt> methodsof the <tt>StackAsLinkedList</tt> classare defined in Program <A HREF="page141.html#progstackAsLinkedListb"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<P><P><A NAME="5659"> </A><A NAME="progstackAsLinkedListb"> </A> <IMG WIDTH=575 HEIGHT=390 ALIGN=BOTTOM ALT="program5445" SRC="img671.gif" ><BR><STRONG>Program:</STRONG> <tt>StackAsLinkedList</tt> class <tt>push</tt>, <tt>pop</tt> and <tt>getTop</tt> methods.<BR><P><P>The implementation of <tt>push</tt> is trivial.It takes as its argument the object, <tt>obj</tt>, to be pushed onto the stackand simply prepends that object to the linked list <tt>_list</tt>.Then, one is added to the <tt>_count</tt> variable.The running time of the <tt>push</tt> method is constant,since the <tt>prepend</tt> method has a constant running time,and updating the <tt>_count</tt> only takes <I>O</I>(1) time.<P>The <tt>pop</tt> method is implemented using the<tt>first</tt> property and the <tt>extract</tt> methodof the <tt>LinkedList</tt> class.The <tt>first</tt> property is used to get the first itemin the linked list.The <tt>first</tt> property get method runs in constant time.The <tt>extract</tt> method is then called to extract the firstitem from the linked list.In the worst case, <tt>extract</tt> requires <I>O</I>(<I>n</I>) time to extractan item from a linked list of length <I>n</I>.But the worst-case time arises only when it is the <em>last</em>element of the list which is to be extracted.In the case of the <tt>pop</tt> method,it is always the <em>first</em> element that is extracted.This can be done in constant time.Assuming that the exception which is raised when <tt>pop</tt>is called on an empty list does not occur,the running time for <tt>pop</tt> is <I>O</I>(1).<P>The definition of the <tt>getTop</tt> method is quite simple.It returns the first object in the linked list.Provided the linked list is not empty,the running time of <tt>getTop</tt> is <I>O</I>(1).If the linked list is empty,the <tt>getTop</tt> method raises a <tt>ContainerEmpty</tt> exception.<P><HR><A NAME="tex2html2837" HREF="page142.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2835" HREF="page138.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2829" HREF="page140.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2839" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -