Parcourir la source

modify functions about bubble sort.

171870002 il y a 5 ans
Parent
commit
79fa152c98

+ 21 - 2
src/main/java/com/example/data_structure/controller/StackController.java

@@ -1,11 +1,14 @@
 package com.example.data_structure.controller;
 
 
+import com.example.data_structure.common.Response;
+import com.example.data_structure.domain.Stack;
 import com.example.data_structure.service.StackService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.CrossOrigin;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.ArrayList;
 
 @RestController
 @CrossOrigin
@@ -13,4 +16,20 @@ public class StackController {
 
     @Autowired
     StackService stackService;
+
+    public ArrayList<Integer> emptyContent=new ArrayList<Integer>();
+
+    @GetMapping(value = "/stack/push") //必须要传capacity参数,若用户未指定,则传最大值20,数据传给后端前需要前端判断capacity是否大于20
+    public Response getStackPushResult(@RequestParam int capacity, @RequestParam ArrayList<Integer> content,@RequestParam int value){
+        Stack stack=new Stack(capacity,content);
+        Response re=stackService.getStackPushResult(stack,value);
+        return re;
+    }
+
+    @GetMapping(value = "/stack/pop")
+    public Response getStackPopResult(@RequestParam int capacity,@RequestParam ArrayList<Integer> content){
+        Stack stack=new Stack(capacity,content);
+        Response re=stackService.getStackPopResult(stack);
+        return re;
+    }
 }

+ 24 - 25
src/main/java/com/example/data_structure/domain/Stack.java

@@ -10,29 +10,24 @@ public class Stack {
     public static final int MAX_SIZE =20;
     private int capacity; //栈的最大容量
     private int size; //栈现有的元素数
-    private String elementType; //栈元素的类型,有char,String,int,double四种类型
-    private ArrayList<Object> content;
+    private ArrayList<Integer> content;
 
     /**
      * 指定最大容量的构造方法
      * @param capacity
-     * @param elementType
      * @param content
      */
-    public Stack(int capacity,String elementType, ArrayList<Object> content) {
+    public Stack(int capacity,ArrayList<Integer> content) {
         this.capacity = capacity;
-        this.elementType = elementType;
         this.content = content;
         this.size=content.size();
     }
 
     /**
      * 不指定最大容量的构造方法
-     * @param elementType
      * @param content
      */
-    public Stack( String elementType, ArrayList<Object> content) {
-        this.elementType = elementType;
+    public Stack(ArrayList<Integer> content) {
         this.content = content;
         this.capacity=MAX_SIZE;
         this.size=content.size();
@@ -42,18 +37,25 @@ public class Stack {
     }
 
     private boolean isFull(){
-        return false;
+        return this.capacity==this.size;
     }
+
     private boolean isEmpty(){
-        return true;
+        return size==0 &&capacity>0;
     }
 
     /**
      * push操作成功返回true,否则返回false
      * @return
      */
-    public boolean push(){
-        return false;
+    public boolean push(int value){
+        boolean re =true;
+        if(this.isFull()){
+            re=false;
+        }else{
+            content.add(value);
+        }
+        return re;
     }
 
     /**
@@ -61,11 +63,17 @@ public class Stack {
      * @return
      */
     public boolean pop(){
-        return false;
+        boolean re=true;
+        if(this.isEmpty()){
+            re=false;
+        }else{
+            content.remove(this.size-1);
+        }
+        return re;
     }
 
     public void makeEmpty(){
-
+        content.clear();
     }
 
 
@@ -83,11 +91,7 @@ public class Stack {
         return size;
     }
 
-    public String getElementType() {
-        return elementType;
-    }
-
-    public ArrayList<Object> getContent() {
+    public ArrayList<Integer> getContent() {
         return content;
     }
 
@@ -101,11 +105,7 @@ public class Stack {
         this.size = size;
     }
 
-    public void setElementType(String elementType) {
-        this.elementType = elementType;
-    }
-
-    public void setContent(ArrayList<Object> content) {
+    public void setContent(ArrayList<Integer> content) {
         this.content = content;
     }
 
@@ -117,7 +117,6 @@ public class Stack {
         return "Stack{" +
                 "capacity=" + capacity +
                 ", size=" + size +
-                ", elementType='" + elementType + '\'' +
                 ", content=" + content +
                 '}';
     }

+ 28 - 0
src/main/java/com/example/data_structure/service/StackService.java

@@ -1,9 +1,37 @@
 package com.example.data_structure.service;
 
 
+import com.example.data_structure.common.Response;
+import com.example.data_structure.domain.Stack;
+import com.example.data_structure.vo.StackVO;
+import org.springframework.remoting.rmi.RmiRegistryFactoryBean;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
+
+
 @Service
 public class StackService {
+    private String name; //没有实际意义
+
+    //push
+    public Response getStackPushResult(Stack stack, int value){
+        boolean flag =stack.push(value);
+        if(flag){
+            return Response.buildSuccess(new StackVO(stack.getCapacity(),stack.getContent()));
+        }else{
+            return Response.buildFailure(stack.getContent(),"Error: stack overflow.");
+        }
+    }
+
+    //pop
+    public Response getStackPopResult(Stack stack){
+        boolean flag=stack.pop();
+        if(flag){
+            return Response.buildSuccess(stack);
+        }else{
+            return Response.buildFailure(stack,"Error: the stack is empty.");
+        }
+    }
 }