page135.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 66 行
HTML
66 行
<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="tex2html2768" HREF="page136.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2766" HREF="page132.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2760" HREF="page134.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2770" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION006113000000000000000"><tt>push</tt>, <tt>pop</tt>, and <tt>getTop</tt> Methods</A></H3><P>Program <A HREF="page135.html#progstackAsArrayb"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the<tt>push</tt>, <tt>pop</tt> and <tt>getTop</tt> methodsof the <tt>StackAsArray</tt> class.The first of these, <tt>push</tt>,adds an element to the stack.In addition to <tt>self</tt>,it takes as its argument the object, <tt>obj</tt>,to be pushed onto the stack.<P><P><A NAME="5640"> </A><A NAME="progstackAsArrayb"> </A> <IMG WIDTH=575 HEIGHT=428 ALIGN=BOTTOM ALT="program5295" SRC="img664.gif" ><BR><STRONG>Program:</STRONG> <tt>StackAsArray</tt> class <tt>push</tt>, <tt>pop</tt>, and <tt>getTop</tt> methods.<BR><P><P>The <tt>push</tt> method first checks to seeif there is room left in the stack.If no room is left,it raises a <tt>ContainerFull</tt> exception.Otherwise, it simply puts the object into the array,and then increases the <tt>_count</tt> variable by one.In a correctly functioning program,stack overflow should not occur.If we assume that overflow does not occur,the running time of the <tt>push</tt> method is clearly <I>O</I>(1).<P>The <tt>pop</tt> method removes an item from the stackand returns that item.The <tt>pop</tt> method first checks if the stack is empty.If the stack is empty,it raises a <tt>ContainerEmpty</tt> exception.Otherwise, it simply decreases <tt>_count</tt> by oneand returns the item found at the top of the stack.In a correctly functioning program,stack underflow will not occur normally.The running time of the <tt>pop</tt> method is <I>O</I>(1).<P>Finally, the <tt>getTop</tt> methodreturns the top item in the stack.The <tt>getTop</tt> method does not modify the stack.In particular, it does <em>not</em> remove the top item from the stack.The <tt>getTop</tt> method first checks if the stack is empty.If the stack is empty,it raises a <tt>ContainerEmpty</tt> exception.Otherwise, it returns the top item,which is found at position <IMG WIDTH=74 HEIGHT=20 ALIGN=MIDDLE ALT="tex2html_wrap_inline60683" SRC="img661.gif" > in the array.Assuming stack underflow does not occur normally,the running time of the <tt>getTop</tt> method is <I>O</I>(1).<P><HR><A NAME="tex2html2768" HREF="page136.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2766" HREF="page132.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2760" HREF="page134.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2770" 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 + =
减小字号Ctrl + -
显示快捷键?