estructuradedatosipilas 101118211345-phpapp02

12
Pilas ESTRUCTURA DE DATOS I

Upload: ana-karen-torres

Post on 27-Jul-2015

405 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Estructuradedatosipilas 101118211345-phpapp02

Pilas

ESTRUCTURA DE DATOS I

Page 2: Estructuradedatosipilas 101118211345-phpapp02

PILAS

Definición:Estructura de datos lineal donde los elementos pueden ser añadidos o removidos solo por un extremo. Trabajan con filosofía LIFO (Last In- First Out ).

Ejemplos:Pila de platosPila de discosPila de llamadas a funcionesPila de recursiónPila de resultados parciales de formulas aritméticas, etc.

Page 3: Estructuradedatosipilas 101118211345-phpapp02

OPERACIONES BASICAS CON PILAS

-PUSH (insertar).- Agrega un elementos a la pila en el extremo llamado tope.

-POP (remover).- Remueve el elemento de la pila que se encuentra en el extremo llamado tope.

-VACIA.- Indica si la pila contiene o no contiene elementos.

-LLENA.- Indica si es posible o no agregar nuevos elementos a la pila.

Page 4: Estructuradedatosipilas 101118211345-phpapp02

IMPLEMENTACION DE PILAS:Usando arreglos: Define un arreglo de una

dimensión (vector) donde se almacenan los elementos.

0 1 2 3 4 5

TOPE: Apunta hacia el elemento que se encuentra en el extremo de la pila. (inicialmente es -1).

Page 5: Estructuradedatosipilas 101118211345-phpapp02

Inicio:

Insertar

A:

Tope -1

Insertar

B:

Insertar

C:

Eliminar

elemento

Tope

A A

B

Tope

A

B

Tope

C

A

B

Tope

Ejemplo

Page 6: Estructuradedatosipilas 101118211345-phpapp02

Pila: Métodos en Java.Clase Pila

class PilaConstructores

Pila(int tamaño)Métodos

boolean EstaVacia() boolean EstaLLena()

void push(String dato)void pop()

PILA EN ARREGLOS

Page 7: Estructuradedatosipilas 101118211345-phpapp02

Clase Pilapackage pila_en_arreglos;

public class Pila { private int tamaño; private int top; private String arreglo[];

public Pila(int tamaño) { this.tamaño = tamaño; this.top = 0; this.arreglo = new String [tamaño]; }

Constructor

Page 8: Estructuradedatosipilas 101118211345-phpapp02

Pila Vacía

public boolean EstaVacia(){

if(top==0){

return true;

}else{

return false;

}

}

public boolean EstaLlena(){

if(top==tamaño){ return true; }else{ return false; }

}

Métodos

Pila Llena

Page 9: Estructuradedatosipilas 101118211345-phpapp02

PUSH(Insertar)

public void Push(String dato){ if(EstaLlena()) {

System.out.println("ERROR PILA LLENA");

}else{ arreglo[top]=dato; top++; } }

public void pop(){ if(EstaVacia()){

System.out.println("ERROR PILA VACIA");

} else{ top--; } }

Métodos

POP(Remover)

Page 10: Estructuradedatosipilas 101118211345-phpapp02

Métodos

public void imprimir(){ if(EstaVacia()) System.out.println("Error:pila

vacía"); else for(int i=0;i<top;i++) System.out.print(arreglo[i]+"

"); }

Imprimir

Page 11: Estructuradedatosipilas 101118211345-phpapp02

Clase Mainpackage pila_en_arreglos;

public class Main {

public static void main(String[] args) {

Pila p1 = new Pila(4);

p1.Push("a"); p1.Push("b"); p1.Push("c"); p1.Push("d"); p1.imprimir(); System.out.println("POP"); p1.pop(); p1.imprimir();

System.out.println(); p1.pop(); p1.imprimir(); System.out.println(); p1.pop(); p1.imprimir(); System.out.println(); p1.pop(); p1.imprimir(); }}

Page 12: Estructuradedatosipilas 101118211345-phpapp02

Fin

Autor:

Geovanny Simbaña