WenJieWangFlyToWorld

导航

4 栈的操作

 1 package com.wwj.cn;
 2 
 3 public interface SqStackInter {
 4     boolean Push(SqStack sqStack, SElemType e);// 进栈操作
 5 
 6     boolean Pop(SqStack sqStack, SElemType e);// 出栈操作
 7 
 8     void InitStack(SqStack sqStack);// 初始化栈
 9 
10     boolean StackEmpty(SqStack sqStack);// 判断是否为空
11 
12     boolean GetTop(SqStack sqStack, SElemType e);// 输出栈顶元素
13 
14     void OutStack(SqStack sqStack);// 输出所有元素
15 
16 }

 

 1 package com.wwj.cn;
 2 
 3 public class SqStackOperation implements SqStackInter {
 4     private final int MAXSIZE = 10;
 5 
 6     @Override
 7     public boolean Push(SqStack sqStack, SElemType e) {
 8         if (sqStack.top == MAXSIZE - 1) {
 9             return false;
10         } else {
11             sqStack.data[sqStack.top] = e.getE();
12             sqStack.top++;
13             return true;
14         }
15     }
16 
17     @Override
18     public boolean Pop(SqStack sqStack, SElemType e) {
19         if (sqStack.top == -1) {
20             return false;
21         } else {
22             e.setE(sqStack.data[sqStack.top]);
23             sqStack.top--;
24             return true;
25         }
26     }
27 
28     @Override
29     public void InitStack(SqStack sqStack) {
30         sqStack.top = -1;
31 
32     }
33 
34     @Override
35     public boolean StackEmpty(SqStack sqStack) {
36         if (sqStack.top == -1) {
37             return true;
38         } else {
39             return false;
40         }
41 
42     }
43 
44     @Override
45     public boolean GetTop(SqStack sqStack, SElemType e) {
46         if (sqStack.top == -1) {
47             return false;
48         } else {
49             e.setE(sqStack.data[sqStack.top]);
50             return true;
51         }
52     }
53 
54     @Override
55     public void OutStack(SqStack sqStack) {
56 
57         if (sqStack.top != -1) {
58             while (sqStack.top != -1) {
59                 System.out.print(sqStack.data[sqStack.top] + " ");
60                 sqStack.top--;
61 
62             }
63             System.out.println("");
64         } else {
65             System.out.println("This Stack is Empty!");
66         }
67 
68     }
69 
70 }

 

posted on 2017-05-25 22:27  WenjieWangFlyToWorld  阅读(112)  评论(0编辑  收藏  举报