pessoal, alguém saberia me informar como implementar um método exibe para pilha e para filas?
Só tá faltando isso, seria de imensa ajuda. Procurei, quebrei a cabeça, mas por ser digamos q “esqueletico” tá bem dificil de achar fontes assim, apesar de ser extremamente básico.
aqui a classe pilha
[code]public class Pilha{
private int v[];
private int topo;
public Pilha(int capacidade){
topo=-1;
v=new int[capacidade];
}
public Pilha(){
this(10);
}
[/code]
e aqui a classe fila
[code]public class Fila{
private int v[];
private int frente;
private int re;
public Fila(int capacidade){
frente=0;
re=-1;
v=new int[capacidade];
}
public Fila(){
this(10);
}[/code]
Alguém saberia como ficariam os métodos. Obrigado desde já pela paciência. Boa semana
código q desenvolvi já ha algum tempo, acho q p a disciplina de Estrut de Dados
no
[code]public class No {
private String data;
private No next;
public No(String d){
this.data = d;
this.next = null;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public No getNext() {
return next;
}
public void setNext(No next) {
this.next = next;
}
}
[/code]
//plha dinamica
public class PilhaDinamica{
private No head;
private int size=0;
//inserir elemento sempre na cabeça
public void push(String element) throws GeneralException{
/**
* Condiçoes para inserir na Lista:
* o elemento nao pode ser nulo ou vazio
*/
if (!element.isEmpty() || element!=null){
No no = new No(element);
//adiciona elemento no inicio da Pilha
no.setNext(head);
head = no;
size++;
}
else{
throw new GeneralException("ERRO: Elemento Invalido!");
}
}
//remover elemento LIFO
public void pop() throws GeneralException{
/**
* Condiçoes para remover item da Lista:
* 1. A pilha nao deve estar VAZIA
*/
if (!isEmpty()){
//remove elemento no inicio da lista
head = head.getNext();
size--;
}
else{
throw new GeneralException("ERRO: Pilha Vazia!");
}
}
public boolean isEmpty() {
if (head==null)
return true;
return false;
}
public int size() {
return size;
}
public String top() throws GeneralException{
if (isEmpty())
throw new GeneralException("ERRO: Pilha Vazia!");
return head.getData();
}
public void print(){
No temp = head;
int count = 1;
while(temp!=null){
System.out.println("Posição " + count + " - Valor: " + temp.getData());
temp=temp.getNext();
count++;
}
}
}
pilha estatica
public class PilhaEstatica {
private String [] arrayStack;
private int size = -1;
public PilhaEstatica (int size){
this.arrayStack = new String[size];
}
//inserir elemento sempre na cabeça
public void push(String element) throws GeneralException{
/**
* Condições para inserir na Pilha:
* 1. a pilha não deve estar cheia
*/
if (!isFull()){
//adiciona elemento no inicio da Pilha
//todos os outros elementos terão que andar para as posições posteriores
size++;
for (int i=size;i>0;i--){
this.arrayStack[i] = this.arrayStack[i-1];
}
this.arrayStack[0] = element;
}
else{
throw new GeneralException("ERRO: Pilha Cheia!");
}
}
//remover elemento LIFO
public void pop() throws GeneralException{
/**
* Condições para remover item da Lista:
* 1. A pilha não deve estar VAZIA
*/
if (!isEmpty()){
//remove elemento no inicio da lista
//todos os outros elementos terão que andar para as posições anteriores
for(int i=0;i<size;i++){
this.arrayStack[i]=this.arrayStack[i+1];
}
this.arrayStack[size]=null;
size--;
}
else{
throw new GeneralException("ERRO: Pilha Vazia!");
}
}
//verifica se a pilha esta vazia
public boolean isEmpty() {
if (this.arrayStack[0]==null)
return true;
return false;
}
//tamanho da pilha
public int size() {
return size+1;
}
//exibe o elemtno da cabeça
public String top() throws GeneralException{
if (isEmpty())
throw new GeneralException("ERRO: Pilha Vazia!");
return this.arrayStack[0];
}
//verifica se a pilha está cheia.
private boolean isFull(){
if (this.arrayStack[arrayStack.length-1]!=null)
return true;
return false;
}
//imprime a pilha
public void print(){
for(int i=0;i<=size;i++){
System.out.println("Objeto posição: " + (i+1) + " - valor: " + arrayStack[i]);
}
}
}
lista dinamica
public class ListaEncadeadaDinamica{
private No head, tail;
private int size=1;
public ListaEncadeadaDinamica(){}
public void add(String element, int position) throws GeneralException {
/**
* Condições para inserir na Lista:
* o elemento não pode ser nulo ou vazio
*/
if (!element.isEmpty() || element!=null){
No no = new No(element);
//se a lista estiver vazia, No é o primeiro elemento;
if (head==null)
head = tail = no;
else{
switch (position){
//adiciona no fim da lista;
case -1:
tail.setNext(no);
tail = no;
break;
//adiciona no inÃcio da lista
case 0:
no.setNext(head);
head = no;
break;
//posição passada pelo usuário
default:
if (position>size){
add(element, -1);
}
else{
No previous = head;
for (int i=1;i<position-1;i++){
previous = previous.getNext();
}
No next = previous.getNext();
no.setNext(next);
previous.setNext(no);
previous = next = null;
}
break;
}
size++;
}
}
else{
throw new GeneralException("ERRO: Elemento Nulo!");
}
}
public void remove(int position) throws GeneralException {
/**
* Condições para inserir na Lista:
* a lista não pode estar vazia
*/
if (!isEmpty()){
No previous = head;
switch (position){
//remove do final da lista
case -1:
while (previous.getNext()!=tail){
previous = previous.getNext();
}
previous.setNext(null);
tail = previous;
break;
//remmove no inÃcio da lista
case 0:
head = head.getNext();
break;
//remove na posição passada pelo usuário
default:
if (position>size)
remove(-1);
else{
for (int i=1;i<position-1;i++){
previous = previous.getNext();
}
No next = previous.getNext().getNext();
previous.setNext(next);
}
break;
}
size--;
}
else{
throw new GeneralException("ERRO: Lista Vazia!");
}
}
public boolean isEmpty() {
if (head==null)
return true;
return false;
}
public int size() {
return this.size;
}
public String show(int position) {
if (!isEmpty() && position<=size){
No temp = head;
for (int i=1;i<=position;i++){
if (i!=position)
temp = temp.getNext();
}
return temp.getData();
}
else if (position>size){
return "Posição Inválida";
}
else
return "Lista Vazia";
}
public void print(){
No d = head;
int count = 1;
while(d!=null){
System.out.println("Posição " + count + " - Valor: " + d.getData());
d=d.getNext();
count++;
}
}
}
lista estatica
public class ListaEncadeadaEstatica {
private String [] arrayList;
private int tail = -1;
public ListaEncadeadaEstatica(int size){
this.arrayList = new String[size];
}
public void add(String element, int position) throws GeneralException {
/**
* Condições para inserir na Lista:
* 1. a lista não deve estar cheia
*/
if (!isFull()){
tail++;
switch (position){
//adiciona elemento no fim da lista
case -1:
this.arrayList[tail] = element;
break;
//adiciona elemento no inicio da lista
//todos os outros elementos terão que andar para as posições posteriores
case 0:
for(int i=getTail();i>0;i--){
this.arrayList[i]=this.arrayList[i-1];
}
this.arrayList[0]=element;
break;
//adiciona elemento na posição passada pelo usuário
//se a posição passada pelo usuário for maios que o fim da lista,
//insere no fim da lista.
default:
if (position>=tail){
add(element, -1);
}
else {
for(int i=getTail();i>position;i--){
this.arrayList[i]=this.arrayList[i-1];
}
this.arrayList[position]=element;
}
break;
}
}
else{
throw new GeneralException("ERRO: Lista Cheia!");
}
}
public void remove(int position) throws GeneralException {
/**
* Condições para remover item da Lista:
* 1. A lista não deve estar VAZIA
*/
if (!isEmpty()){
switch (position){
//remove elemento no fim da lista
case -1:
break;
//remove elemento no inicio da lista
//todos os outros elementos terão que andar para as posições anteriores
case 0:
for(int i=0;i<getTail();i++){
this.arrayList[i]=this.arrayList[i+1];
}
break;
//remove elemento na posição passada pelo usuário
default:
if (position<tail){
for(int i=position;i<=getTail();i++){
this.arrayList[i]=this.arrayList[i+1];
}
}
break;
}
this.arrayList[tail] = null;
tail--;
}
else
throw new GeneralException("ERRO: Lista Vazia!");
}
public boolean isEmpty() {
if (this.arrayList[0]==null)
return true;
return false;
}
public boolean isFull(){
if (this.arrayList[arrayList.length-1]!=null)
return true;
return false;
}
public int size() {
return this.getTail()+1;
}
public String show(int position) {
if (!isEmpty() && position<=tail)
return this.arrayList[position];
else if (position>tail){
return "Posição Inválida";
}
else
return "Lista Vazia";
}
public void print(){
for(int i=0;i<=getTail();i++){
System.out.println("Objeto posição: " + i + " - valor: " + arrayList[i]);
}
}
public int getTail() {
return tail;
}
}
Agora, na BOA, ESTUDE…
Se vc somente copiar e colar, sem estudar, VOCÊ sairá perdendo!!
Abraços
Caraaa, valeu aeee
Pode crer. Isso vai ser muito valioso para estudar sim. Excelente.
Valew mesmo, tudo de bom pra ti, obrigado pela ajuda

Se quiser estudar, ao invés de copiar e colar, esse código aqui está muito comentado e elegante:
http://www.guj.com.br/posts/list/55235.java
[quote]Se quiser estudar, ao invés de copiar e colar, esse código aqui está muito comentado e elegante:
http://www.guj.com.br/posts/list/55235.java[/quote]
Realmente está muito bom!!
Abraços
Obrigado pela dicas!! principalmente o link indicado para estudo, bem didático. wlw