Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Stack_Backstrom
- {
- static int a[] = new int [50];
- static int top = -1;
- public static void main (String args[])
- {
- Scanner scanner = new Scanner(System.in);
- System.out.println("This program performs various stack operations.");
- while(true)
- {
- System.out.println("Please select one of the numbers below:");
- System.out.println("1.Clear - Clears the contents of the stack.");
- System.out.println("2.IsEmpty - Returns true if the stack is empty, false if it is not.");
- System.out.println("3.IsFull - Returns true if the stack is full, false if it is not.");
- System.out.println("4.Push - Adds an item to the top of the stack.");
- System.out.println("5.Pop - Removes an item from the top of the stack.");
- System.out.println("6.Peek - Returns the value at the top of the stack.");
- System.out.println("7.Exit - Exits the program.");
- int Choice = scanner.nextInt();
- switch(Choice)
- {
- case 1:
- top = -1;
- break;
- case 2:
- isempty();
- break;
- case 3:
- isfull();
- break;
- case 4:
- push();
- break;
- case 5:
- pop();
- break;
- case 6:
- peek();
- break;
- case 7:
- System.exit(0);
- break;
- }
- }
- }
- static void isempty()
- {
- if (top == -1)
- System.out.println("true");
- else System.out.println("false");
- }
- static void isfull()
- {
- if(top == 50)
- System.out.println("true");
- else System.out.println("false");
- }
- static void push()
- {
- if(top == 50)
- System.out.println("Error: Stack is full.");
- else
- {
- top++;
- System.out.println("Please enter the value to be added to the top of the stack:");
- Scanner scanner = new Scanner(System.in);
- int pushval = scanner.nextInt();
- a[top] = pushval;
- }
- }
- static void pop()
- {
- if(top == -1)
- System.out.println("Error: Stack is empty.");
- else
- {
- int popval = a[top];
- top--;
- System.out.println("Value" + " " + popval + " " + "has been popped.");
- }
- }
- static void peek()
- {
- if(top == -1)
- System.out.println("Error: Stack is empty.");
- else
- {
- System.out.println("The value on top of the stack is:" + a[top]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement