Stack and its Methods

 

import java.util.Iterator;
import java.util.Scanner;
import java.util.Stack;

public class StackDemo {

//Method to add element in the Stack
static void stack_Push(Stack<String> stack) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no of elements you want to enter");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
String str = sc.nextLine();

stack.push(str);
}
System.out.println("Push Operation Successful");
System.out.println(n + " element pushed");
}

//Method to delete an element from Stack
static void stack_Pop(Stack<String> stack) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no of elements you want to remove");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
stack.pop();
}
System.out.println("Pop Operation Successful");
System.out.println(n + " element popped");
}

//to get Top Element of Stack
static void stack_Peek(Stack<String> stack) {
System.out.println(stack.peek());
}

//to search an element in the Stack
static void stack_Search(Stack<String> stack) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the element you want to search");
String str = sc.nextLine();
int pos = stack.search(str);
if (pos == -1) {
System.out.println(str + " is not present in the Stack");
} else {
System.out.println(str + " found at position " + pos);
}
}

//to check if Stack is empty or not
static void stack_Empty(Stack<String> stack) {
boolean emp = stack.empty();
if (emp == true) {
System.out.println("Stack is empty");
} else {
System.out.println("Stack is not empty");
}
}

//method to print element of Stack;
static void stack_Print(Stack<String> stack) {
Iterator itr = stack.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}

public static void main(String[] args) {
Stack<String> stack = new Stack<>();
stack_Push(stack);
System.out.println("Elements before popping");
stack_Print(stack);

stack_Pop(stack);
System.out.println("Elements After popping");
stack_Print(stack);
stack_Peek(stack);
stack_Search(stack);
stack_Empty(stack);
}
}

Google Script for Data Entry Form in Google Spreadsheet

// function to validate the entry made by user in user form function validateEntry (){ // declare a variable and referernece of active goog...