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 MyStack {
- private int maxSize; // Размер массива
- private int[] stackArray;
- private int top; // Вершина стека
- public MyStack(int s) // Конструктор
- {
- maxSize = s; // Определение размера стека
- stackArray = new int[maxSize]; // Создание массива
- top = -1; // Пока нет ни одного элемента
- }
- public void push(int v) // Размещение элемента на вершине стека
- {
- top++;
- stackArray[top] = v; // Увеличение top, вставка элемента
- }
- public int pop() // Извлечение элемента с вершины стека
- {
- top--;
- return stackArray[top + 1]; // Извлечение элемента
- }
- public long peek() // Чтение элемента с вершины стека
- {
- return stackArray[top];
- }
- public boolean empty() // true, если стек пуст
- {
- if (top == -1) return true;
- return false;
- }
- public boolean full() // true, если стек полон
- {
- if (top == maxSize - 1) return true;
- return false;
- }
- public void clear() {
- top = -1;
- }
- }
- 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