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

LabAssign1

The document describes the implementation of a generic stack class called ArrayStack2 in Java, which uses an array to store stack entries. It includes methods for standard stack operations such as push, pop, peek, and checking if the stack is empty, along with dynamic resizing of the array when necessary. Additionally, it provides methods for clearing the stack, displaying its contents, and reducing the array size when it becomes too large.

Uploaded by

kelly
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)
6 views

LabAssign1

The document describes the implementation of a generic stack class called ArrayStack2 in Java, which uses an array to store stack entries. It includes methods for standard stack operations such as push, pop, peek, and checking if the stack is empty, along with dynamic resizing of the array when necessary. Additionally, it provides methods for clearing the stack, displaying its contents, and reducing the array size when it becomes too large.

Uploaded by

kelly
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/ 2

/**

A class of stacks whose entries are stored in an array.


@author Frank M. Carrano
@version 3.0
*/
import java.util.Arrays;
public class ArrayStack2<T> implements StackInterface<T>
{
private T[] stack; // array of stack entries
private int topIndex; // index of top entry
private static final int DEFAULT_INITIAL_CAPACITY = 50;

public ArrayStack2()
{
this(DEFAULT_INITIAL_CAPACITY);
} // end default constructor

public ArrayStack2(int initialCapacity)


{
// the cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] tempStack = (T[])new Object[initialCapacity];
stack = tempStack;
topIndex = -1;
} // end constructor

public void push(T newEntry)


{
ensureCapacity();
topIndex++;
stack[topIndex] = newEntry;
} // end push

private void ensureCapacity()


{ if (topIndex == stack.length - 1) // if array is full, double size of
array
stack = Arrays.copyOf(stack, 2 * stack.length);
} // end ensureCapacity

public T peek()
{
T top = null;
if (!isEmpty())
top = stack[topIndex];
return top;
} // end peek

public T pop()
{
T top = null;

if (!isEmpty()) {
top = stack[topIndex];
stack[topIndex] = null;
topIndex--;
} // end if

if(isTooBig())
reduceArray();
return top;
} // end pop

public boolean isEmpty()


{
return topIndex < 0;
} // end isEmpty

public void clear()


{
for(int i = 0; i <= topIndex; ++i)
stack[i] = null;
topIndex = -1;
}

public void display()


{
if(isEmpty())
System.out.println("The stack is empty");
else {
for(int i=topIndex;i>=0;i--)
System.out.print(stack[i] + " ");
System.out.print("\n");
}
}

public int remove(int n)


{
int counter = 0;
while(!isEmpty()) {
T x = pop();
if(++counter == n)
break;
}
return counter;
}

private boolean isTooBig()


{
return stack.length>=20 && stack.length>2*(topIndex+1);
}

private void reduceArray()


{
stack = Arrays.copyOf(stack, 3 * stack.length / 4);
}

public void printInfo() // FOR TESTING PURPOSES


{
System.out.println(topIndex+1 + " / " + stack.length);
}
} // end ArrayStack

You might also like