public class ListStack {
Node stack = null;
// int top = 0;
public void printPop(){
System.out.println("Poppoed " + this.pop());
}
public static void main(String args[]){
ListStack s = new ListStack();
s.push(10);
s.push(15);
s.push(20);
s.push(25);
s.printStack();
s.printPop();
s.printPop();
s.push(19);
s.push(24);
s.printStack();
s.printPop();
s.printPop();
s.printPop();
s.printPop();
s.printStack();
}
public void push(int p){
Node tmp = new Node(p);
if(stack == null){
stack = tmp;
}else{
tmp.next = stack;
stack = tmp;
}
}
public int pop(){
Node tmp = null;
if(stack != null){
tmp = stack;
stack = stack.next;
return tmp.data;
}
return -99999;
}
public void printStack(){
System.out.print("Stack [LIFO order]: ");
for(Node t=stack; t != null; t=t.next){
System.out.print(t.data + " ");
}
System.out.println();
}
}
Node stack = null;
// int top = 0;
public void printPop(){
System.out.println("Poppoed " + this.pop());
}
public static void main(String args[]){
ListStack s = new ListStack();
s.push(10);
s.push(15);
s.push(20);
s.push(25);
s.printStack();
s.printPop();
s.printPop();
s.push(19);
s.push(24);
s.printStack();
s.printPop();
s.printPop();
s.printPop();
s.printPop();
s.printStack();
}
public void push(int p){
Node tmp = new Node(p);
if(stack == null){
stack = tmp;
}else{
tmp.next = stack;
stack = tmp;
}
}
public int pop(){
Node tmp = null;
if(stack != null){
tmp = stack;
stack = stack.next;
return tmp.data;
}
return -99999;
}
public void printStack(){
System.out.print("Stack [LIFO order]: ");
for(Node t=stack; t != null; t=t.next){
System.out.print(t.data + " ");
}
System.out.println();
}
}
No comments:
Post a Comment