Java 데이터 구조와 알고리즘의 창고(동력 노드 Java 학원 정리)

스택, 중국어 번역은 창고, 사실 창고, heap, 더미를 가리킨다.여기서 말하는 것은 데이터 구조의 창고이지 메모리 분배 안의 더미와 창고가 아니다.
창고는 선진적으로 나온 데이터의 구조로 마치 네 접시가 하나하나 쌓여 있는 것과 같다. 마지막에 놓은 것은 맨 위에 쌓여 있는 것과 같다.
대열은 줄을 서서 사과를 사는 것이고, 먼저 가는 것은 먼저 살 수 있다.
창고

public class Stack { 
   private int array[]; 
   private int max; 
   private int top; 
   public Stack(int max){ 
     this.max = max; 
     array = new int[max]; 
     top = 0; 
   } 
   public void push(int value){ 
     if(isFull()){ 
       System.out.println("full,can not insert"); 
       return; 
     } 
     array[top++]=value; 
   } 
   public int pop(){ 
     return array[--top]; 
   } 
   public boolean isEmpty(){ 
     if(top == 0){ 
       return true; 
     } 
     return false; 
   } 
   public boolean isFull(){ 
     if(top == max ){ 
       return true; 
     } 
     return false; 
   } 
   public void display(){ 
     while(!isEmpty()){ 
       System.out.println(pop()); 
     } 
   } 
   public static void main(String[] args) { 
     Stack s = new Stack(5); 
     s.push(1); 
     s.push(3); 
     s.push(5); 
     s.push(5); 
     s.push(5); 
     s.display(); 
   } 
 } 
사실 top을 -1로 설정하는 것이 계산하기 쉽다고 생각한다. 이곳의 i++와++i를 기억해라. 만약에 i=1이라면array[i++]=2,array[1]=2를 가리킨다. 다음에 i를 사용할 때 i의 값이 2가 되고++i는 i=2를 직접 사용하는 것이다.
top는 0을 가리킨다. 매번 push에 원소를 하나 추가하기 때문에 마지막 원소에 추가할 때 top=max.선진적인 후출이기 때문에 먼저 나온 것은 마지막에 들어간 것이고 방금 top-1이 있는 위치이다.
올바른 출력:
 5 
 5 
 5 
 3 
 1 
1. 창고의 사용 - 단어 역순.

 public String reverse(String in){ 
     String out=""; 
     for (int i = 0; i < in.length(); i++) { 
       char c = in.charAt(i); 
       push(c); 
     } 
     while(!isEmpty()){ 
       out+=pop(); 
     } 
     return out; 
   } 
   public static void main(String[] args) { 
     Scanner s = new Scanner(System.in); 
     String string = s.nextLine(); 
     Stack st = new Stack(string.length()); 
     System.out.println(st.reverse(string));      
   } 
Stack의 배열 유형을 char로 변경하면 됩니다.
읽기 입력은 입출력으로 읽을 수도 있습니다.

 public static void main(String[] args) { 
   InputStreamReader is = new InputStreamReader(System.in); 
   BufferedReader b = new BufferedReader(is); 
   String string=""; 
   try { 
     string = b.readLine(); 
   } catch (IOException e) { 
     e.printStackTrace(); 
   } 
   Stack st = new Stack(string.length()); 
   System.out.println(st.reverse(string)); 
 } 
2. 창고의 사용 - 구분 기호가 일치합니다.

 public int charat(char c){ 
   for (int i = 0; i < array.length; i++) { 
     if(c == array[i]) 
       return i; 
   } 
   return array.length; 
 } 
 public void match(String in){ 
   String out=""; 
   for (int i = 0; i < in.length(); i++) { 
     char c = in.charAt(i); 
     if(c == '{' || c == '(' || c == '[' ){ 
       push(c); 
     } 
     if(c == '}' || c == ')' || c == ']'){ 
       char temp = pop(); 
      if(c == '}' && temp != '{'|| c == ')' && temp != '('|| c == ']' && temp != ']'){ 
         System.out.println("can not match in "+i); 
       } 
     } 
   } 
   while(!isEmpty()){ 
     char c = pop(); 
     if(c == '{'){ 
       System.out.println("insert } to match "+charat(c)); 
     } 
     if(c == '[' ){ 
       System.out.println("insert ] to match "+charat(c)); 
     } 
     if(c == '(' ){ 
       System.out.println("insert ) to match "+charat(c)); 
     } 
   } 
 } 
 public static void main(String[] args) { 
   Scanner s = new Scanner(System.in); 
   String string = s.nextLine(); 
   Stack st = new Stack(string.length()); 
   st.match(string); 
 } 
 result: 
 klsjdf(klj{lkjjsdf{) 
 can not match in 19 
 insert } to match 1 
 insert ) to match 0 
({[먼저 창고에 눌러서 만납니다)}]를 팝업 요소와 비교하고 일치하면 일치합니다.만약 계속 없다면)}], 마지막에 창고의 왼쪽 기호가 튀어나올 것입니다. 어떤 위치에 있는지, 부족한 구체적인 오른쪽 기호 형식을 알려 줍니다.
이것은 창고로 실현할 수 있는 도구다.
창고에 있는 데이터가 창고에 들어가는 시간과 창고에 나가는 시간의 복잡도는 상수 O(1)이다. 데이터 개수와 무관하게 직접 눌러서 튀어나오기 때문에 조작 시간이 짧고 장점이 바로 여기에 있다. 만약에 현실 생활의 사용은 선진적인 순서만 사용하고 출입 데이터의 비교만 하면 창고를 사용할 수 있다.
위에서 말한 것은 여러분이 소개한 자바 데이터 구조와 알고리즘의 창고(동력 노드 자바 학원 정리)입니다. 여러분께 도움이 되었으면 합니다. 만약에 궁금한 점이 있으면 저에게 메시지를 남겨 주시면 제때에 답장해 드리겠습니다.여기에서도 저희 사이트에 대한 지지에 감사드립니다!

좋은 웹페이지 즐겨찾기