-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackDemo.java
More file actions
58 lines (51 loc) · 1.3 KB
/
StackDemo.java
File metadata and controls
58 lines (51 loc) · 1.3 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: StackDemo.java
# Author:
# Mail:
# Created Time: 2016年11月12日 星期六 14时20分39秒
************************************************************************/
class Stack{
int stck[] = new int[10];
int tos;
//initialize top - of - stack;
Stack(){
tos = -1;
}
//push an item onto the stack
void push(int item){
if(tos == 9){
System.out.println("Stack is full.");
}
else
stck[++tos] = item;
}
//pop an item from the stack
int pop(){
if(tos < 0){
System.out.println("stack is empty.");
return 0;
}
else
return stck[tos--];
}
}
class StackDemo{
public static void main(String args[]){
Stack s1 = new Stack();
Stack s2 = new Stack();
for(int i = 0;i < 10;i++){
s1.push(i);
}
for(int i = 10;i < 20;i++){
s2.push(i);
}
System.out.println("stack in s1:");
for(int i = 0;i < 10;i++){
System.out.println(s1.pop());
}
System.out.println("stack in s2:");
for(int i = 0;i < 10;i ++){
System.out.println(s2.pop());
}
}
}