用java模拟算法_栈

294 阅读1分钟

算法-1:栈

  1. push(入栈)

  2. pop(出栈)

  3. top(栈顶)

  4. 特点:后进先出

  5. 题目:请你实现一个栈。

    • push x:将 加x 入栈,保证 x为 int 型整数。
    • pop:输出栈顶,并让栈顶出栈
    • top:输出栈顶,栈顶不出栈
  6. 代码

 import java.util.Scanner;
 ​
 /**
  * @author : JiangShan
  * @version : 1.0
  * @ClassName :
  * @Date : Created in 13:46 2022/5/29
  * 栈
  * 请你实现一个栈。
  * 操作:
  * push x:将 加x 入栈,保证 x为 int 型整数。
  * pop:输出栈顶,并让栈顶出栈
  * top:输出栈顶,栈顶不出栈
  */
 ​
     public class Main {
         public static void main(String... args) {
             // 获取输入
             Scanner sc = new Scanner(System.in);
             //valueof() 返回指定对象原始值
             int size = Integer.valueOf(sc.nextLine());
             JimStack stack = new JimStack(size);
             // 判断输入
             String tempStr;
             String[] tempArr;
             while (sc.hasNextLine()) {
                 tempStr = sc.nextLine();
                 tempArr = tempStr.split(" ");
                 // 是否通过输入校验
                 boolean isSuccess = isPrintSuccess(tempArr);
                 if (!isSuccess) continue;
 ​
                 if ("push".equals(tempArr[0])) {
                     stack.push(tempArr[1]);
                     continue;
                 }
                 if ("pop".equals(tempArr[0])) {
                     stack.pop();
                     continue;
                 }
                 if ("top".equals(tempArr[0])) {
                     stack.top();
                     continue;
                 }
             }
 ​
             sc.close();
         }
 ​
         /**** 判断输入是否通过验证 **/
         public static boolean isPrintSuccess(String[] printArr) {
             if (printArr.length == 0) {
                 System.out.println("error");
                 return false;
             }
             String first = printArr[0];
             if ("pop".equals(first) || "push".equals(first) ||
                     "top".equals(first) ) return true;
             // 其他情况不通过验证
             System.out.println("error");
             return false;
         }
     }
 ​
     // 自定义实现一个栈
     class JimStack {
         int maxSize;
         int top = -1;
         int[] data;
 ​
         public void push(String value) {
             top ++;
             data[top] = Integer.valueOf(value);
         }
 ​
         public void pop() {
             if (top == -1){
                 System.out.println("error");
                 return;
             }
             int value = data[top];
             System.out.println(value);
             top--;
         }
 ​
         public void top() {
             if (top == -1) {
                 System.out.println("error");
                 return ;
             }
             System.out.println(data[top]);
         }
 ​
         public JimStack(int maxSize) {
             this.maxSize = maxSize;
             this.data = new int[maxSize];
         }
     }
 ​
 ​
 ​
 ​

题目详解转自牛客网用户 “牛客353928144号”,最重要的是将后进先出 以及top(栈顶),pop(出栈),push(入栈)的理念用算法表现出来,而且用有参构造器圈定了数组长度,nb!!

  class JimStack {
         int maxSize;
         int top = -1;
         int[] data;
 ​
         public void push(String value) {
             top ++;
             data[top] = Integer.valueOf(value);
         }
 ​
         public void pop() {
             if (top == -1){
                 System.out.println("error");
                 return;
             }
             int value = data[top];
             System.out.println(value);
             top--;
         }
 ​
         public void top() {
             if (top == -1) {
                 System.out.println("error");
                 return ;
             }
             System.out.println(data[top]);
         }
 ​
         public JimStack(int maxSize) {
             this.maxSize = maxSize;
             this.data = new int[maxSize];
         }
     }