stack in java


class stack {

 int arr[];

 int top;

 stack(int size) {

  arr = new int[size];

  top = -1;

 }


 void insert(int data) {

  System.out.println("inserting the data " + data);

  arr[++top] = data;

 }

 void pop() {

  if (top == -1) {

   System.out.println(" stack is underflow");

   return;

  } else {

   System.out.println("deleting " + arr[top]);



   --top;


  }

 }

 void size() {

  int siz = top;

  System.out.println("the size is " + ++siz);



 }

 void change(int data) {

  System.out.println("changing the data from " + arr[top] + " to " + data);

  arr[top] = data;

 }

 void print() {

  System.out.println("your data is ");

  int to = top;

  while (to != -1) {

   System.out.print(arr[to] + " ");

   --to;

  }

  System.out.println();

 }

 public static void main(String ar[]) {

  stack s1 = new stack(10);

  s1.size();

  s1.insert(100);

  s1.insert(127);

  s1.insert(99);

  s1.print();

  s1.size();

  s1.pop();

  s1.print();

  s1.size();



 }

}


Comments