Write a java program that if the STACK is full it wont add element and display the msg that STACK is full.
and
Write a program when the Stack is empty display the msg stack is empty.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Shweta LodhaPosted Apr 6, 2014, 8:31 AM
packagealgorithm.stack;publicclassTNStack {privateintsize;privateint[] stackArr;privateinttop = -1;publicTNStack(intsize) {this.size = size;stackArr =newint[size];}/*** increment the ctr and push element into stack* @param i element to be pushed*/publicvoidpush(inti) {top++;System.out.println("Pushing "+i);stackArr[top] = i;}/*** pop the element from stack and decrement the ctr* @return the popped element*/publicintpop() {inti = stackArr[top];top--;System.out.println("Popping "+i);returni;}publicintpeek() {System.out.println("Peek "+stackArr[top]);returnstackArr[top];}publicbooleanisFull() {return(top == size-1);}publicbooleanisEmpty() {return(top == -1);}}packagealgorithm.stack;/*** @author ntallapa**/publicclassTNStackClient {/*** @param args*/publicstaticvoidmain(String[] args) {TNStack tns =newTNStack(3);// push some elementsif(!tns.isFull())tns.push(4);if(!tns.isFull())tns.push(5);if(!tns.isFull())tns.push(3);if(!tns.isFull())tns.push(6);elseSystem.out.println("Stack is full, cannot push element");// pop some elementsif(!tns.isEmpty())tns.pop();if(!tns.isEmpty())tns.pop();if(!tns.isEmpty())tns.pop();if(!tns.isEmpty())tns.pop();elseSystem.out.println("Stack is empty, cannot pop element");//reinsert to verify peek methodif(!tns.isFull())tns.push(6);// peek couple of times; result should be sametns.peek();tns.peek();}}
Original link: http://tekmarathon.wordpress.com/2013/03/12/stack-implementation-in-java/comment-page-1/
Abhishek JaiswalPosted Apr 6, 2014, 2:27 AM
Lakshmanan Sethu SankaranarayanPosted Apr 6, 2014, 2:13 AM