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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Shweta LodhaRank #31 · 39.8K reputationPosted Apr 6, 2014, 4:31 AM EDT
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/
Lakshmanan Sethu SankaranarayanRank #124 · 17.7K reputationPosted Apr 5, 2014, 10:13 PM EDT
Abhishek JaiswalRank #126 · 17.5K reputationPosted Apr 5, 2014, 10:27 PM EDT