您的位置:首页 > 理论基础 > 数据结构算法

Java数据结构与算法之栈_动力节点Java学院整理

2017-09-25 16:22 716 查看
 
stack,中文翻译为堆栈,其实指的是栈,heap,堆。这里讲的是数据结构的栈,不是内存分配里面的堆和栈。

栈是先进后出的数据的结构,好比你碟子一个一个堆起来,最后放的那个是堆在最上面的。

队列就是排队买苹果,先去的那个可以先买。



1. public class Stack {  

2.     private int array[];  
3.     private int max;  

4.     private int top;  
5.     public Stack(int max){  

6.         this.max = max;  
7.         array = new int[max];  

8.         top = 0;  
9.     }  

10.     public void push(int value){  
11.         if(isFull()){  

12.             System.out.println("full,can not insert");  
13.             return;  

14.         }  
15.         array[top++]=value;  

16.     }  
17.     public int pop(){  

18.         return array[--top];  
19.     }  

20.     public boolean isEmpty(){  
21.         if(top == 0){  

22.             return true;  
23.         }  

24.         return false;  
25.     }  

26.     public boolean isFull(){  
27.         if(top == max ){  

28.             return true;  
29.         }  

30.         return false;  
31.     }  

32.     public void display(){  
33.         while(!isEmpty()){  

34.             System.out.println(pop());  
35.         }  

36.     }  
37.     public static void main(String[] args) {  

38.         Stack s = new Stack(5);  
39.         s.push(1);  

40.         s.push(3);  
41.         s.push(5);  

42.         s.push(5);  
43.         s.push(5);  

44.         s.display();  
45.     }  

46. }  
其实还是觉得设置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所在的位置。

正确输出:

1. 5  

2. 5  
3. 5  

4. 3  
5. 1  

一、栈的使用——单词逆序。

1. public String reverse(String in){  

2.         String out="";  
3.         for (int i = 0; i < in.length(); i++) {  

4.             char c = in.charAt(i);  
5.  
4000
           push(c);  

6.         }  
7.         while(!isEmpty()){  

8.             out+=pop();  
9.         }  

10.         return out;  
11.     }  

12.     public static void main(String[] args) {  
13.         Scanner s = new Scanner(System.in);  

14.         String string = s.nextLine();  
15.         Stack st = new Stack(string.length());  

16.         System.out.println(st.reverse(string));  
17.           

18.     }  
将Stack的数组类型改为char即可。

读取输入也可以用IO读取。

1. public static void main(String[] args) {  

2.     InputStreamReader is = new InputStreamReader(System.in);  
3.     BufferedReader b = new BufferedReader(is);  

4.     String string="";  
5.     try {  

6.         string = b.readLine();  
7.     } catch (IOException e) {  

8.         e.printStackTrace();  
9.     }  

10.     Stack st = new Stack(string.length());  
11.     System.out.println(st.reverse(string));  

12. }  
二、栈的使用——分隔符匹配。

1. public int charat(char c){  

2.     for (int i = 0; i < array.length; i++) {  
3.         if(c == array[i])  

4.             return i;  
5.     }  

6.     return array.length;  
7. }  

8. public void match(String in){  
9.     String out="";  

10.     for (int i = 0; i < in.length(); i++) {  
11.         char c = in.charAt(i);  

12.         if(c == '{' || c == '(' || c == '[' ){  
13.             push(c);  

14.         }  
15.         if(c == '}' || c == ')' || c == ']'){  

16.             char temp = pop();  
17.             if(c == '}' && temp != '{'|| c == ')'  && temp != '('|| c == ']' && temp != ']'){  

18.                 System.out.println("can not match in "+i);  
19.             }  

20.         }  
21.     }  

22.     while(!isEmpty()){  
23.         char c = pop();  

24.         if(c == '{'){  
25.             System.out.println("insert } to match "+charat(c));  

26.         }  
27.         if(c == '[' ){  

28.             System.out.println("insert ] to match "+charat(c));  
29.         }  

30.         if(c == '(' ){  
31.             System.out.println("insert ) to match "+charat(c));  

32.         }  
33.     }  

34. }  
35. public static void main(String[] args) {  

36.     Scanner s = new Scanner(System.in);  
37.     String string = s.nextLine();  

38.     Stack st = new Stack(string.length());  
39.     st.match(string);  

40. }  
41.   

42. result:  
43. klsjdf(klj{lkjjsdf{)  

44. can not match in 19  
45. insert } to match 1  

46. insert ) to match 0  

将({[先压入栈,一旦遇到)}]便与弹出的元素比较,若吻合,则匹配。如果一直没有)}],最后便会弹出栈的左符号,提示是在具体哪个位置,缺少的具体的右符号类型。

这是可以用栈来实现的工具。

栈中数据入栈和出栈的时间复杂度为常数O(1),因为与数据个数无关,直接压入弹出,操作时间短,优势便在这里,如果现实生活的使用只需用到先进后出的顺序而且只用到进出数据的比较,那就可以使用栈了。

本文转自互联网,由动力节点整理发布

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息