Added Stacks + Implementations + Queue Intf
This commit is contained in:
2
.idea/encodings.xml
generated
2
.idea/encodings.xml
generated
@@ -3,5 +3,7 @@
|
|||||||
<component name="Encoding">
|
<component name="Encoding">
|
||||||
<file url="file://$PROJECT_DIR$/Lab3/src/main/java" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/Lab3/src/main/java" charset="UTF-8" />
|
||||||
<file url="file://$PROJECT_DIR$/Lab3/src/main/resources" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/Lab3/src/main/resources" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/Lab4/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/Lab4/src/main/resources" charset="UTF-8" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
1
.idea/misc.xml
generated
1
.idea/misc.xml
generated
@@ -8,6 +8,7 @@
|
|||||||
<option name="originalFiles">
|
<option name="originalFiles">
|
||||||
<list>
|
<list>
|
||||||
<option value="$PROJECT_DIR$/Lab3/pom.xml" />
|
<option value="$PROJECT_DIR$/Lab3/pom.xml" />
|
||||||
|
<option value="$PROJECT_DIR$/Lab4/pom.xml" />
|
||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
|
|||||||
17
Lab4/pom.xml
Normal file
17
Lab4/pom.xml
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>org.lumijiez</groupId>
|
||||||
|
<artifactId>Lab4</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
||||||
11
Lab4/src/main/java/org/lumijiez/intf/LumiQueue.java
Normal file
11
Lab4/src/main/java/org/lumijiez/intf/LumiQueue.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package org.lumijiez.intf;
|
||||||
|
|
||||||
|
public interface LumiQueue<E> {
|
||||||
|
void enqueue(E item);
|
||||||
|
E dequeue();
|
||||||
|
E peek();
|
||||||
|
boolean isEmpty();
|
||||||
|
int size();
|
||||||
|
void clear();
|
||||||
|
}
|
||||||
|
|
||||||
11
Lab4/src/main/java/org/lumijiez/intf/LumiStack.java
Normal file
11
Lab4/src/main/java/org/lumijiez/intf/LumiStack.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package org.lumijiez.intf;
|
||||||
|
|
||||||
|
public interface LumiStack<E> {
|
||||||
|
|
||||||
|
void push(E item);
|
||||||
|
E pop();
|
||||||
|
E peek();
|
||||||
|
boolean isEmpty();
|
||||||
|
int size();
|
||||||
|
void clear();
|
||||||
|
}
|
||||||
65
Lab4/src/main/java/org/lumijiez/stacks/ArrayLumiStack.java
Normal file
65
Lab4/src/main/java/org/lumijiez/stacks/ArrayLumiStack.java
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package org.lumijiez.stacks;
|
||||||
|
|
||||||
|
import org.lumijiez.intf.LumiStack;
|
||||||
|
|
||||||
|
import java.util.EmptyStackException;
|
||||||
|
|
||||||
|
public class ArrayLumiStack<E> implements LumiStack<E> {
|
||||||
|
private final int MAX_CAPACITY_DEFAULT = 10;
|
||||||
|
private Object[] array;
|
||||||
|
private int size;
|
||||||
|
|
||||||
|
public ArrayLumiStack() {
|
||||||
|
array = new Object[MAX_CAPACITY_DEFAULT];
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayLumiStack(int capacity) {
|
||||||
|
array = new Object[capacity];
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void push(E item) {
|
||||||
|
if (size == array.length) {
|
||||||
|
throw new StackOverflowError("Stack is full");
|
||||||
|
}
|
||||||
|
array[size++] = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public E pop() {
|
||||||
|
if (isEmpty()) {
|
||||||
|
throw new EmptyStackException();
|
||||||
|
}
|
||||||
|
E item = (E) array[--size];
|
||||||
|
array[size] = null;
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public E peek() {
|
||||||
|
if (isEmpty()) {
|
||||||
|
throw new EmptyStackException();
|
||||||
|
}
|
||||||
|
return (E) array[size - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return size == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
array[i] = null;
|
||||||
|
}
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package org.lumijiez.stacks;
|
||||||
|
|
||||||
|
import org.lumijiez.intf.LumiStack;
|
||||||
|
|
||||||
|
import java.util.EmptyStackException;
|
||||||
|
|
||||||
|
public class DynamicArrayLumiStack<E> implements LumiStack<E> {
|
||||||
|
private Object[] array;
|
||||||
|
private int size;
|
||||||
|
private static final int DEFAULT_CAPACITY = 10;
|
||||||
|
|
||||||
|
public DynamicArrayLumiStack() {
|
||||||
|
array = new Object[DEFAULT_CAPACITY];
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void push(E item) {
|
||||||
|
ensureCapacity(size + 1);
|
||||||
|
array[size++] = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public E pop() {
|
||||||
|
if (isEmpty()) {
|
||||||
|
throw new EmptyStackException();
|
||||||
|
}
|
||||||
|
E item = (E) array[--size];
|
||||||
|
array[size] = null;
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public E peek() {
|
||||||
|
if (isEmpty()) {
|
||||||
|
throw new EmptyStackException();
|
||||||
|
}
|
||||||
|
return (E) array[size - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return size == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
array[i] = null;
|
||||||
|
}
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ensureCapacity(int minCapacity) {
|
||||||
|
if (minCapacity > array.length) {
|
||||||
|
int newCapacity = Math.max(array.length * 2, minCapacity);
|
||||||
|
array = java.util.Arrays.copyOf(array, newCapacity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package org.lumijiez.stacks;
|
||||||
|
|
||||||
|
import org.lumijiez.intf.LumiStack;
|
||||||
|
|
||||||
|
import java.util.EmptyStackException;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class LinkedListLumiStack<E> implements LumiStack<E> {
|
||||||
|
private LinkedList<E> list = new LinkedList<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void push(E item) {
|
||||||
|
list.addFirst(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public E pop() {
|
||||||
|
if (isEmpty()) {
|
||||||
|
throw new EmptyStackException();
|
||||||
|
}
|
||||||
|
return list.removeFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public E peek() {
|
||||||
|
if (isEmpty()) {
|
||||||
|
throw new EmptyStackException();
|
||||||
|
}
|
||||||
|
return list.getFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return list.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return list.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
list.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user