In: Computer Science
Write a program that prompts the user to insert an item into a stack, delete an item from the stack, or display all the items in the stack. Assume that the stack maximum capacity is 10. Here is sample run
. Stack operation menu:
1. Insert
2. Delete
3. Display
4. Quit
Stack.java
/*** The following implements functionalities for the
user to insert
* an item into a stack, delete an item from the stack,
* or display all the items in the stack. */
import java.util.Scanner;
public class Stack {
/** maxCapacity specifies the maximum size of the stack array
*/
int maxCapacity;
/** stack[] is the integer array for storing the items of the stack
*/
int stack[];
/** top always specifies the position of last element added to the
stack
* and which is also the position of first item popped out of the
stack */
int top;
/** Constructor to initialize the maxCapactiy of the
stack. Also top is
* initialized with -1 to specify empty stack.
*/
public Stack(int size)
{
maxCapacity = size;
stack = new int[maxCapacity];
top = -1;
}
/** isFull() method checks if all the array positions got filled up
with items.
* It is identified by checking the top position with the
maxCapacity.
* If it is equal, true is returned otherwise
* false is returned.
*/
public boolean isFull()
{
if (top < maxCapacity )
return false;
else
return true;
}
/** isEmpty() checks if the stack is empty. This is done by
checking it top=-1
*/
public boolean isEmpty()
{
if(top == -1)
return true;
else
return false;
}
/** Push method is used to insert an item into the stack. It calls
isFull() method to
* check if stack is full. If so, it displays error message.
* If stack is not full, increment the top position by 1 and insert
the new element in the
* postion of top.
*/
public void push(int item) {
if(isFull()==true) {
System.out.println("Stack Overflow !");
}
else
{
top++;
stack[top] = item;
System.out.println("Item " + item + " is pushed onto Stack
!");
}
}
/** pop method is used to take the item at the top
location and decrement the value of top by 1
* Pop method should check for items in the stack. It does that by
calling isEmpty() function.
* If isEmpty() returns false, it means that there are items in the
stack and pop can be done
*/
public int pop() {
int item;
if (isEmpty() == false) {
item = stack[top];
top--;
return(item);
} else {
return -1;
}
}
/** The method is used to print the elements of the stack. A simple
for loop is used to read
* teh elements of the array and print the stack contents. The
contents are printed in the
* reverse order just to show the Last Inserted is Found First
(LIFO) of stack.
*/
public void displayStack() {
if (isEmpty() == false) {
System.out.println("\nItems in stack are :");
for (int i = top; i >= 0; i--) {
System.out.println(stack[i]);
}
} else {
System.out.println("\nStack Underflow !No more items in the
stack");
}
}
/** Driver method to create an object of Stack class and
call all the methods of stack */
public static void main(String[] args) {
Stack stk = new Stack(10);
Scanner sc = new Scanner(System.in);
int item;
int choice;
do
{
System.out.println("1. Insert an item onto stack");
System.out.println("2. Delete an item from stack");
System.out.println("3. Display items of stack");
System.out.println("4. Exit");
System.out.print("Enter your choice :: ");
choice = sc.nextInt();
switch(choice)
{
case 1:
System.out.print("Type the item to push into the stack :: ");
item = sc.nextInt();
stk.push(item);
break;
case 2:
item = stk.pop();
if(item!=-1)
System.out.println("Item " + item + " is popped off from
stack");
else
System.out.println("No more items in the stack");
break;
case 3:
System.out.println("\nContents of the stack");
stk.displayStack();
break;
case 4:
System.out.println("Thank you!!!");
break;
default:
System.out.println("Invalid Option");
}
}while(choice!=4);
}
}
==================================================================================
===========================================================================================
Sample output