0% found this document useful (0 votes)
1 views3 pages

Stack Implementation in Java

This document defines a Stack class that uses an array to implement a stack data structure. The Stack class contains methods to push numbers onto the stack, pop numbers off the stack, and display the current contents of the stack. A main method then demonstrates using a Stack object to push 3 numbers, pop 1 number, and display the remaining contents of 2 numbers still in the stack.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

Stack Implementation in Java

This document defines a Stack class that uses an array to implement a stack data structure. The Stack class contains methods to push numbers onto the stack, pop numbers off the stack, and display the current contents of the stack. A main method then demonstrates using a Stack object to push 3 numbers, pop 1 number, and display the remaining contents of 2 numbers still in the stack.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

//STACK USING OBJECT AND CLASS

class Stack { private int[] number; private int top; Stack() { number=new int[10]; top=-1; } void push(int num) { if(top==9) [Link]("OverFlow"); else { top++; number[top]=num; [Link]("Number is Pushed is =" +number[top]); } } void pop() { if(top==-1) {

[Link]("Under Flow"); } else { [Link]("Number is poped is ="+number[top]); top--; } } void display() { for(int i=0;i<=top ;i++) [Link](number[i]+""); } } class stack2 { public static void main(String[] arg) { Stack s=new Stack(); [Link](14); [Link](17); [Link](20); [Link](); [Link]("Number in stack="); [Link]();

} }

OUTPUT

You might also like