In: Computer Science
JAVA
I need to make it that I can expand the stack for everytime a push operation is performed that would normally cause an overflow, returning a false value. Should accompany 3 nodes initially.
DriverStack.java
public class DriverStack {
public static void main (String[] args)
{
Stack s = new Stack(3);
Listing l;
Listing l1 = new Listing ("Bill", "1st Avenue", "123 4567");
Listing l2 = new Listing ("Alec", "2nd Avenue", "456 4567");
Listing l3 = new Listing ("James", "3rd Avenue", "367 8896");
Listing l4 = new Listing ("Nico", "4th Avenue", "322 1156");
System.out.println(s.pop());
System.out.println(s.push(l1));
System.out.println(s.push(l2));
System.out.println(s.push(l3));
System.out.println(s.push(l4));
System.out.println("\n");
System.out.println("----------------All listings with one over
flow----------------" + "\n");
s.showAll(); //shows listings
System.out.println("----------------Tries to pop the Listing
'1'----------------" + "\n");
//Three pop operations
l = s.pop();
System.out.println(l.toString());
l = s.pop();
System.out.println(l.toString());
l = s.pop();
System.out.println(l.toString());
l = s.pop(); //will try to pop an already empty stack
System.out.println(l);
System.exit(0);
}
}
Stack.java
public class Stack
{
private Listing[] data;
private int top;
private int size;
public Stack()
{
top = -1;
size = 3;
data = new Listing[3];
}
public Stack (int n)
{
top = -1;
size = n;
data = new Listing[n];
}
public void settop()
{
top=-1;
}
public boolean push(Listing newNode)
{
if(top == size - 1)
return false; //overflow
else
{
top = top + 1;
data[top] = newNode.deepCopy();
return true;
}
}
public Listing pop()
{
int topLocation;
if(top == -1)
return null; //underflow error
else
{
topLocation = top;
top = top - 1;
return data[topLocation];
}
}
public void showAll()
{
for (int i = top; i >= 0; i--)
System.out.println(data[i].toString());
}
public void initialStack()
{
top = -1;
}
public boolean StackEmpty()
{
if(top == -1)
return true;
else
return false;
}
public boolean StackFull()
{
if(top == size-1)
return true;
else
return false;
}
public Listing peek()
{
if(StackEmpty())
{
System.out.println("Stack is underflow");
return null;
}
else
{
return data[top];
}
}
}
There is one more class called Listing but I dont think anything needs done with that.
Please comment next to the new code that was added, Thank you!
If you have any doubts, please give me comment...
public class Stack {
private Listing[] data;
private int top;
private int size;
public Stack() {
top = -1;
size = 3;
data = new Listing[3];
}
public Stack(int n) {
top = -1;
size = n;
data = new Listing[n];
}
public void settop()
{
top = -1;
}
public boolean push(Listing newNode) {
if (top == size - 1){
Listing[] temp = new Listing[size+1];
for(int i=0; i<size; i++){
temp[i] = data[i];
}
data = temp;
size++;
}
top = top + 1;
data[top] = newNode.deepCopy();
return true;
}
public Listing pop() {
int topLocation;
if (top == -1)
return null; // underflow error
else {
topLocation = top;
top = top - 1;
return data[topLocation];
}
}
public void showAll() {
for (int i = top; i >= 0; i--)
System.out.println(data[i].toString());
}
public void initialStack() {
top = -1;
}
public boolean StackEmpty()
{
if (top == -1)
return true;
else
return false;
}
public boolean StackFull()
{
if (top == size - 1)
return true;
else
return false;
}
public Listing peek() {
if (StackEmpty()) {
System.out.println("Stack is underflow");
return null;
} else {
return data[top];
}
}
}