Repository navigation
Expand file tree
/
Copy pathIntStack.java
More file actions
59 lines (54 loc) · 1.46 KB
/
Copy pathIntStack.java
File metadata and controls
59 lines (54 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*************************************************************************
# File Name: IntStack.java
# Author:
# Mail:
# Created Time: 2016年11月17日 星期四 23时19分04秒
************************************************************************/
interface IntStack{
//define a int stack
void push(int item);
int pop();
}
//instantiation
class FixedStack implements IntStack{
private int stck[];
private int tos;
FixedStack(int size){
stck = new int[size];
tos = -1;
}
//push an item into the stack
public void push(int item){
if(tos==stck.length-1){
System.out.println("Stack is full");
}
else
stck[++tos] = item;
}
//pop
public int pop(){
if(tos < 0){
System.out.println("Stack is empty");
return 0;
}
else
return stck[tos--];
}
}
//test class
class IFTest{
public static void main(String args[]){
FixedStack mystack1 = new FixedStack(5);
FixedStack mystack2 = new FixedStack(8);
for(int i = 0;i < 5;i++)
mystack1.push(i);
for(int i = 0;i < 8;i++)
mystack2.push(i);
System.out.println("Stack in mystack1:");
for(int i = 0;i < 5;i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2");
for(int i =0;i < 8;i++)
System.out.println(mystack2.pop());
}
}