public class NodeStack {
TNode stack[] = null;
int top = 0;
//int ssize = 0;
NodeStack(int n) {
stack = new TNode[n];
}
NodeStack() {
stack = new TNode[100];
}
public boolean isEmpty() {
if (top < 0) {
return true;
} else {
return false;
}
}
public void printPop() {
System.out.println("Poppoed " + this.pop().data);
}
public void push(TNode p) {
if (top < stack.length) {
stack[top++] = p;
}
//System.out.println("Top: " + top);
}
public TNode top(){
if(top>0)
return stack[top-1];
return null;
}
public TNode pop() {
TNode p = null;
if (top > 0) {
p = stack[--top];
}
//System.out.println("Top: " + top);
return p;
}
public void printStack() {
System.out.print("Stack [LIFO order]: ");
for (int i = this.top - 1; i >= 0; i--) {
System.out.print(stack[i].data + " ");
}
System.out.println();
}
public static void main(String args[]) {
NodeStack s = new NodeStack();
s.push(new TNode(10));
s.push(new TNode(15));
s.push(new TNode(20));
s.push(new TNode(25));
s.printStack();
s.printPop();
s.printPop();
s.push(new TNode(19));
s.push(new TNode(24));
s.printStack();
s.printPop();
s.printPop();
s.printPop();
s.printPop();
s.printStack();
}
}
TNode stack[] = null;
int top = 0;
//int ssize = 0;
NodeStack(int n) {
stack = new TNode[n];
}
NodeStack() {
stack = new TNode[100];
}
public boolean isEmpty() {
if (top < 0) {
return true;
} else {
return false;
}
}
public void printPop() {
System.out.println("Poppoed " + this.pop().data);
}
public void push(TNode p) {
if (top < stack.length) {
stack[top++] = p;
}
//System.out.println("Top: " + top);
}
public TNode top(){
if(top>0)
return stack[top-1];
return null;
}
public TNode pop() {
TNode p = null;
if (top > 0) {
p = stack[--top];
}
//System.out.println("Top: " + top);
return p;
}
public void printStack() {
System.out.print("Stack [LIFO order]: ");
for (int i = this.top - 1; i >= 0; i--) {
System.out.print(stack[i].data + " ");
}
System.out.println();
}
public static void main(String args[]) {
NodeStack s = new NodeStack();
s.push(new TNode(10));
s.push(new TNode(15));
s.push(new TNode(20));
s.push(new TNode(25));
s.printStack();
s.printPop();
s.printPop();
s.push(new TNode(19));
s.push(new TNode(24));
s.printStack();
s.printPop();
s.printPop();
s.printPop();
s.printPop();
s.printStack();
}
}
# output:
Stack [LIFO order]: 25 20 15 10
Poppoed 25
Poppoed 20
Stack [LIFO order]: 24 19 15 10
Poppoed 24
Poppoed 19
Poppoed 15
Poppoed 10
Stack [LIFO order]: