Here is a class called Stack that implements a stack for integers:
class Stack {
int stck[] = new int[10];
int tos;
Stack() {
tos = -1;
}
void push(int item) {
if (tos == 9)
System.out.println("Stack is full.");
else
stck[++tos] = item;
}
int pop() {
if (tos < 0) {
System.out.println("Stack is empty.");
return 0;
} else
return stck[tos--];
}
}
public class Main {
public static void main(String args[]) {
Stack mystack1 = new Stack();
for (int i = 0; i < 10; i++){
mystack1.push(i);
}
for (int i = 0; i < 10; i++){
System.out.println(mystack1.pop());
}
}
}
This program generates the following output:
9
8
7
6
5
4
3
2
1
0
java2s.com | Contact Us | Privacy Policy |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |