Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Queue;
- import java.io.*;
- public class Queue
- {
- private int head,tail;
- private final int MAX;
- private Persona[] elem;
- private static final int MAXDEFAULT = 10;
- public Queue()
- {
- this(MAXDEFAULT);
- }
- public Queue(int max)
- {
- head = tail = 0;
- MAX = max;
- elem = new Persona[MAX];
- }
- public boolean IsEmpty()
- {
- return head == tail;
- }
- public boolean IsFull()
- {
- return head == (tail++%MAX);
- }
- public boolean EnQueue(String str,int val)
- {
- if(IsFull())
- {
- System.out.println("L' elenco è pieno");
- return false;
- }
- elem[tail].Nome = str;
- elem[tail].età = val;
- tail = ++tail % MAX;
- return true;
- }
- public Persona Dequeue()
- {
- if(IsEmpty())
- {
- System.out.println("L' elenco è vuoto");
- return null;
- }
- Persona aux = elem[head];
- head = ++head% MAX;
- return aux;
- }
- public void ClearQueue()
- {
- head = tail = 0;
- }
- public Persona FirstElem()
- {
- return elem[head];
- }
- public void VisAll()
- {
- int i = head;
- for(; i != (tail++)%MAX;i++)
- System.out.println("Nome :"+elem[i].Nome+"\n"
- +"Eta' :"+elem[i].età+"\n");
- }
- public static void main(String[] args)
- {
- ConsoleReader console = new ConsoleReader();
- int scelta=0;
- System.out.println("Quanti record vuoi creare?");
- int max = console.ReadInt();
- Queue lista = new Queue(max);
- System.out.println("Lista Creata\n\n\n");
- do
- {
- System.out.println("Menù :");
- System.out.println("1) EnQueue");
- System.out.println("2) DeQueue");
- System.out.println("3) Cancella Queue");
- System.out.println("4) Primo elemento");
- System.out.println("5) Visualizza tutto l'elenco");
- System.out.println("6) Esci");
- System.out.println("\n\nInserisci la scelta");
- scelta = console.ReadInt();
- switch(scelta)
- {
- case 1:
- System.out.println("Inserisci il nome");
- String str = console.ReadLine();
- System.out.println("Inserisci l' età");
- int n = console.ReadInt();
- lista.EnQueue(str, n);
- break;
- case 2:
- Persona aux = lista.Dequeue();
- System.out.println("Hai eliminato "+aux.Nome);
- break;
- case 3:
- lista.ClearQueue();
- System.out.println("Elenco cancellato");
- break;
- case 4:
- Persona aux2 = lista.FirstElem();
- System.out.println("La prima persona è :"+aux2.Nome);
- break;
- case 5:
- lista.VisAll();
- break;
- case 6:
- System.out.println("Ciao!");
- break;
- default:
- System.out.println("Scelta errata. Riprova");
- break;
- }
- }while(scelta != 6);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement