Advertisement
AdelKhalilov

Untitled

Jun 23rd, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4. * Created by Адель on 22.06.2016.
  5. */
  6. public class Main {
  7. public class MyQueue {
  8. private int maxSize;
  9. private int[] queArray;
  10. private int front;
  11. private int rear;
  12. private int nItems; // Для простоты кода будем хранить
  13.  
  14. // и изменять количество элементов
  15. MyQueue(int s) // Конструктор
  16. {
  17. maxSize = s;
  18. queArray = new int[maxSize];
  19. front = 0;
  20. rear = -1;
  21. nItems = 0;
  22. }
  23.  
  24. public void clear() {
  25. front = -1;
  26. }
  27.  
  28. public void offer(int v) // Добавить элемент в очередь
  29. {
  30. if (rear == maxSize - 1) // проверка на конец массива
  31. rear = -1;
  32. rear++;
  33. queArray[rear] = v;
  34. nItems++; // увеличить счетчик
  35. }
  36.  
  37. public int poll() // Извлечение первого элемента очереди
  38. {
  39. int temp = queArray[front]; // взять элемент
  40. front++;
  41. if (front == maxSize) // проверка на конец массива
  42. front = 0;
  43. nItems--; // уменьшить количество элементов
  44. return temp;
  45. }
  46.  
  47. public int peek() // Чтение первого элемента очереди
  48. {
  49. return queArray[front];
  50. }
  51.  
  52. public boolean empty() // true если очередь пуста
  53. {
  54. if (nItems == 0) return true;
  55. return false;
  56. }
  57.  
  58. public boolean isFull() // true если очередь полна
  59. {
  60. if (nItems == maxSize) return true;
  61. return false;
  62. }
  63.  
  64. public static void main(String args[]) {
  65.  
  66. int i;
  67. Scanner sc = new Scanner(System.in);
  68. MyStack st = new MyStack(100);
  69. String command = "";
  70.  
  71. while (!command.equals("exit")) {
  72. command = sc.next();
  73. if (command.equals("push")) {
  74. st.push(sc.nextInt());
  75. System.out.println("ok");
  76. }
  77. if (command.equals("pop")) {
  78. System.out.println(st.pop());
  79. }
  80. if (command.equals("back")) {
  81. System.out.println(st.peek());
  82. }
  83. if (command.equals("size")) {
  84. System.out.println(top);
  85. }
  86. if (command.equals("clear")) {
  87. st.clear();
  88. System.out.println("ok");
  89. }
  90.  
  91. }
  92. System.out.println("bye");
  93. }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement