0% found this document useful (0 votes)
142 views

Oddstack

The document describes an OddStack class that implements a stack data structure with some unique behaviors. The OddStack prioritizes odd numbers, popping them out first even if even numbers were added later. It uses two separate arrays, one for odd numbers and one for even, and combines them for the output string. The class contains push(), pop(), and toString() methods to add/remove numbers and output the stack contents.

Uploaded by

api-547507672
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views

Oddstack

The document describes an OddStack class that implements a stack data structure with some unique behaviors. The OddStack prioritizes odd numbers, popping them out first even if even numbers were added later. It uses two separate arrays, one for odd numbers and one for even, and combines them for the output string. The class contains push(), pop(), and toString() methods to add/remove numbers and output the stack contents.

Uploaded by

api-547507672
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*OddStack.

java
* Author: Seth Cram
* Description:
* A stack that holds ten integers and prioritizes odd number,
* without using a Stack Library. Its output is shown in
* outputOddStack.txt, and W11Main.java is its test file.
*
*Instructions:
* Create a file called OddStack.Java. In it create a class OddStack.
*
* OddStack must have 3 methods:
* public void push(int e)
* public int pop()
* @Override (not required but helps prevent errors)
* public String toString()
*
* OddStack is like most stacks. It takes in integers. The first integer
* pushed in is the last one popped out, but OddStack has one odd feature:
* all the odd integers that are pushed (like 5, 37 or 1) get popped before
* any even integer get popped even if an even integer is pushed after the
* odd ones. Other than that it maintains the order.
* The toString method prints out the elements in the form
* [e1, e2, ... en], but it prints them in the order they would be popped
* (not the order that they were pushed).
* You are free to implement your OddStack any way you want, as long as
* you do not use a Stack library from Java.
* The stack itself holds 10 elements. If you try to push to a full stack
* it must give the error:
* Error: OddStack is full
* If you try to pop form a stack that is empty it must give the error:
* Error: OddStack is empty
* Beyond that they only other thing that it should print is a confirmation
* when you push an element.
* For example push(35) should report:
* Pushing: 35
* Remember, you have limited space for compiling and running your program,
* so do not make your class too complex.
*
* Compile:
* javac -J-Xmx10m -J-Xms5m OddStack.java
* Run:
* java -Xmx10m -Xms5m W11Main
*
* Stack is a FIFO, first in first out, data structure
*
*/

public class OddStack {


private int max_depth; // max size of stack
private int ocurr_depth; //present size of odd array
private int ecurr_depth; //present size of even array
private int[] oddArray = new int[10];//array odd numbers stored in
private int[] evenArray = new int[10];//array even numbers stored in

//constructor
public OddStack(){
//odd and even arrays start with depths of 0
ocurr_depth = 0;
ecurr_depth = 0;
max_depth = 10; //can't hold more than 10 ints
}

//adds number passed in to the top of either the even or odd array
public void push(int num){
System.out.println("Pushing: " + num);

if( ecurr_depth + ocurr_depth == max_depth ){ //if trying to append to a full


stack
System.out.println("Error: OddStack is full");
}
else{ //add number to an array

if( num % 2 == 1 ){ //if number is odd


ocurr_depth++; //add 1 to odd array depth
oddArray[ocurr_depth] = num; //add number to odd array
}else{ //number is even
ecurr_depth++; //adds 1 to even depth
evenArray[ecurr_depth] = num; //add number to even array
}
}
}

//Removes odd numbers in a FIFO manner, then removes even numbers in


// the same manner. Popped value locations aren't scrubbed.
public int pop(){
if( ecurr_depth + ocurr_depth == 0 ){ //if both arrrays empty
System.out.println("Error: OddStack is empty");
return 0; //To signify an error in addition to the print statement
}
else{

if( ocurr_depth != 0 ){ //if still odd numbers left

//removes the need for a temporary variable bc it returns


oddArray[occur_depth], then decrements occur_depth after
return oddArray[ocurr_depth--];
} else{ //only even numbers left
return evenArray[ecurr_depth--];
}
}
}

//Returns stack from right to left in list form. Rightmost number


// is the top of the stack.
@Override //overiding predefined toString() functions
public String toString(){
// new total array to hold combined even and odd arrays to return
int totArraySize = ocurr_depth + ecurr_depth; //needed in for loop
int[] totArray = new int[totArraySize];

//placeholders for odd and even depth so method caller's odd and deven depths
aren't altered
int oddPH = ocurr_depth;
int evenPH = ecurr_depth;

String completeList; //string to return the completed list of what's in


OddStack
//combines even and odd arrays into totArray
while( oddPH + evenPH > 0 ){
if( oddPH > 0 ){ //copy over odd array
totArray[oddPH+evenPH-1] = oddArray[oddPH]; //have to subtract 1 bc if
totArray[0] not overwritten, it outputs a 0
oddPH--; //decremented to move onto next odd value
}else{ //copy over even array
totArray[evenPH-1] = evenArray[evenPH];
evenPH--;
}
}

// appends totArray's entries onto completeList string


completeList = "[";
for(int i = 0; totArraySize > i; i++){
completeList = completeList + totArray[i];

if( i != totArraySize-1){ //subtract 1 so don't add comma after final


element
completeList = completeList + ","; //commas to separate entries
}

}
completeList = completeList + "]";

return completeList; //return completeList string in the form of a list


}
}

You might also like