Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #226

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions malvinseth/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
public class Queue<T> {
// Malvin Seth Osuro; 10909449
private int front;
private int rear;
private int size;
private T[] items;

public Queue(int size) {
this.size = size;
this.items = new T[size];
this.front = 0;
this.rear = 0;
}

public void enqueue(T item) {
if (isFull()) {
throw new IllegalStateException("Queue is full");
}

items[rear] = item;
rear = (rear + 1) % size;
}

public T dequeue() {
if (isEmpty()) {
throw new IllegalStateException("Queue is empty");
}

T item = items[front];
items[front] = null;
front = (front + 1) % size;
return item;
}

public boolean isEmpty() {
return front == rear;
}

public boolean isFull() {
return (rear + 1) % size == front;
}

public T peek() {
if (isEmpty()) {
throw new IllegalStateException("Queue is empty");
}

return items[front];
}

public int size() {
return (rear - front + size) % size;
}
}
11 changes: 11 additions & 0 deletions malvinseth/Queues.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
64 changes: 64 additions & 0 deletions malvinseth/StackArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package Malvin;

import java.util.Arrays;

public class StackArray<S> implements StackINT<S> {
private final static int DEFAULT_CAPACITY = 100;
private int top;
private S[] stack;

public StackArray() {
this(DEFAULT_CAPACITY);
}

@SuppressWarnings("unchecked")
public StackArray(int initialCapacity) {
top = 0;
stack = (S[]) (new Object[initialCapacity]);
}

@Override
public void push(S element) {
if (size() == stack.length)
expandCapacity();
stack[top] = element;
top++;
}

private void expandCapacity() {
stack = Arrays.copyOf(stack, stack.length * 2);
}

@Override
public S pop() {
if (top == 0)
return null;
top--;
S result = stack[top];
stack[top] = null;

return result;
}

@Override
public S peek() {
if (top == 0)
return null;
return stack[top - 1];
}

@Override
public int size() {
return top;
}

@Override
public boolean isEmpty() {
return top == 0;
}

@Override
public void printStack(){
System.out.println(Arrays.toString(stack));
}
}
16 changes: 16 additions & 0 deletions malvinseth/StackINT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package Malvin;

public interface StackINT<S> {
public void push (S element);

public S pop();

public S peek();

public int size();

public boolean isEmpty();

public void printStack();
}