master
3009594 2024-09-21 22:40:46 +02:00
parent e5d61482f6
commit b84ed6ce96
8 changed files with 297 additions and 0 deletions

View File

@ -0,0 +1 @@
,OBAI/obaya,obai,21.09.2024 22:26,file:///C:/Users/obaya/AppData/Roaming/LibreOffice/4;

View File

@ -0,0 +1,63 @@
package QueueStructure.CircularQueue_UsingArray;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import QueueStructure.MyQueue;
class JTest {
MyCircularQueue<Integer> test;
@BeforeEach
void erstelle_List() {
test = new MyCircularQueue<>(6);
}
@Test
void test_Enqueue() throws MyException {
assertEquals(1,test.enqeueu(1));
}
@Test
void test_getFront() throws MyException {
test.enqeueu(4);
test.enqeueu(2);
test.enqeueu(6);
assertEquals(4,test.getFront());
}
@Test
void test_getRear() throws MyException {
test.enqeueu(4);
test.enqeueu(2);
test.enqeueu(6);
assertEquals(6,test.getRear());
}
@Test
void test_dequeue() throws MyException {
test.enqeueu(4);
test.enqeueu(2);
test.enqeueu(6);
assertTrue(test.dequeue());
}
@Test
void test_clear() throws MyException {
test.enqeueu(4);
test.enqeueu(2);
test.enqeueu(6);
assertTrue(test.clear());
}
@Test
void test_getSize() throws MyException {
test.enqeueu(6);
assertEquals(1,test.getsize());
}
}

View File

@ -0,0 +1,83 @@
package QueueStructure.CircularQueue_UsingArray;
public class MyCircularQueue <T> {
private T wert;
private T arr[];
private int erst;// oder (Head, first, begin)
private int last; // oder (back,last,tail,end)
private int size;
private int zaeler = 0; // checke für die länge meiner Queue
MyCircularQueue(int size) {
this.size = size;
arr =(T[]) new Object[size];
initi_werte();
}
public void initi_werte() {
erst = -1;
last = -1;
}
public T enqeueu(T value) throws MyException{
this.wert = value;
if(isEmpty())
erst++;
zaeler++;
last = (last + 1) % size;
arr[last] = value;
return arr[last];
}
public boolean dequeue() {
if(isEmpty())
return false;
zaeler--;
erst = (erst + 1) % size;
erst++;
return true;
}
public T getFront()throws MyException {
if(isEmpty())
throw new MyException("Queue ist leer");
return arr[erst];
}
public T getRear()throws MyException {
if(isEmpty())
throw new MyException("Queue ist leer");
return arr[last];
}
public boolean isFull() {
return last >= arr.length - 1;
}
public boolean isEmpty() {
return erst == -1 && last == -1;
}
public boolean clear() {
if (!isEmpty()) {
initi_werte();
return true;
}
return false;
}
public int getsize() {
if (isEmpty())
return -1;
return zaeler;
}
}

View File

@ -0,0 +1,9 @@
package QueueStructure.CircularQueue_UsingArray;
public class MyException extends Exception {
MyException(String fheler){
super(fheler);
}
}

View File

@ -0,0 +1,43 @@
package QueueStructure;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class JTest {
MyQueue test;
@BeforeEach
void erstelle_List() {
test = new MyQueue(5);
}
@Test
public void test_Enqueue() throws MyException {
assertEquals(10,test.enqueue(10));
assertEquals(120,test.enqueue(120));
}
@Test
public void test_isfull() throws MyException {
test.enqueue(10);
test.enqueue(10);
test.enqueue(10);
test.enqueue(10);
test.enqueue(10);
assertTrue(test.isFull());
}
@Test
public void test_isempty() throws MyException {
assertTrue(test.isEmpty());
}
}

View File

@ -0,0 +1,20 @@
package QueueStructure;
public class Main {
public static void main(String[] args) throws MyException {
MyQueue test = new MyQueue(5);
try {
test.enqueue(10);
test.enqueue(120);
test.enqueue(140);
test.enqueue(140);
test.enqueue(140);
test.printAll();
} catch (MyException e) {
System.out.println(e.toString() + e.getClass());
}
System.out.println(3 % 5);
}
}

View File

@ -0,0 +1,9 @@
package QueueStructure;
public class MyException extends Exception {
MyException(String fheler){
super(fheler);
}
}

View File

@ -0,0 +1,69 @@
package QueueStructure;
public class MyQueue {
int wert;
int erst;
int last;
int arr[];
MyQueue(int size) {
arr = new int[size];
erst = -1;
last = -1;
}
public int enqueue(int value) throws MyException {
if (isFull())
throw new MyException ("Kein Platz mehr in der Queue");
this.wert = value;
if (erst == -1)
erst++;
last++;
arr[last] = wert;
return arr[last];
}
public void deqeue() throws MyException {
if (isEmpty())
throw new MyException ("Queue ist leer!");
if (erst == last) {
erst = -1;
last = -1;
return;
}
erst++;
}
public int peek() throws MyException {
if (isEmpty())
throw new MyException ("Queue ist leer!");
return arr[erst];
}
public boolean isEmpty() {
return last == -1 && erst == -1;
}
public boolean isFull() {
return last >= arr.length-1;
}
public void printAll() throws MyException {
if (isEmpty())
throw new MyException ("Queue ist leer!");
System.out.print("[");
for (int i = erst; i < last; i++)
System.out.print(arr[i] + " ");
System.out.print("]");
}
}