Stack Implementation
Stack Implementation in java : there can be three approach to implement stack.
1) Array based
2) Dynamic array based
3) Linked list base
1) Array based Stack implementation:
package stack;
public class Stack
{
int data ;
int top;
int size;
int[] array;
Stack()
{
this.top = -1;
this.size = 20;
array = new int[this.size];
}
Stack(int value, int size)
{
this.data = value;
this.top = -1;
this.size = size;
array = new int[this.size];
}
boolean push(int node)
{
if (this.stackIsFull())
{
return false;
}
array[++this.top] = node;
return true;
}
int pop()
{
if (this.empty())
{
return 0;
}
return array[this.top--];
}
int peek()
{
if (this.empty())
{
return 0;
}
return array[this.top];
}
void delete()
{
this.top = -1;
}
boolean empty()
{
return this.top == -1;
}
boolean stackIsFull()
{
return (this.top == this.size - 1);
}
public static void main(String [] arg)
{
System.out.println("Stack implementation using array");
Stack obj = new Stack();
obj.push(2);
obj.push(3);
obj.push(4);
obj.push(5);
obj.push(6);
obj.push(7);
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.peek());
System.out.println("Stack Element " + obj.peek());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
}
}
2) Stack Implementation using dynamic array
package stack;
public class Stack
{
int data ;
int top;
int size;
int[] array;
Stack()
{
this.top = -1;
this.size = 5;
array = new int[this.size];
}
Stack(int value, int size)
{
this.data = value;
this.top = -1;
this.size = size;
array = new int[this.size];
}
void doubleStack()
{
int[] newarray = new int[this.size * 2];
System.arraycopy(this.array, 0, newarray, 0, this.size);
this.size = this.size * 2;
this.array = newarray;
}
boolean push(int node)
{
if (this.stackIsFull())
{
this.doubleStack();
}
array[++this.top] = node;
return true;
}
int pop()
{
if (this.empty())
{
return 0;
}
return array[this.top--];
}
int peek()
{
if (this.empty())
{
return 0;
}
return array[this.top];
}
void delete()
{
this.top = -1;
}
boolean empty()
{
return this.top == -1;
}
boolean stackIsFull()
{
return (this.top == this.size - 1);
}
public static void main(String [] arg)
{
System.out.println("Stack implementation using array");
Stack obj = new Stack();
obj.push(2);
obj.push(3);
obj.push(4);
obj.push(5);
obj.push(6);
obj.push(7);
obj.push(8);
obj.push(9);
obj.push(01);
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
}
}
### Reverse the Stack elements:
void pushAtBottom(int data)
{
if (this.empty())
{
this.push(data);
return;
}
int temp = this.pop();
pushAtBottom(data);
this.push(temp);
}
void reverseStack()
{
if (this.empty())
{
return;
}
int temp = this.pop();
this.reverseStack();
this.pushAtBottom(temp);
}
1) Array based
2) Dynamic array based
3) Linked list base
1) Array based Stack implementation:
package stack;
public class Stack
{
int data ;
int top;
int size;
int[] array;
Stack()
{
this.top = -1;
this.size = 20;
array = new int[this.size];
}
Stack(int value, int size)
{
this.data = value;
this.top = -1;
this.size = size;
array = new int[this.size];
}
boolean push(int node)
{
if (this.stackIsFull())
{
return false;
}
array[++this.top] = node;
return true;
}
int pop()
{
if (this.empty())
{
return 0;
}
return array[this.top--];
}
int peek()
{
if (this.empty())
{
return 0;
}
return array[this.top];
}
void delete()
{
this.top = -1;
}
boolean empty()
{
return this.top == -1;
}
boolean stackIsFull()
{
return (this.top == this.size - 1);
}
public static void main(String [] arg)
{
System.out.println("Stack implementation using array");
Stack obj = new Stack();
obj.push(2);
obj.push(3);
obj.push(4);
obj.push(5);
obj.push(6);
obj.push(7);
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.peek());
System.out.println("Stack Element " + obj.peek());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
}
}
2) Stack Implementation using dynamic array
package stack;
public class Stack
{
int data ;
int top;
int size;
int[] array;
Stack()
{
this.top = -1;
this.size = 5;
array = new int[this.size];
}
Stack(int value, int size)
{
this.data = value;
this.top = -1;
this.size = size;
array = new int[this.size];
}
void doubleStack()
{
int[] newarray = new int[this.size * 2];
System.arraycopy(this.array, 0, newarray, 0, this.size);
this.size = this.size * 2;
this.array = newarray;
}
boolean push(int node)
{
if (this.stackIsFull())
{
this.doubleStack();
}
array[++this.top] = node;
return true;
}
int pop()
{
if (this.empty())
{
return 0;
}
return array[this.top--];
}
int peek()
{
if (this.empty())
{
return 0;
}
return array[this.top];
}
void delete()
{
this.top = -1;
}
boolean empty()
{
return this.top == -1;
}
boolean stackIsFull()
{
return (this.top == this.size - 1);
}
public static void main(String [] arg)
{
System.out.println("Stack implementation using array");
Stack obj = new Stack();
obj.push(2);
obj.push(3);
obj.push(4);
obj.push(5);
obj.push(6);
obj.push(7);
obj.push(8);
obj.push(9);
obj.push(01);
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
System.out.println("Stack Element " + obj.pop());
}
}
### Reverse the Stack elements:
void pushAtBottom(int data)
{
if (this.empty())
{
this.push(data);
return;
}
int temp = this.pop();
pushAtBottom(data);
this.push(temp);
}
void reverseStack()
{
if (this.empty())
{
return;
}
int temp = this.pop();
this.reverseStack();
this.pushAtBottom(temp);
}
Comments
Post a Comment