Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class StackX {
- private int maxSize;
- private long[] stackArray;
- private int top;
- private int view;
- public StackX(int s) {
- maxSize = s;
- stackArray = new long[maxSize];
- top = -1;
- view = -1;
- }
- public void push(long j) {
- stackArray[++top] = j;
- }
- public long pop() {
- return stackArray[top--];
- }
- public long view() {
- return stackArray[view--];
- }
- public void setViewToTop() {
- view = top;
- }
- public long peek() {
- return stackArray[top];
- }
- public boolean isEmpty() {
- return (top == -1);
- }
- public boolean isEmptybyView() {
- return (view == -1);
- }
- public boolean isFull() {
- return (top == maxSize - 1);
- }
- }
- class StackApp {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- StackX theStack = new StackX(10);
- int answer = 0;
- while (!(answer == 3)) {
- theStack.setViewToTop();
- System.out.print("[");
- while (!theStack.isEmptybyView()) {
- long value = theStack.view();
- System.out.print(value);
- if (theStack.isEmptybyView()) break;
- System.out.print(" ");
- }
- theStack.setViewToTop();
- System.out.println("]");
- System.out.println("1. Add element");
- System.out.println("2. Remove element");
- System.out.println("3. Exit");
- answer = in.nextInt();
- if (answer == 1) {
- System.out.print("Enter the element: ");
- theStack.push(in.nextInt());
- }
- if (answer == 2) {
- theStack.pop();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment