page131.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 72 行
HTML
72 行
<HTML><HEAD><TITLE>Stacks</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="tex2html2716" HREF="page132.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2714" HREF="page130.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2708" HREF="page130.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2718" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H1><A NAME="SECTION006100000000000000000">Stacks</A></H1><P>The simplest of all the containers is a <em>stack</em><A NAME=5022> </A>.A stack is a container which provides exactly one method, <tt>push</tt>,for putting objects into the container;and one method, <tt>pop</tt>,for taking objects out of the container.Figure <A HREF="page131.html#figstack"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> illustrates the basic idea.<P><P><A NAME="5200"> </A><A NAME="figstack"> </A> <IMG WIDTH=575 HEIGHT=335 ALIGN=BOTTOM ALT="figure5026" SRC="img658.gif" ><BR><STRONG>Figure:</STRONG> Basic stack operations.<BR><P><P>Objects which are stored in a stack are kept in a pile.The last item put into the stack is a the top.When an item is pushed into a stack,it is placed at the top of the pile.When an item popped,it is always the top item which is removed.Since it is always the last item to be put into the stackthat is the first item to be removed,a stack is a <em>last-in, first-out</em><A NAME=5204> </A>or <em>LIFO</em><A NAME=5206> </A> data structure.<P>In addition to the <tt>push</tt> and <tt>pop</tt> operations,the typical stack implementation also hasa property called <tt>top</tt>that returns the item at the top of thestack without removing it from the stack.<P>Program <A HREF="page131.html#progstacka"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>Stack</tt> class.The abstract <tt>Stack</tt> class extends the abstract <tt>Container</tt> classdefined in Program <A HREF="page119.html#progcontainera"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>-Program <A HREF="page125.html#progcontainerd"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.Hence, it comprises all of the methodsinherited from <tt>Container</tt> plus the<tt>push</tt> and <tt>pop</tt> methods and the <tt>Top</tt> property.<P><P><A NAME="5627"> </A><A NAME="progstacka"> </A> <IMG WIDTH=575 HEIGHT=374 ALIGN=BOTTOM ALT="program5220" SRC="img659.gif" ><BR><STRONG>Program:</STRONG> Abstract <tt>Stack</tt> class.<BR><P><P>When implementing a data structure,the first issue to be addressedis which foundational data structure(s) to use.Often, the choice is between an array-based implementationand a linked-list implementation.The next two sections present an array-based implementation of stacksfollowed by a linked-list implementation.<P><BR> <HR><UL> <LI> <A NAME="tex2html2719" HREF="page132.html#SECTION006110000000000000000">Array Implementation</A><LI> <A NAME="tex2html2720" HREF="page138.html#SECTION006120000000000000000">Linked-List Implementation</A><LI> <A NAME="tex2html2721" HREF="page144.html#SECTION006130000000000000000">Applications</A></UL><HR><A NAME="tex2html2716" HREF="page132.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2714" HREF="page130.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2708" HREF="page130.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2718" 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 + -
显示快捷键?