Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- * Created by Адель on 22.06.2016.
- */
- public class Main {
- public class MyQueue {
- private int maxSize;
- private int[] queArray;
- private int front;
- private int rear;
- private int nItems; // Для простоты кода будем хранить
- // и изменять количество элементов
- MyQueue(int s) // Конструктор
- {
- maxSize = s;
- queArray = new int[maxSize];
- front = 0;
- rear = -1;
- nItems = 0;
- }
- public void clear() {
- front = -1;
- }
- public void offer(int v) // Добавить элемент в очередь
- {
- if (rear == maxSize - 1) // проверка на конец массива
- rear = -1;
- rear++;
- queArray[rear] = v;
- nItems++; // увеличить счетчик
- }
- public int poll() // Извлечение первого элемента очереди
- {
- int temp = queArray[front]; // взять элемент
- front++;
- if (front == maxSize) // проверка на конец массива
- front = 0;
- nItems--; // уменьшить количество элементов
- return temp;
- }
- public int peek() // Чтение первого элемента очереди
- {
- return queArray[front];
- }
- public boolean empty() // true если очередь пуста
- {
- if (nItems == 0) return true;
- return false;
- }
- public boolean isFull() // true если очередь полна
- {
- if (nItems == maxSize) return true;
- return false;
- }
- public static void main(String args[]) {
- int i;
- Scanner sc = new Scanner(System.in);
- MyStack st = new MyStack(100);
- String command = "";
- while (!command.equals("exit")) {
- command = sc.next();
- if (command.equals("push")) {
- st.push(sc.nextInt());
- System.out.println("ok");
- }
- if (command.equals("pop")) {
- System.out.println(st.pop());
- }
- if (command.equals("back")) {
- System.out.println(st.peek());
- }
- if (command.equals("size")) {
- System.out.println(top);
- }
- if (command.equals("clear")) {
- st.clear();
- System.out.println("ok");
- }
- }
- System.out.println("bye");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement