-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloatStack.java
More file actions
90 lines (73 loc) · 2.12 KB
/
FloatStack.java
File metadata and controls
90 lines (73 loc) · 2.12 KB
1
public class FloatStack { private float Stack[]; private int StackPosition; public FloatStack() { this.StackPosition = -1; } public FloatStack(int Length) { this.Stack = new float[Length]; this.StackPosition = -1; // StackPosition represents the position of the current element in the stack // -1 means there are no elements. } public void push(float pushme) { if(StackPosition<=Stack.length-1) { if(this.StackPosition == Stack.length-1) { float temp[] = new float[2*Stack.length]; for(int i=0; i<Stack.length; i++) temp[i] = Stack[i]; Stack = temp; Stack[++StackPosition] = pushme; } else Stack[++StackPosition] = pushme; } } public void print_stack() { for(int i=0; i<=StackPosition; i++) System.out.print(Stack[i] + " "); System.out.println(); } public void replace(int position, float pushme) { if(position <= StackPosition) Stack[position] = pushme; else System.out.println("Invalid position. The position is beyond the Stack Position"); } public float access(int position) { if(position <= StackPosition) return Stack[position]; else { System.out.println("Invalid position. cannot return a value returning a zero value"); return 0; } } public boolean check(float Value) { for(int i=StackPosition; i>=0; i--) { if(Stack[i] == Value) return true; } return false; } public boolean check(int position, float Value) { if(position <= StackPosition & position >=0) return (Stack[position] == Value); else { System.out.println("Invalid position. returning a false"); return false; } } public void ChangeStackPosition(int NewPosition) { if( NewPosition < -1 || NewPosition > Stack.length-1 ) System.out.println("Invalid position in the stack. Cannot change to the position."); else StackPosition = NewPosition; } //////// Send -1 as argument to this function to reset the stack. public int noofelements() { return (StackPosition+1); } public int smallestvalue() { int temp =0; for(int i=1; i<=StackPosition; i++) { if(Stack[i]<Stack[temp]) temp = i; } return temp; } }