,QWHUID]SDUDSLODV public interface Stack { public int size(); public boolean isEmpty(); public void push(Object o); public Object pop() throws StackEmptyException; public Object top() throws StackEmptyException; } Copyright © 2001 [email protected] Java: Pilas y Colas / 1 8QLQWHUID]\ YDULDVLPSOHPHQWDFLRQHV Stack ArrayStack Copyright © 2001 [email protected] LinkedStack Java: Pilas y Colas / 2 1 ,PSOHPHQWDFLyQ EDVDGDHQDUUD\V S toptoptoptoptoptop 1 2 3 4 5 0 1 3 4 2 5 Copyright © 2001 [email protected] N-1 Java: Pilas y Colas / 3 ,PSOHPHQWDFLyQ EDVDGDHQDUUD\V public class ArrayStack implements Stack { public static final int CAP=1000; private int capacity; private Object S[]; private int top=-1; public ArrayStack(){this(CAP);} public ArrayStack(int cap){ capacity=cap; S=new Object[capacity];} Copyright © 2001 [email protected] Java: Pilas y Colas / 4 2 ,PSOHPHQWDFLyQ EDVDGDHQDUUD\V public int size() {return (top+1);} public boolean isEmpty() {return (top<0);} public Object top() throws StackEmptyException { if (isEmpty()) throw new StackEmptyException("vacio"}; return S[top];} Copyright © 2001 [email protected] Java: Pilas y Colas / 5 ,PSOHPHQWDFLyQ EDVDGDHQDUUD\V public void push(Object o) throws StackFullException { if (size()==capacity) throw new StackFullException("lleno"); S[++top]=o;} public Object pop() throws StackEmptyException { Object el; if (isEmpty()) throw new StackEmptyException("vacio"}; el=S[top]; S[top--]=null; return el;} Copyright © 2001 [email protected] Java: Pilas y Colas / 6 } 3 ,PSOHPHQWDFLyQEDVDGD HQOLVWDVHQFDGHQDGDV Madrid Miami Múnich Copyright © 2001 [email protected] Java: Pilas y Colas / 7 ,PSOHPHQWDFLyQ EDVDGDHQOLVWDV class Node { private Object elem; private Node next; public Node(Object e, Node n) { elem=e; next=n;} void setElem(Object e) {elem=e;} void setNext(Node n) {next=n;} Object getElem() {return elem;} Node getNext() {return next;} } Copyright © 2001 [email protected] Java: Pilas y Colas / 8 4 ,PSOHPHQWDFLyQ EDVDGDHQOLVWDV public class LinkedStack implements Stack { private Node top; private int size; public LinkedStack() {top=null; size=0;} public int size() {return size;} public boolean isEmpty() { return(top==null);} Copyright © 2001 [email protected] Java: Pilas y Colas / 9 Java: Pilas y Colas / 10 ,PSOHPHQWDFLyQ EDVDGDHQOLVWDV public void push(Object e) { Node n=New Node(); n.setElem(e); n.setNext(top); top=n; size++; } Copyright © 2001 [email protected] 5 ,QVHUFLyQpush top Madrid Miami Múnich Moscú Copyright © 2001 [email protected] Java: Pilas y Colas / 11 ,PSOHPHQWDFLyQ EDVDGDHQOLVWDV public Object top() throws StackEmptyException { if (isEmpty()) throw new StackEmptyException("vacia"); return top.getElem(); } Copyright © 2001 [email protected] Java: Pilas y Colas / 12 6 ,PSOHPHQWDFLyQ EDVDGDHQOLVWDV public Object pop() throws StackEmptyException { Object temp; if (isEmpty()) throw new StackEmptyException("vacia"); temp=top.getElem(); top=top.getNext(); size--; return temp; } Copyright © 2001 [email protected] Java: Pilas y Colas / 13 %RUUDGRpop top Moscú Copyright © 2001 [email protected] Madrid Miami Múnich Java: Pilas y Colas / 14 7 3LODV\UHFXUVLyQ ❚ public static long fac (int n) {if (n<=1) return 1; else return n*fac(n-1); } Copyright © 2001 [email protected] Java: Pilas y Colas / 15 (MHFXFLyQ ❚ ❚ ❚ ❚ ❚ ❚ ❚ ❚ fac(4) 4*fac(3) 4*(3*fac(2)) 4*(3*(2*fac(1))) 4*(3*(2*1))) 4*(3*2) 4*6 24 Copyright © 2001 [email protected] 2* 3* 4* Java: Pilas y Colas / 16 8 &RODV &DUORV'HOJDGR.ORRV ,QJHQLHUtD7HOHPiWLFD 8QLY&DUORV,,,GH0DGULG Copyright © 2001 [email protected] Java: Pilas y Colas / 17 (MHPSOR ❚ /DFRODGHODXWRE~V ❚ /DFRODGHODLPSUHVRUD Copyright © 2001 [email protected] Java: Pilas y Colas / 18 9 &DUDFWHUtVWLFDV ❚ (VWUXFWXUDOLQHDO ❚ $FFHVRGHLQVHUFLyQSRUXQH[WUHPR\ GHHOLPLQDFLyQSRUHORWURH[WUHPR Copyright © 2001 [email protected] Java: Pilas y Colas / 19 ,QWHUID]SDUDFRODV public interface Queue { public int size(); public boolean isEmpty(); public void enqueue(Object o); public Object dequeue() throws QueueEmptyException; public Object front() throws QueueEmptyException; } Copyright © 2001 [email protected] Java: Pilas y Colas / 20 10 8QLQWHUID]\ YDULDVLPSOHPHQWDFLRQHV Queue ArrayQueue LinkedQueue Copyright © 2001 [email protected] Java: Pilas y Colas / 21 ,QVHUFLyQenqueue top tail Madrid Copyright © 2001 [email protected] Miami Múnich Moscú Java: Pilas y Colas / 22 11 ,PSOHPHQWDFLyQ EDVDGDHQOLVWDV public void enqueue(Object e) { Node n=New Node(); n.setElem(e); n.setNext(null); if (size==0) top=n; else tail.setNext(n); tail=n; size++; } Copyright © 2001 [email protected] Java: Pilas y Colas / 23 public Object dequeue() throws QueueEmptyException { Object temp; if (isEmpty()) throw new QueueEmptyException("vacia"); temp=top.getElem(); top=top.getNext(); size--; if (size==0) tail=null return temp; } Copyright © 2001 [email protected] Java: Pilas y Colas / 24 ,PSOHPHQWDFLyQ EDVDGDHQOLVWDV 12