Stack Implementation Using Arrays

Program


package com.wordpress.sreeharshasite;
import java.util.Arrays;
public class Stack<T> {
private int size=0;
private Object[] array;
private static final int INITIAL_CAPACITY=10;
public Stack()
{
this.array=new Object[INITIAL_CAPACITY];
}
public void push(Object obj)
{
/*To check if the capacity of the array is reached ,
* if yes then length of the array is doubled
*/
if(array.length==size)
{
ensureCapacity();
}
/*if there are no objects add the element at the first index */
if(size==0)
{
array[0]=obj;
size++;
}
/*add the element at the zero index of the array*/
else
{
int index=size-1;
while(index>=0)
{
Object temp=array[index];
array[index+1]=temp;
index--;
}
array[index+1]=obj;
size++;
}
}
/*Removing the first element in the list */
public void pop()
{
if(size<=0)
{
throw new ArrayIndexOutOfBoundsException();
}
else
{
int index=0;
while(index<size-1)
{
array[index]=array[index+1];
index++;
}
size--;
}
}
/*Creating a new array while copying the old elements
* in the array and increasing the length twice*/
public void ensureCapacity()
{
array=Arrays.copyOf(array, array.length*2);
System.out.println("Size capacity after increase-"+array.length);
}
public void print()
{
for(int i=0;i<size;i++)
{
System.out.print(array[i]+" ");
}
System.out.println();
}
public Object peek()
{
int index=0;
if(size>0)
return array[index];
else
return null;
}
}


package com.wordpress.sreeharshasite;
public class StackDemo {
public static void main(String[] args) {
Stack<Integer> stack=new Stack<Integer>();
stack.push(10);
stack.push(9);
stack.push(99);
stack.push(70);
stack.push(65);
stack.push(47);
stack.push(32);
stack.push(23);
stack.push(9);
stack.push(76);
stack.push(54);
stack.push(45);
stack.push(3);
System.out.println("Elements in the Stack After Push");
stack.print();
stack.pop();
System.out.println("Elements in the Stack After Pop");
stack.print();
stack.pop();
System.out.println("Elements in the Stack After Pop");
stack.print();
Integer firstElement=(Integer) stack.peek();
System.out.println("Element at the first position in the stack "+firstElement);
stack.push(130);
System.out.println("Elements in the Stack After Push");
stack.print();
}
}

Output

Size capacity after increase-20
Elements in the Stack After Push
3 45 54 76 9 23 32 47 65 70 99 9 10 
Elements in the Stack After Pop
45 54 76 9 23 32 47 65 70 99 9 10 
Elements in the Stack After Pop
54 76 9 23 32 47 65 70 99 9 10 
Element at the first position in the stack 54
Elements in the Stack After Push
130 54 76 9 23 32 47 65 70 99 9 10

 

Advertisements

I am Sreeharsha from Bengaluru. I am working in java for the past 6+ years. In this blog i will be sharing posts regarding java , advanced java , monthly challenges, collection of quotes , my themes and many more. If you like my posts please follow my blog.

Tagged with: , ,
Posted in Core Java, Data Structures

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow My Diary on WordPress.com
Archives

Enter your email address to follow this blog and receive notifications of new posts by email.

%d bloggers like this: