Estructura de Datos : Pilas Secuenciales


Representaciones

Push y Pop en Pilas


Pila vacia



                       *-----------*           *-----------*
Push Q en Pila vacia   |     Q     |  ------>  |     Q     |
                       *-----------*           *-----------*


                       *-----------*           *-----------*
Push A en Pila         |     A     |  ------>  |     A     |
                       *-----------*           *-----------*
                                               |     Q     |
                                               *-----------*


                                                                       *-----------*
Pop desde Pila                                                ------>  |     A     |
                                               *-----------*           *-----------*
                                               |     Q     |
                                               *-----------*


                                                                       *-----------*
Pop desde Pila                                                ------>  |     Q     |
                                                                       *-----------*


                       *-----------*           *-----------*
Push R en Pila vacia   |     R     |  ------>  |     R     |
                       *-----------*           *-----------*


                       *-----------*           *-----------*
Push D en Pila         |     D     |  ------>  |     D     |
                       *-----------*           *-----------*
                                               |     R     |
                                               *-----------*


                       *-----------*           *-----------*
Push M en Pila         |     M     |  ------>  |     M     |
                       *-----------*           *-----------*
                                               |     D     |
                                               *-----------*
                                               |     R     |
                                               *-----------*


                       *-----------*           *-----------*
Push M en Pila         |     M     |  ------>  |     M     |
                       *-----------*           *-----------*
                                               |     D     |
                                               *-----------*
                                               |     R     |
                                               *-----------*


                                                                       *-----------*
Pop desde Pila                                                ------>  |     M     |
                                               *-----------*           *-----------*
                                               |     D     |
                                               *-----------*
                                               |     R     |
                                               *-----------*


                       *-----------*           *-----------*
Push Q en Pila         |     Q     |  ------>  |     Q     |
                       *-----------*           *-----------*
                                               |     D     |
                                               *-----------*
                                               |     R     |
                                               *-----------*


                       *-----------*           *-----------*
Push S en Pila         |     S     |  ------>  |     S     |
                       *-----------*           *-----------*
                                               |     Q     |
                                               *-----------*
                                               |     D     |
                                               *-----------*
                                               |     R     |
                                               *-----------*


Ejemplos

Llamados a Subprogramas

                                                                    *------------*                                     *------------*
                                                                    |            |                                     |            |
                                  *------------*                    |            |                                     | Programa B |
                                  |            |                    |            |                                     |            |
                                  |            |                    | Programa C |                    *------------*   *------------*   *------------*
                                  | Programa A |                    |            |                    |            |   |            |   |            |
                                  |            |                    |            |                    | Programa B |   | Programa B |   | Programa B |
                                  |            |                    |            |                    |            |   |            |   |            |
                 *------------*   *------------*   *------------*   *------------*   *------------*   *------------*   *------------*   *------------*
                 |            |   |            |   |            |   |            |   |            |   |            |   |            |   |            |
                 | Programa B |   | Programa B |   | Programa B |   | Programa B |   | Programa B |   | Programa B |   | Programa B |   | Programa B |
                 |            |   |            |   |            |   |            |   |            |   |            |   |            |   |            |
*------------*   *------------*   *------------*   *------------*   *------------*   *------------*   *------------*   *------------*   *------------*
|            |   |            |   |            |   |            |   |            |   |            |   |            |   |            |   |            |
|            |   |            |   |            |   |            |   |            |   |            |   |            |   |            |   |            |
| Programa A |   | Programa A |   | Programa A |   | Programa A |   | Programa A |   | Programa A |   | Programa A |   | Programa A |   | Programa A |
|            |   |            |   |            |   |            |   |            |   |            |   |            |   |            |   |            |
|            |   |            |   |            |   |            |   |            |   |            |   |            |   |            |   |            |
*------------*   *------------*   *------------*   *------------*   *------------*   *------------*   *------------*   *------------*   *------------*

----------------------------------------------------------------------- Tiempo ---------------------------------------------------------------------->

