Created
September 21, 2022 20:36
-
-
Save gmakc-094423/be63e90b69d282eaa21d6cd110190e49 to your computer and use it in GitHub Desktop.
Домашнее задание к 4 семинару
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Stack<T> { | |
private T[] array; | |
private int index; | |
public Stack(int length) { | |
this.index = 0; | |
this.array = (T[]) new Object[length]; | |
} | |
public int size() { | |
return this.index; | |
} | |
public boolean empty() { | |
return this.size() == 0; | |
} | |
public void push(T value) { | |
if (this.index < this.array.length) { | |
this.array[index++] = value; | |
} else { | |
System.out.println("Стек переполнен!"); | |
} | |
} | |
public T peek() { | |
try { | |
return this.array[index - 1]; | |
} catch (Exception e) { | |
System.out.println("Стек пуст!"); | |
} | |
return null; | |
} | |
public T pop() { | |
try { | |
return this.array[--index]; | |
} catch (Exception e) { | |
System.out.println("Стек пуст!"); | |
} | |
return null; | |
} | |
public static void main(String[] args) { | |
Stack<String> stack = new Stack<>(3); | |
stack.push("String 1"); | |
stack.push("String 2"); | |
System.out.printf("Размер стека: %s\n", stack.size()); | |
stack.push("String 3"); | |
System.out.println(stack.pop()); | |
// System.out.println(stack.empty()); | |
System.out.println(stack.pop()); | |
System.out.println(stack.peek()); | |
System.out.println(stack.pop()); | |
System.out.println(stack.pop()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Queue<T> { | |
private T[] array; // длина мессива (списка) | |
private int index; // текущее количество элементов в очереди | |
private int front; // начальный индекс | |
private int rear; // конечный индекс | |
public Queue(int length) { | |
this.index = 0; | |
this.front = 0; | |
this.rear = -1; | |
this.array = (T[]) new Object[length]; | |
} | |
public int size() { | |
return this.index; | |
} | |
public boolean empty() { | |
return this.size() == 0; | |
} | |
public void add(T value) { | |
if (this.index < this.array.length) { // проверка на заполненность | |
if (rear == this.array.length - 1) { // циклический перенос | |
rear--; | |
} | |
index++; | |
this.array[++rear] = value; | |
} else { | |
System.out.println("Очередь переполнена!"); | |
} | |
} | |
public T peek() { | |
return this.array[front]; | |
} | |
public T poll() { | |
T temp = this.array[front]; | |
this.array[front++] = null; | |
if (front == this.array.length - 1) { // циклический перенос | |
front = 0; | |
} | |
index--; // уменьшаем количество элементов в очереди | |
return temp; | |
} | |
public void display() { | |
for (Integer i = 0; i < this.array.length; i++) { | |
System.out.printf("Elem %s = %s\n", i, this.array[i]); | |
} | |
} | |
public static void main(String[] args) { | |
Queue<String> queue = new Queue<>(3); | |
queue.add("String 1"); | |
queue.add("String 2"); | |
queue.add("String 3"); | |
System.out.printf("Size: %s\n", queue.size()); | |
System.out.printf("Is empty? %s\n", queue.empty()); | |
System.out.printf("peek: %s\n", queue.peek()); | |
System.out.printf("poll: %s\n", queue.poll()); | |
System.out.printf("peek: %s\n", queue.peek()); | |
System.out.printf("Size: %s\n", queue.size()); | |
queue.display(); | |
queue.add("String 4"); | |
queue.add("String 5"); | |
System.out.printf("peek: %s\n", queue.peek()); | |
queue.display(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment