A stack is a data structure which is a logical representation of a real physical stack or pile of objects, where insertion and deletion of items takes place at one end called top of the stack. The basic concept can be illustrated by thinking of the data set as a stack of plates or books where you can only take the top item off the stack in order to remove things from it. In a stack last added item is deleted first and so it is called a LIFO (Last In First Out) data structure.
Following is the implementation of a stack in Java programming language by using array. This example has two files MyStack.java and JavaStack.java.
MyStack is a class that contains the function to perform stack operations. Member functions of MyStack are:
1. public boolean push (int value) - To add a new value to the stack. On success, it returns true else it returns false.
2. public int pop () - To take off or pop a value from the stack. On success it returs the poped value else it returns -1
3. public int top () - To access the value at the top of the stack. On success it returns the value at stack top else it returns -1
4. public boolean isFull () - To check whether the stack is full or not.
5. public boolean isEmpty() - To check whether the stack is empty or not
6. public void display() - to display the content of the stack
JavaStack class contains the main function of the program. It uses an instance of MyStack and tests the functions.
Source Code
MyStack.java
public class MyStack {
private int [] data;
private int size;
private int top;
/* Default Constructer */
public MyStack () {
this.size=5; // Default size
this.data = new int [this.size];
top = -1;
}
/* Overloaded Constructer */
public MyStack (int size) {
this.size=size;
data = new int [this.size];
top = -1;
}
/* Function to push a value.*/
public boolean push (int value) {
if (this.isFull()) {
return false;
}
this.top++;
this.data[this.top]=value;
return true;
}
/* Function to pop a value*/
public int pop () {
int value;
if (this.isEmpty()) {
return -1;
}
value = this.data[this.top];
this.top--;
return value;
}
/* Function to get the top value*/
public int top () {
int value;
if (this.top == -1) {
return -1;
}
value = this.data[this.top];
return value;
}
/* Function to check whether stack is full*/
public boolean isFull () {
if (this.top >= this.size-1) {
return true;
}
return false;
}
/* Function to check whether stack is empty */
public boolean isEmpty () {
if (this.top == -1) {
return true;
}
return false;
}
/* Function to display stack content */
public void display () {
if (this.isEmpty()) {
System.out.println("Stack is empty!");
return;
}
for (int i=this.top;i>=0;i--){
System.out.println(this.data[i]);
}
return;
}
}
}
JavaStack.java
Output
How to test the program on your computer?
1. Create MyStack.java file and copy the source code in the section 1.
2. Create JavaStack.java and copy the source code in the section 2.
3. Compile the program by using the command javac JavaStack.java
4. Run the program by using the command java JavaStack
Following is the implementation of a stack in Java programming language by using array. This example has two files MyStack.java and JavaStack.java.
MyStack is a class that contains the function to perform stack operations. Member functions of MyStack are:
1. public boolean push (int value) - To add a new value to the stack. On success, it returns true else it returns false.
2. public int pop () - To take off or pop a value from the stack. On success it returs the poped value else it returns -1
3. public int top () - To access the value at the top of the stack. On success it returns the value at stack top else it returns -1
4. public boolean isFull () - To check whether the stack is full or not.
5. public boolean isEmpty() - To check whether the stack is empty or not
6. public void display() - to display the content of the stack
JavaStack class contains the main function of the program. It uses an instance of MyStack and tests the functions.
Source Code
MyStack.java
public class MyStack {
private int [] data;
private int size;
private int top;
/* Default Constructer */
public MyStack () {
this.size=5; // Default size
this.data = new int [this.size];
top = -1;
}
/* Overloaded Constructer */
public MyStack (int size) {
this.size=size;
data = new int [this.size];
top = -1;
}
/* Function to push a value.*/
public boolean push (int value) {
if (this.isFull()) {
return false;
}
this.top++;
this.data[this.top]=value;
return true;
}
/* Function to pop a value*/
public int pop () {
int value;
if (this.isEmpty()) {
return -1;
}
value = this.data[this.top];
this.top--;
return value;
}
/* Function to get the top value*/
public int top () {
int value;
if (this.top == -1) {
return -1;
}
value = this.data[this.top];
return value;
}
/* Function to check whether stack is full*/
public boolean isFull () {
if (this.top >= this.size-1) {
return true;
}
return false;
}
/* Function to check whether stack is empty */
public boolean isEmpty () {
if (this.top == -1) {
return true;
}
return false;
}
/* Function to display stack content */
public void display () {
if (this.isEmpty()) {
System.out.println("Stack is empty!");
return;
}
for (int i=this.top;i>=0;i--){
System.out.println(this.data[i]);
}
return;
}
}
}
JavaStack.java
public class JavaStack {
public static void main(String[] args) {
MyStack ms = new MyStack();
System.out.println("Pushed 1:" + ms.push(1));
System.out.println("Pushed 2:" + ms.push(2));
System.out.println("Pushed 3:" + ms.push(3));
System.out.println("Pushed 4:" + ms.push(4));
System.out.println("Pushed 5:" + ms.push(5));
System.out.println("Pushed 6:" + ms.push(6));
System.out.println("Is Full:" + ms.isFull());
System.out.println("Stack Content");
ms.display();
System.out.println("Poped:" + ms.pop());
System.out.println("Poped:" + ms.pop());
System.out.println("Poped:" + ms.pop());
System.out.println("Poped:" + ms.pop());
System.out.println("Poped:" + ms.pop());
System.out.println("Poped:" + ms.pop());
ms.display();
}
}
public static void main(String[] args) {
MyStack ms = new MyStack();
System.out.println("Pushed 1:" + ms.push(1));
System.out.println("Pushed 2:" + ms.push(2));
System.out.println("Pushed 3:" + ms.push(3));
System.out.println("Pushed 4:" + ms.push(4));
System.out.println("Pushed 5:" + ms.push(5));
System.out.println("Pushed 6:" + ms.push(6));
System.out.println("Is Full:" + ms.isFull());
System.out.println("Stack Content");
ms.display();
System.out.println("Poped:" + ms.pop());
System.out.println("Poped:" + ms.pop());
System.out.println("Poped:" + ms.pop());
System.out.println("Poped:" + ms.pop());
System.out.println("Poped:" + ms.pop());
System.out.println("Poped:" + ms.pop());
ms.display();
}
}
Output
Pushed 1:true
Pushed 2:true
Pushed 3:true
Pushed 4:true
Pushed 5:true
Pushed 6:false
Is Full:true
Stack Content
5
4
3
2
1
Poped:5
Poped:4
Poped:3
Poped:2
Poped:1
Poped:-1
Stack is empty!
Pushed 2:true
Pushed 3:true
Pushed 4:true
Pushed 5:true
Pushed 6:false
Is Full:true
Stack Content
5
4
3
2
1
Poped:5
Poped:4
Poped:3
Poped:2
Poped:1
Poped:-1
Stack is empty!
How to test the program on your computer?
1. Create MyStack.java file and copy the source code in the section 1.
2. Create JavaStack.java and copy the source code in the section 2.
3. Compile the program by using the command javac JavaStack.java
4. Run the program by using the command java JavaStack
No comments:
Post a Comment