Implement your own version of the stack data structure using java with OOPs principles mentioned functionalities: push, pop, peek, contains, size, center, sort reverse, iterator, traverse/print.
Use of similar data structures already present in the language/framework is not allowed.
public class StackProgram {
static class Stack<T>
{
private T[] items;
private int top;
private int capacity;
public Stack()
{
this.capacity = 10000;
this.items = (T[]) new Object[this.capacity];
this.top = -1;
}
public Stack(int capacity)
{
this.capacity = capacity;
this.items = (T[]) new Object[capacity];
this.top = -1;
}
public void Push(T item)
{
if (!IsFull())
{
items[++top] = item;
}
else {
new Exception("Stack is full");
}
}
public T Pop()
{
if (!IsEmpty())
{
return items[top--];
}
else
{
new Exception("Stack is empty");
}
return null;
}
public T Peek()
{
return items[top];
}
public T Center()
{
int centerItem;
if (!IsEmpty())
{
if (Size() % 2 == 0){
System.out.println("The stack has no central element");
return null;
}
else {
centerItem = (Size() / 2);
}
}
else{
System.out.println("Stack is empty");
return null;
}
return items[centerItem];
}
public Boolean Contains(T item) {
for (int i = 0; i < top; i++) {
if (item.equals(items[i])) {
return true;
}
}
return false;
}
public int Size()
{
return top + 1;
}
public Boolean IsEmpty()
{
return top == -1;
}
public Boolean IsFull()
{
return top == capacity - 1;
}
public void Reverse() {
T[] itemsTemp=(T[]) new Object[top+1];
int counter = top;
for (int i = 0; i <= top; i++)
{
itemsTemp[counter] = items[i];
counter--;
}
items = itemsTemp;
}
public void Print()
{
if (IsEmpty())
{
System.out.println("Stack is empty");
}
else {
System.out.print("Items in the stack are:");
for (int i = top; i >= 0; i--)
{
System.out.print(items[i] + " ");
}
System.out.println();
}
}
}
static class StackIterator<T>
{
private Stack<T> currentStack;
public StackIterator(Stack<T> currentStack)
{
this.currentStack = currentStack;
}
public Boolean IsEmpty()
{
return this.currentStack.IsEmpty();
}
public T Pop()
{
return this.currentStack.Pop();
}
}
public static void main(String[] args)
{
try
{
Stack<Integer> stack = new Stack<Integer>(5);
stack.Push(8);
stack.Push(5);
stack.Push(10);
stack.Push(2);
stack.Push(99);
stack.Print();
System.out.println("Stack size is: " + stack.Size());
stack.Reverse();
System.out.println("Reverse");
stack.Print();
System.out.print("Center: " + stack.Center());
System.out.println();
if (stack.Contains(10))
{
System.out.println("Stack contains item 10");
}
else {
System.out.println("Stack does not contain item 10");
}
if (stack.Contains(546))
{
System.out.println("Stack contains item 546");
}
else
{
System.out.println("Stack does not contain item 546");
}
System.out.println("The top item is " + stack.Peek());
System.out.println("Delete the top item: " + stack.Pop());
System.out.println("Stack size is: " + stack.Size());
System.out.print("Center: " + stack.Center());
System.out.println();
StackIterator<Integer> stackIterator = new StackIterator<Integer>(stack);
System.out.println("\nDisplay items using StackIterator");
while (!stackIterator.IsEmpty()) {
System.out.println(stackIterator.Pop());
}
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
Comments
Leave a comment