Buenas y Malas Representaciones de Pilas

                                                          *----------*
                                                          |  Caja 8  |
                                                          *----------*
                                                         *------------*
                                                         |   Caja 7   |
                                                         *------------*                                                       --------->                           --------->
                                                          *----------*
                                                          |          |                    +---+*--------------*+---+        ---------------------------------------------------
                                                          |  Caja 6  |                    |   ||   Item  3    ||   |         | | | | | | | |\| | | | | | | | / | | | | | | | |
                  *----------*                            |          |                    +-+ |*--------------*| +-+        -----------------\--------------/------------------
                  |  Caja 4  |                            *----------*                      | |*--------------*| |                          \-\            /-/
                  *----------*                           *------------*                     | ||   Item  2    || |                           \-\          /-/
                 *------------*                          |            |                     | |*--------------*| |                            \-\        /-/
                 |   Caja 3   |                          |   Caja 5   |                     | |*--------------*| |                             \-\      /-/
                 *------------*                          |            |                     | ||   Item  1    || |                           \  \-\    /-/  A
                  *----------*                           *------------*                     | |*--------------*| |                            \  \-\  /-/  /
                  |          |                     +-------------------------+              | |----------------| |                             \  \-\/-/  /
                  |  Caja 2  |                     |                         |              | |     -----      | |                              \  \/\/  /
                  |          |                     +--+-+---------------+-+--+              | |        /       | |                               \ |-|  /
                  *----------*                        | |               | |                 | |      /         | |                               | |-| |
                 *------------*                       | |               | |                 | |     -----      | |                               V |-| |
                 |            |                       | |               | |                 | |        /       | |                                 |-|
                 |   Caja 1   |                       | |               | |                 | +----------------+ |                             *---------*
                 |            |                       | |               | |                 |                    |                             |*********|
                 *------------*                       +-+               +-+                 +--------------------+                             *---------*

                 Pilas de Cajas                          Pilas de Cajas                     Contenedor con Resorte                            Vias del Tren


Interfases

Interfase Axiomatica

structure STACK(item)

        declare CREATE()         ----> stack
                ADD(item,stack)  ----> stack
                DELETE(stack)    ----> stack
                TOP(stack)       ----> item
                ISEMTS(stack)    ----> boolean

        for all S perteneciente a stack
                  i perteneciente a item

        let
                ISEMTS(CREATE)   ====> true
                ISEMTS(ADD(i,S)) ====> false
                DELETE(CREATE)   ====> error
                DELETE(ADD(i,S)) ====> S
                TOP(CREATE)      ====> error
                TOP(ADD(i,S))    ====> i
        end

end STACK

Interfase Funcional

Definicion

La Estructura de Datos Pilas ( Stacks ) representa una pila de objetos, segun el comportamiento last-in-first-out (LIFO). Stacks extiende la Estructura de Datos Vector con cinco funciones que permiten que el Vector sea tratado como una Pila. Las funciones tradicionales de push y pop son provistas, Tambien existe un metodo para obtener el item que esta al tope de la pila, otro para probar si la Pila esta vacia y otro para descubrir cuan lejos del tope de la Pila esta un elemento. Cuando la Pila se crea no contiene items.


               java.util

               Class Stack

               java.lang.Object
                 |
                 +--java.util.AbstractCollection
                       |
                       +--java.util.AbstractList
                             |
                             +--java.util.Vector
                                   |
                                   +--java.util.Stack

               public class Stack extends Vector

Variables

Constructores ( Constructors )

Stack()
Creates an empty Stack.

Métodos ( Methods )

boolean empty()
Tests if this stack is empty.

Object peek()
Looks at the object at the top of this stack without removing it from the stack.

Object pop()
Removes the object at the top of this stack and returns that object as the value of this function.

Object push(Object item)
Pushes an item onto the top of this stack.

int search(Object o)
Returns the 1-based position where an object is on this stack.


Pilas Multiples

Stack S(1:m)

        1   2   3   .........................................  m-1  m
      *---------------------------------------------------------------*
      |   |   |   |   .....................................   |   |   |
      *---------------------------------------------------------------*
       0   1   2   3  .....................................    m-2 m-1

Array A(0:m-1)


Stack S1(1:m/2)
Stack S2(1:m/2)
                                     |
    S1  *---------------->           |             <----------------* S2
                                     |
        1   2   3   ...........   m/2|m/2   .............   3   2   1
      *---------------------------------------------------------------*
      |   |   |   |   .........  |   |   |  ...........   |   |   |   |
      *---------------------------------------------------------------*
       0   1   2   3  .................................    m-3 m-2 m-1

Array A(0:m-1)


Stack S1(1:m/n)
Stack S2(1:m/n)
...............
...............
...............
Stack Sn(1:m/n)

       S1 *-------------->            S2 *-------------->            S3 *-------------->            Sn *-------------->

        1   2   3   ...........   m/n| 1   2   3   ...........   m/n| 1   2   3   ...........   m/n| 1   2   3   ...........   m/n|
      *---------------------------------------------------------------------------------------------------------------------------*
      | # |   | % |   .........  |   | # | % |   |   .........  |   | # |   | % |   .........  |   | # |   |   |   .........  | % |
      *---------------------------------------------------------------------------------------------------------------------------*
       0   1   2   3  ......................................................................................................   m-1

Array A(0:m-1)

                                                 # Elemento que esta mas abajo del stack B(i)  ( Botton )

                                                 % Elemento que esta mas alto del stack  T(i)  ( Top )

                                                 Stack Vacio ----> B(i) = T(i) = ( m / n ) ( i - 1 )   1 <= i <= n

                                                 Stack Lleno ----> B(i) = ( m / m ) ( i - 1 )   1 <= i <= n
                                                                   T(i) = B(i+1) - 1            1 <= i <= n