Advertisement
Guest User

Queue

a guest
Feb 15th, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. package Queue;
  2. import java.io.*;
  3. public class Queue
  4. {
  5. private int head,tail;
  6. private final int MAX;
  7. private Persona[] elem;
  8. private static final int MAXDEFAULT = 10;
  9.  
  10. public Queue()
  11. {
  12. this(MAXDEFAULT);
  13. }
  14.  
  15. public Queue(int max)
  16. {
  17. head = tail = 0;
  18. MAX = max;
  19. elem = new Persona[MAX];
  20. }
  21.  
  22. public boolean IsEmpty()
  23. {
  24. return head == tail;
  25. }
  26.  
  27. public boolean IsFull()
  28. {
  29. return head == (tail++%MAX);
  30. }
  31.  
  32. public boolean EnQueue(String str,int val)
  33. {
  34. if(IsFull())
  35. {
  36. System.out.println("L' elenco è pieno");
  37. return false;
  38. }
  39. elem[tail].Nome = str;
  40. elem[tail].età = val;
  41. tail = ++tail % MAX;
  42. return true;
  43. }
  44.  
  45. public Persona Dequeue()
  46. {
  47. if(IsEmpty())
  48. {
  49. System.out.println("L' elenco è vuoto");
  50. return null;
  51. }
  52. Persona aux = elem[head];
  53. head = ++head% MAX;
  54. return aux;
  55. }
  56.  
  57. public void ClearQueue()
  58. {
  59. head = tail = 0;
  60. }
  61.  
  62. public Persona FirstElem()
  63. {
  64. return elem[head];
  65. }
  66.  
  67. public void VisAll()
  68. {
  69. int i = head;
  70. for(; i != (tail++)%MAX;i++)
  71. System.out.println("Nome :"+elem[i].Nome+"\n"
  72. +"Eta' :"+elem[i].età+"\n");
  73. }
  74. public static void main(String[] args)
  75. {
  76. ConsoleReader console = new ConsoleReader();
  77. int scelta=0;
  78. System.out.println("Quanti record vuoi creare?");
  79. int max = console.ReadInt();
  80. Queue lista = new Queue(max);
  81. System.out.println("Lista Creata\n\n\n");
  82. do
  83. {
  84. System.out.println("Menù :");
  85. System.out.println("1) EnQueue");
  86. System.out.println("2) DeQueue");
  87. System.out.println("3) Cancella Queue");
  88. System.out.println("4) Primo elemento");
  89. System.out.println("5) Visualizza tutto l'elenco");
  90. System.out.println("6) Esci");
  91. System.out.println("\n\nInserisci la scelta");
  92. scelta = console.ReadInt();
  93. switch(scelta)
  94. {
  95. case 1:
  96. System.out.println("Inserisci il nome");
  97. String str = console.ReadLine();
  98. System.out.println("Inserisci l' età");
  99. int n = console.ReadInt();
  100. lista.EnQueue(str, n);
  101. break;
  102. case 2:
  103. Persona aux = lista.Dequeue();
  104. System.out.println("Hai eliminato "+aux.Nome);
  105. break;
  106. case 3:
  107. lista.ClearQueue();
  108. System.out.println("Elenco cancellato");
  109. break;
  110. case 4:
  111. Persona aux2 = lista.FirstElem();
  112. System.out.println("La prima persona è :"+aux2.Nome);
  113. break;
  114. case 5:
  115. lista.VisAll();
  116. break;
  117. case 6:
  118. System.out.println("Ciao!");
  119. break;
  120. default:
  121. System.out.println("Scelta errata. Riprova");
  122. break;
  123. }
  124. }while(scelta != 6);
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement