Commit 178073fd authored by michael lundquist's avatar michael lundquist
Browse files

Adding method shells from the assignment

parent 9679b5ee
Pipeline #4110 failed with stage
in 1 minute and 21 seconds
......@@ -25,10 +25,12 @@ public class Application {
}
/**
* validates the brackets of a char[]
* validates the brackets of a mathematical expression
*
* @param input a char[] to validate
* @param input a char[] math expression to validate
* @return true if it's valid, else false
*/
//TODO: test this
public static boolean isValid(char[] input) {
Stack s = new Stack();
......
......@@ -81,5 +81,26 @@ public class ArrayQueue {
data = trimmedArray;
}
}
/**
* Inserts the specified element into this queue if it is possible to do
* so immediately without violating capacity restrictions. When using a
* capacity-restricted queue, this method is generally preferable to
* add(Object), which can fail to insert an element only by throwing
* an exception
*
* @param e the element to add
* @return true if the elementwas added to this queue, else false
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this queue
* @throws NullPointerException if the specified element is null and this queue does not permit null
* elements
* @throws IllegalArgumentException -if some property of this element
* prevents it from being added to this queue
*/
//TODO write this
public boolean offer(Object e){
return false;
}
}
\ No newline at end of file
......@@ -20,6 +20,31 @@ public class ArrayStack {
manyItems = 0;
data = new Object[initialCapacity];
}
/**
* Returns the 1-based position where an object is on this stack. If the
* object o occurs as an item in this stack, this method returns the
* distance from the top of the stack of the occurrence nearest the top
* of the stack; the topmost item on the stack is considered to beat
* distance 1. The equals method is used to compare o to the items in
* this stack.
*
* @param o the desired object
* @return the 1-based position from the top of the stack where the
* object is located; the return value -1 indicates that the object
* is not on the stack.
* @author Michael Lundquist
*/
//TODO
public int search(Object o){
int distance = 1;
//search for the element and increment distance
if(distance <= data.size()){
return distance;
}else{
return -1;
}
}
public void ensureCapacity(int minimumCapacity){
......
......@@ -2,6 +2,7 @@ package com.ds.fifthassignment;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
public class LinkedListQueue implements Iterable{
//JAVA-LinkedList implementation of Queue.
......@@ -38,7 +39,18 @@ public class LinkedListQueue implements Iterable{
}
}
/**
* Retrieves, but does not remove, the head of this queue.
* This method throws an exception if this queue is empty.
*
* @return the head of this queue
* @throws NoSuchElementException if the queue is empty
*/
//TODO make sure this works.
public Object element() throws NoSuchElementException{
return head;
}
public boolean isEmpty() {
return ( head==null ? true : false);
......
......@@ -5,14 +5,16 @@ import java.util.LinkedList;
import javax.management.RuntimeErrorException;
import java.util.EmptyStackException;
public class LinkedListStack implements Iterable{
//JAVA-LinkedList implementation of Stack.
private LinkedList data;
private Object top;
public LinkedListStack() {
data= new LinkedList();
top=null;
data = new LinkedList();
top = null;
}
public void push(Object x) {
......@@ -24,15 +26,55 @@ public class LinkedListStack implements Iterable{
if(data.isEmpty()) {
throw new RuntimeException("StackUnderflow!");
}
else { Object out = top;
data.removeLast();
if(!data.isEmpty()) top = data.getLast();
else top = null;
return out;
else {
Object out = top;
data.removeLast();
if(!data.isEmpty()) top = data.getLast();
else top = null;
return out;
}
}
/**
* Looks at the object at the top of this stack without
* removing it from the stack
*
* @return the object at the top of the stack
* @author Michael Lundquist
* @throws EmptyStackException if the stack is empty
*/
//TODO make sure this works
public Object peek() throws EmptyStackException{
if(top == null){
throw new EmptyStackException();
}
return top;
}
/**
* Returns the 1-based position where an object is on this stack. If the
* object o occurs as an item in this stack, this method returns the
* distance from the top of the stack of the occurrence nearest the top
* of the stack; the topmost item on the stack is considered to beat
* distance 1. The equals method is used to compare o to the items in
* this stack.
*
* @param o the desired object
* @return the 1-based position from the top of the stack where the
* object is located; the return value -1 indicates that the object
* is not on the stack.
* @author Michael Lundquist
*/
//TODO make sure this works
public int search(Object o){
int distance = 1;
//search for the element and increment distance
if(distance <= data.size()){
return distance;
}else{
return -1;
}
}
public boolean isEmpty() {
return ( top==null ? true : false);
......@@ -41,6 +83,4 @@ public class LinkedListStack implements Iterable{
public Iterator iterator() {
return data.iterator();
}
}
\ No newline at end of file
package com.ds.fifthassignment;
import java.util.NoSuchElementException;
//linkedlist implementation of Queue developed from scratch.
public class Queue {
......@@ -26,6 +28,18 @@ public class Queue {
tail = node; //updating the tail
}
}
/**
* Retrieves, but does not remove, the head of this queue.
* This method throws an exception if this queue is empty.
*
* @return the head of this queue
* @throws NoSuchElementException if the queue is empty
*/
//TODO make sure this works
public Object element() throws NoSuchElementException{
return head;
}
public Object remove(){
......
package com.ds.fifthassignment;
import java.util.EmptyStackException;
//linkedlist implementation of Stack developed from scratch.
/**
* Taken from class
*
* @author Michael Lundquist G00737340
* email: mlundqu2@masonlive.gmu.edu
*/
public class Stack {
private Node top;
......@@ -23,7 +32,47 @@ public class Stack {
top = node;
}
}
/**
* Looks at the object at the top of
* this stack without removing it from the stack
*
* @return the object at the top of the stack
* @author Michael Lundquist
* @throws EmptyStackException if the stack is empty
*/
//TODO make sure this work
public Object peek() throws EmptyStackException{
if(top == null){
throw new EmptyStackException();
}
return top.getData();
}
/**
* Returns the 1-based position where an object is on this stack. If the
* object o occurs as an item in this stack, this method returns the
* distance from the top of the stack of the occurrence nearest the top
* of the stack; the topmost item on the stack is considered to beat
* distance 1. The equals method is used to compare o to the items in
* this stack.
*
* @param o the desired object
* @return the 1-based position from the top of the stack where the
* object is located; the return value -1 indicates that the object
* is not on the stack.
* @author Michael Lundquist
*/
//TODO
public int search(Object o){
int distance = 1;
//search for the element and increment distance
if(distance <= data.size()){
return distance;
}else{
return -1;
}
}
public Object pop(){
if(this.isEmpty()) return -1;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment