Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner; // for user input
- public class StackNames {
- public static void main (String[] args) {
- final int n = 10;
- Stack stack = new Stack(n); // size of stack is 10 in this case
- Scanner scan = new Scanner(System.in);
- for (int i=0; i<n; i++) {
- System.out.print("Enter name ["+(i+1)+"]: ");
- String s = scan.nextLine();
- stack.push(s);
- }
- while (!stack.isEmpty()) {
- System.out.println( stack.pop()); // print 10 names removed
- }
- } // end main()
- } // end class StackNames
- class Stack {
- private static final int DEFAULT_SIZE = 10;
- private static int size;
- private static int top;
- private static String[] values;
- public Stack() {
- this (DEFAULT_SIZE);
- }
- public Stack(int size) {
- this.size = size;
- values = new String[size];
- top = -1;
- }
- public static boolean isFull() {
- if (top < size - 1) {
- return false;
- } else {
- return true;
- }
- }
- public static boolean isEmpty() {
- if (top == - 1) {
- return true;
- } else {
- return false;
- }
- }
- public static void push(String x) {
- if (!isFull()) {
- top++;
- values[top] = x;
- }
- }
- public static String pop() {
- String netVal = "";
- if (!isEmpty()) {
- netVal = values[top];
- top--;
- }
- return netVal;
- }
- } // end class Stack
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement