Created
February 22, 2017 14:00
-
-
Save shreezan123/369b454f6738eda2930359170147618e to your computer and use it in GitHub Desktop.
Stack implementation in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StackOperations{ | |
int mystack[]; | |
int global_size; | |
int counter = 0; | |
int newcounter = 0; | |
int extensionstack[]; | |
public StackOperations(int size){ | |
this.mystack = new int[size]; //create a stack of size as mentioned while creating object | |
this.extensionstack = new int[size*100]; | |
this.global_size = size; //update the global variable | |
} | |
//the below method will allow us to calculate the number of | |
public void push(int value){ | |
if (counter<global_size){ | |
mystack[counter] = value; | |
counter += 1;//we have to do this unless counter < stacksize | |
} | |
else{ | |
/*so if I have to make my code work for user input greater than my initial stack size, | |
I am creating a new method called stackextender where I put new value in a new array called | |
extension stack. Then on the method, mystackmodifier I create a temparray and put contents of | |
original stack and extensionstack in the temparray. At last i do mystack = temparray to | |
copy the contents to my original stack*/ | |
stackextender(value); | |
counter ++; | |
} | |
} | |
//This method puts exceeding values to a new stack called extension stack | |
public void stackextender(int value){ | |
extensionstack[newcounter] = value; | |
newcounter ++; | |
} | |
//This method modifies the contents of original stack | |
public void mystackmodifier(){ | |
int temparray[] = new int[(global_size+newcounter)*2]; | |
if(extensionstack.length>0){ | |
int key; | |
for ( key = 0; key<mystack.length; key++){ | |
temparray[key] = mystack[key]; | |
} | |
for (int i = 0; i<newcounter; i++){ | |
temparray[key+i] = extensionstack[i]; | |
} | |
mystack = temparray; | |
} | |
} | |
public int getSize(){ | |
return counter; | |
} | |
public boolean isempty(){ | |
if (counter==0){ | |
return true; | |
} | |
return false; | |
} | |
public int peek(){ | |
return mystack[counter-1]; | |
} | |
public int pop(){ | |
int lastelem = mystack[counter-1]; | |
int afterpopped[] = new int[counter]; | |
counter -= 1; | |
for (int i = 0; i < counter-1; i+=1){ | |
afterpopped [i] = mystack[i]; | |
} | |
return lastelem; | |
} | |
public String toString(){ | |
String output = ""; | |
for (int i = 0; i<counter; i++){ | |
output += mystack[i] + " "; | |
} | |
return "TOP [ "+ output + "] Bottom"; | |
} | |
} | |
//Here is the driver file | |
package howard.edu.ood.hw2.collections; | |
import java.util.*; | |
public class ArrayStack{ | |
public static void main(String args[]){ | |
StackOperations mystack = new StackOperations(100); | |
System.out.println(mystack.toString()); | |
/*Although the stack size is instantiated as 10, the code works for insertion for as many | |
number of items as demonstrated below*/ | |
for (int i = 0; i<150; i+=1){ | |
mystack.push(i); | |
} | |
int stacksize = mystack.getSize(); | |
if (stacksize == 0) { | |
try{ | |
throw new NoSuchElementException(); | |
}catch (NoSuchElementException exc){ | |
exc.printStackTrace(); | |
} | |
} | |
System.out.println(mystack.toString()); | |
System.out.println("Stack size is "+ mystack.getSize()); | |
System.out.println("Last element is "+mystack.peek()); | |
System.out.println("Popping: "+ mystack.pop()); | |
System.out.println("Stack size is "+ mystack.getSize()); | |
System.out.println("Popping: "+ mystack.pop()); | |
System.out.println("Stack size is "+ mystack.getSize()); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment