Universidade Federal de Pernambuco 
Centro de Informática

IF672- Algoritmos e Estruturas de Dados
junho a setembro de 2002


Considerações sobre as listas implementadas em JAVA

Exemplo

L0Q0) Implemente uma pilha (máximo 1000) utilizando array, onde o tipo básico é o registro

Pessoa
	Nome   : String;
	E-mail : String;


Eis uma resposta:
class Pessoa {
	String nome;
	String mail;
	Pessoa() {}
	Pessoa(String n, String m) {
		nome = n;
		mail = m;
	}
}

class UnderflowException extends Exception {
    UnderflowException() {}
	UnderflowException(String msg) {
		super(msg);
	}
}

class OverflowException extends Exception {
    OverflowException() {}
	OverflowException(String msg) {
		super(msg);
	}
}

class Pilha {
	Pessoa[] pilha = new Pessoa[1000];
	int sp = 0;
	void push(Pessoa p) throws OverflowException {
		if (sp < 1000) {
			pilha[sp++] = p;
   		} else {
			throw new OverflowException("Overflow pushing "+p.nome+"!");
		}
	}
	Pessoa pop() throws UnderflowException {
		if (sp > 0) {
			return pilha[--sp];
   		} else {
			throw new UnderflowException("Underflow!");
		}
	}
	boolean empty() {
		return sp == 0;
	}
}


[Última alteração em 20/06/2002 porKatia.]